There’s an increasing amount of useful packages that allow for spatial analysis in python. Having said that, actually drawing a map remains relatively tricky, here I am sharing a few of the methods that I have come up with recently to help in this area. Firstly, let’s consider the basic set of prerequisites that you should have installed to do some useful things in Python.
Numeric Python and Scientific Python vastly extend the scientific programming capabilities of Python. Numpy adds the array() object which, for numeric matters, is far superior to the standard Python List, as well as numerous mathematical methods. SciPy then makes use of these methods to provide interfaces to allow the use of numerous mathematical techniques, I have made use of SciPy’s Clustering routines, as well as elements that allow Multi Dimensional Scaling.
matplotlib – easy_install matplotlib
Standard Python plotting library, with excellent graphic data visualisation capabilities built in and an excellent set of examples and tutorials. We can leverage the ‘patch’ plotting capabilities for creating maps.
descartes – easy_install descartes
This is the matplotlib lever that allows us to draw geojson-like geometries on a matplotlib canvas effectively.
This is a c/c++ library which is accessible in Python which makes reading and writing many different spatial data formats easy, this includes shapefiles, kml, gml etc. GDAL works specifically with raster data, and OGR with vectors. Actually setting this up can be tricky, for a start you need to make sure you have a c compiler installed, and that all the paths work. Pretty easy on a Mac (and i guess Linux), but a nightmare on Windows. Be prepared to consult forums.
This is a superb spatial analysis library that is currently under development. Provides a shapefiel reader is gdal is out of the question as well as numerous other modules. I’ve been using the ESDA, exploratory spatial data analysis, module a lot as it includes a series of useful map classification functions.
Now, the first attempt we will make to draw a map in Python will be a simple one. This will involve using data that is already in a projected coordinate system, so no need for coordinate transformations and we will simply visualise one aspect of the data using different classes. The model we will follow to create the map is a standard approach as such:
- Read in data, in this example as a shapefile
- Parse attributes to create the boundary values for classification
- Draw geometries, styling as per the previously defined boundary values
- Add a Legend, North Arrow, Scale Bar and add any required text
- Display or save the map.
In creating the above map there are a few elements to remember that add to the challenge. Firstly, the north arrow is an image that I made and simply loaded into the map, secondly, the scale bar is simply an appropriately scaled square patch with some explanatory text and thirdly the class colours come from colour brewer. For the map I’m using the freely available population data from london.data.gov.uk and district boundaries the OS Boundary Line open data release. These are both subject to Crown Copyright of course, but I provide them rather than force you to expressly download them. I’ve also included the north arrow image that I made in Processing. I last used the population data shapefile to visualise population cartograms in Processing.