[GIS] Build a leaflet polygon(s) from GeoJSON feature(collection)

geojsonleafletpolygon

I find it quite practical to work with leaflet Polygon and it would be great to be able to build such objects directly from GeoJSON content. I did not find any Polygon constructor taking neither a GeoJSON object nor a GeoJSON string as input. Is there some method to build a leaflet polygon directly from a feature or an array of polygons from a featurecollection?

Something like:

geoJ = GeoJSON object returned/parsed from ajax call
pol = L.polygon( geoJ.features[0] )
bounds = pol.getBounds()

Best Answer

Besides converting a feature to a Polygon with a dedicated function (see this answer), L.geoJson(geoJ) can be used to get a collection of objects. In case the input geoJ contains polygons, the objects themselves will be set to be L.Polygons, hence the conversion is done automatically. Summing up in both cases:

geoJ = GeoJSON object returned/parsed from ajax call
pol = L.polygon( geoJ.features[0] )
bounds = pol.getBounds()

and

geoJ = GeoJSON object returned/parsed from ajax call
polCollection = L.polygon( geoJ )
polCollection.eachLayer( function(pol) {
    bounds = pol.getBounds()
})

give access to the(each) polygon bounds.

Thanks to @nathansnider and @ghybs for the useful comments.