Fiona now writes feature collections to disk. Here's a bit of code from the tests, dressed up with extra comments:
from fiona import collection from shapely import asShape, mapping # Open a source of features with collection("docs/data/test_uk.shp", "r") as source: # Define a schema for the feature sink schema = input.schema.copy() schema['geometry'] = 'Point' # Open a new sink for features with collection( "test_write.shp", "w", driver="ESRI Shapefile", schema=schema ) as sink: # Process only the features intersecting a box for f in source.filter(bbox=(-5.0, 55.0, 0.0, 60.0)): # Get their centroids using Shapely f['geometry'] = mapping(asShape(f['geometry']).centroid) # Stage feature for writing sink.write(f) # The sink shapefile is written to disk when its ``with`` block ends
That's just 9 statements. Fiona isn't just about less code, it's
about taking advantage of Python built-ins and idioms to shrink the API's
cranial memory footprint. You already know dicts, and data are better than
objects, so features are modeled as GeoJSON-like mappings. Feature schemas are
mappings, too. You already know how Python file I/O works, so persisted
featured collections are modeled like files. Obviousness and familiarity are what
I'm going for here. If you have to call
help(fiona) more than 8 times in
your entire life, I'll have failed.
I still need to work on support for writing geometry types other than 'Point', coordinate reference systems and make sure it's tight memory-wise (Fiona is all C underneath). It also might be nice to let the sink collection's schema be set from the first written feature, making the above example only 7 statements. The OGR library is so loaded with features – making a simple wrapper API is almost entirely about saying no and throwing features out. And how I've thrown things out. Geometries, out. Fields, out. Features, out. Cursors, out. Layers, out. There's almost nothing left except "open file", "filter iterator", "next dict", "append dict" and "close file". It almost goes without saying that this is for minimalists only.
Update (2011-12-10): the "writing" branch of Fiona now writes polylines and polygons. Multipart geometry types coming soon.