Inspired by comments I saw today on the internet, this is how you spatially clean features using Fiona and Shapely (for small values of dirty).

importloggingimportsysfromshapely.geometryimportmapping,shapefromfionaimportcollectionlogging.basicConfig(stream=sys.stderr,level=logging.INFO)withcollection("docs/data/test_uk.shp","r")asinput:schema=input.schema.copy()withcollection("with-shapely.shp","w","ESRI Shapefile",schema)asoutput:forfininput:try:# Make a shapely object from the dict.geom=shape(f['geometry'])ifnotgeom.is_valid:# Use the 0-buffer polygon cleaning trickclean=geom.buffer(0.0)assertclean.geom_type=='Polygon'assertclean.is_validgeom=clean# Make a dict from the shapely object.f['geometry']=mapping(geom)output.write(f)exceptException,e:# Writing uncleanable features to a different shapefile# is another option.logging.exception("Error cleaning feature %s:",f['id'])

Makes a neat example, I think. Shapely just happens to provide this handy zero buffer feature (via GEOS), it's not required at all. Fiona just reads and writes GeoJSON-like mappings (like Python's dict), it doesn't care what you do in between or what you do it with.

Update (2011-12-25): The error I noted in the comments is fixed above.

Comments

Re: Fiona and Shapely: spatially cleaning features

Author: Sean

Oops, there's an error above. I mean to assert that the cleaned geom is valid.