My recent work on creating a more flexible circular cartogram algorithm meant that I needed (well, wanted) to load shapefiles into a Python application. After many searches and scouring of message boards, I settled on the OGR/GDAL libraries. And 3 hours later I had it installed on my MacBook…
I’m sure that OGR (vector) and GDAL (raster; pronounced, apparently, like ‘goodle’) are great for their intended purpose: providing a jack-of-all-trades library for doing pretty much anything with geospatial data. But for users who need to do only simple tasks, they’re overkill. And slow. Loading a small shapefile took a few seconds, and accessing points to, say, compute centroids took a few more. This was too much. I needed a simple and quick module to load shapefiles and populate a list of their contents. I couldn’t find one, so I wrote my own. Have a gander:
[UPDATE: The following file has been modified so that it actually works]
Here’s a simple use example:
# load the shapefile, populating a list of dictionaries
shpRecords = shpUtils.loadShapefile('shapefiles/gb.shp')
# now do whatever you want with the resulting data
# i'm going to just print out the first feature in this shapefile
for part in shpRecords['shp_data']:
print part, shpRecords['shp_data'][part]
The above code (tried out on a shapefile of British counties), produces the following output (copy-pasted to a file).
After working on the module for a while, I realized Python doesn’t have a standard utility for loading DBF files (like PHP’s dbase_open). A few such libraries exist for Python, but all were overkill for my purposes. Then I stumbled onto this on ASPN — a very simple DBF reader/writer module by Raymond Hettinger. I copy-pasted it into the following file:
You’ll need that file, too, if you want to use my shapefile module. Enjoy!