Taming the OGR
2007-11-06T07:26:53Z | Comments: 5
This evening I made a protoype of a smoother, simpler interface to the industrial-strength vector data functions in libgdal. The new entities in Refinery are workspaces, collections, and features. Workspace.collections is a dict of Collection objects, which are approximately equal to OGR layers. Collection.features is an iterator over GeoJSON-style objects:
>>> workspace = refinery.workspace('/var/gis/data/world')
>>> workspace.collections.keys()
['world_borders']
>>> borders = workspace.collections['world_borders']
>>> for feature in borders.features:
... print feature.id, feature.properties['CNTRY_NAME']
world_borders/0 Aruba
(etc)
Use of the refinery package reduces the size of my canonical matplotlib script from 17 to 12 lines:
import pylab
from numpy import asarray
import refinery
from shapely.wkb import loads
workspace = refinery.workspace('/var/gis/data/world')
borders = workspace.collections['world_borders']
fig = pylab.figure(1, figsize=(4,2), dpi=300)
for feature in borders.features:
shape = loads(feature.geometry)
a = asarray(shape.exterior)
pylab.plot(a[:,0], a[:,1])
pylab.show()
That's starting to feel civilized.
This started off as recreational programming, but I think it has some promise. Get the code [Refinery-0.0.tar.gz] if you're curious and make a trial workspace from your own data. Next: a look at the GeoDjango GDAL wrappers to see what Justin and I are doing differently.
