# Getting extent (bounds) of each feature using Fiona

I was looking in Fiona to get for each feature its extent but I didn't find how.

I have try to far to do something like below

``````import fiona

for f in source:
geom = f['geometry']
print geom
``````

I was expecting to get a method for this at the f variable level. After some documentation reading, I've seen that f is a pure python record. So at the end, how with Fiona can I get the extent (or bounding box) of each feature geometry?

PS: I already know the pure GDAL/OGR python solution so I expect a Fiona solution please.

• I would be really interested in seeing the OGR python solution. I'm going through the documentation, and it looks like extents for layers and features are handled differently in OGR. Apr 16, 2014 at 0:22

I'd do it like this:

``````def explode(coords):
"""Explode a GeoJSON geometry's coordinates object and yield coordinate tuples.
As long as the input is conforming, the type of the geometry doesn't matter."""
for e in coords:
if isinstance(e, (float, int, long)):
yield coords
break
else:
for f in explode(e):
yield f

def bbox(f):
x, y = zip(*list(explode(f['geometry']['coordinates'])))
return min(x), min(y), max(x), max(y)
``````
• Just in case it's not obvious: the approach above works for deserialized GeoJSON generally and isn't limited to use with Fiona. Mar 23, 2014 at 15:33
• I made a slight change to use ESRI's json and this solved one of my problems as well. Thanks! Apr 3, 2016 at 18:40

You need to use the function `shape` of Shapely:

``````from shapely.geometry import shape
# first record
country = c.next()
print "country name :",country['properties']['NAME']
country name : Aruba
# shape(country['geometry']) -> shapely geometry
print "bounds:", shape(country['geometry']).bounds
bounds: (-70.062408006999874, 12.417669989000046, -69.876820441999939, 12.632147528000104)
``````
• I've accepted @sgillies answer because no lib dependency but I appreciate also your answer. Thanks Mar 23, 2014 at 11:07
• It does Thomas, but there you go, it's just not implicit! Oct 23, 2017 at 14:21
• As of Fiona 2.0, use `country = next(iter(c)` for `Collection.__next__() is buggy`.
– Binx
Jul 19, 2021 at 21:37

Great answers above, just want to add that using `rasterio` instead of `shapely` you can do

``````import fiona
import rasterio