Thursday, April 23, 2015

Drawing My First Map with Python

I was able to use python to read data from a shapefile and generate a map of voter precincts in Arlington County, Virginia.

Arlington County, Virginia makes numerous maps available on-line here.  The specific shapefile I started with holds boundaries for voter precincts in the county, and is available here.

I used the Voter_Precinct shapefile and adapted what I learned about gdal and mapnik from Python Geospatial Development. Specifically, the following chapters contained the examples I needed for this first map:
  • Chapter 3 - Python Libraries for Geospatial Development
  • Chapter 5 - Working with Geospatial Data in Python
  • Chapter 8 - Using Python and Mapnik to Produce Maps
I also read through the ESRI Shapefile Technical Description.

The Scripts

I wrote the following two scripts, which are now located in my gis_front_and_back repo on github:
The file, also in the github repo, contains a link to where to get the shapefile.

Since I use Python 3 whenever I can, the first script uses Python 3, while the 2nd, which requires mapnik, is written in Python 2.  Running the first script and redirecting the output to a file produces this.

Running the 2nd script generates this map:

Making this map by adapting the example scripts from the text was fairly trivial.  I've been unable to get filters to work thus far, however, which will enable me to vary the colors of the individual precincts based on rules.

I've been wanting to make a map ever since last November's election which will color code the results by precinct in the race between John Vihstadt and Alan Howze (see John Vihstadt beats Democrat Alan Howze in race for Arlington County Board seat).  The key issue in this race was the proposed Columbia Pike Streetcar, which was canceled soon after the election (see Arlington Cancels Streetcar Program).

I grabbed precinct election results from the website and saved them in a PDF file which I still have here. I was struck by how the support for the streetcar was clearly visible near the communities most effected by it (along the Columbia Pike corridor), while opposition (assuming a direct correlation between opposition to the streetcar and support for Vihstadt) was strongest in North Arlington, furthest away from where the streetcar would have run. This kind of data cries out for a good map, so I've been wanting to make one ever since.

I just downloaded the complete election result data in CVS format from here.  My most immediate problem now is to figure out how to color individual precincts based on filters.  I'll also look further into how to manipulate shapefiles, perhaps using the pyshp library, which can be installed on Ubuntu through the python-pyshp and python3-pyshp debian packages for versions 2 and 3 of Python respectively.  Then I'm thinking I'll either figure out how to read the election result data from the CVS file, or use the PDF to read the election results for each precinct and to color code them on the map accordingly.

No comments:

Post a Comment