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
with fiona.open('countries/ne_10m_admin_0_countries.shp', 'r') as source:
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
Answer
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)
No comments:
Post a Comment