Saturday, October 26, 2013

Plotting state boundary data from shapefiles using Python

The great folks at census.gov have put up some of the data they collect so we can download and use it. On this page they have data relating to state boundaries. The files are available as zipped directories containing a shapefile and other metadata information. If you want to plot state boundaries and some state metadata (like zip code, state name) the .shp shapefile is sufficient. Assuming that the shape file is 'tl_2010_us_state10/tl_2010_us_state10.shp', some sample code using the pyshp package is:
#http://stackoverflow.com/questions/10871085/viewing-a-polygon-read-from-shapefile-with-matplotlib
#http://stackoverflow.com/questions/1441717/plotting-color-map-with-zip-codes-in-r-or-python
import shapefile as sf, pylab

map_f = sf.Reader('tl_2010_us_state10/tl_2010_us_state10.shp')
state_metadata = map_f.records()
state_shapes = map_f.shapes()

for n in range(len(state_metadata)):
  pylab.plot([px[0] if px[0] <0 else px[0]-360 for px in state_shapes[n].points],[px[1] for px in state_shapes[n].points],'k.',ms=2)

for n in range(len(state_metadata)):
  pylab.plot(float(state_metadata[n][13]),float(state_metadata[n][12]),'o')
pylab.axis('scaled')
The pyshp package makes things so easy! Note that you can plot continuous lines instead of dots for the state boundaries, however, for some states like Alaska and Florida with islands, where the boundaries are not contiguous, you get nasty disjoint lines. Removing this requires much more processing (unless you do it by hand and break down states into "sub-states".

No comments:

Post a Comment