Visualising GeoJSON in 15 Minutes

The objective of this post is to save a GeoJSON feature collected from the web to an image file, and was inspired by the Shapely manual. This will be done using Windows, Python 2.5 (although 2.6 should be almost exactly the same steps), and some additional Python packages. This example uses GeoJSON data from a CloudMade geocoding service.

image

The greatest difficulty when trying to do things in Python is getting the right packages to match your Python version, and making sure all the package dependencies are in place.

Set Up Steps

1. Download Numpy from http://sourceforge.net/projects/numpy/files/NumPy/. Numpy is “a package that defines a multi-dimensional array object and associated fast math functions that operate on it.”

Check that you download the version that matches your Python installation.  In my case I needed numpy-1.4.1-win32-superpack-python2.5.exe.

image

2. Download MatPlotLib from http://sourceforge.net/projects/matplotlib/files/. I downloaded matplotlib-0.99.1.win32-py2.5.exe. Again there are many different versions that can be downloaded – expand the arrows to see the options.

There’s even a 64-bit version for Windows. However there doesn’t appear to be a 64 bit version of Numpy for Windows, and as Numpy is required by MapPlotLib to run I’m not sure how it could ever be of use. This is where the whole Python packages and 3rd party libraries gets exceedingly confusing..

3. Install the Python setuptools from http://pypi.python.org/pypi/setuptools. These tools include easy_install which “lets you automatically download, build, install, and manage Python packages.” Again get the relevant Python package in my case setuptools-0.6c11.win32-py2.5.exe.

If you have only installed Python during your log in session then reboot to refresh your environment variables. This should allow you to use the easy_install program directly from the Windows Command Prompt (by typing easy_install rather than the full path).

4. We can now install descartes using the easy_install program. Descartes is uses “geometric objects as matplotlib paths and patches.” Type the command below (the full path may not be required) into a Windows Command Prompt:

C:\Python25\Scripts\easy_install.exe descartes

This command should return output similar to below:

image

If you already have IDLE (the Python editor open) then quit it and restart. If not you are likely to get “ImportError: No module named X” errors, even though the modules have been installed.

5. You should now be able to run the Python script below to generate a PNG image file of England.

from matplotlib import pyplot
from descartes import PolygonPatch
import math
import urllib2
import simplejson

def getData():
    #create the cloudmade query url
    root = 'http://geocoding.cloudmade.com'
    apikey = '85453debd0fc4ad18c5855c3d8eef780'
    query = 'england'
    url = '%s/%s/geocoding/v2/find.geojs?query=%s&return_geometry=true' \
          % (root,apikey,query)

    #open the url to get the geojson
    return urllib2.urlopen(url).read()

def configurePlot():
    #set up the mapplotlib
    fig = pyplot.figure(1, figsize=(10, 4), dpi=180)
    ax = fig.add_subplot(121)
    return ax

def setPlotExtent(ax,data):
    #get feature extents (a property of the cloudmade geojson)
    #note this was previously bbox
    minx = data['bounds'][0][0]
    maxx = data['bounds'][1][0]
    miny = data['bounds'][0][1]
    maxy = data['bounds'][1][1]

    #set the graph axes to the feature extents
    ax.set_xlim(minx,maxx)
    ax.set_ylim(miny,maxy)

def plotFeature(coordlist, myplot):
    #create a polygon geojson-like feature
    poly = {"type": "Polygon", "coordinates": coordlist}
    patch = PolygonPatch(poly, fc='#6699cc', ec='#6699cc', alpha=0.5, zorder=2)
    #plot it on the graph
    myplot.add_patch(patch)

#turn the geojson into a python object
pydata = simplejson.loads(getData())
print pydata

myplot = configurePlot()
setPlotExtent(myplot,pydata)

#loop through each polygon in the MULTIPOLYGON collection
for coordlist in pydata['features'][0]['geometry']['coordinates']:
    plotFeature(coordlist, myplot)

#save the plot as an image
pyplot.savefig('myplot.png')

I now have a way of easily visualising GeoJSON I have a way of checking spatial operations on spatial data manipulated directly from the cloud. More to follow.



3 views shared on this article. Join in...

  1. geographika says:

    @Sean Gillies
    Thanks for the link Sean. I realised after reading further there is a find.js page to return JSON – which can also return coordinates in the [lat, lon] order, and a find.geojs which return geojson in a [lon,lat] order.
    Once I updated the code to use the find.geojs all worked fine.

  2. Sean Gillies says:

    Calling ax.set_aspect(1) will square up that plot. Thank you for blogging this. I’m pleased to see that this mostly works out of the box with win32.

Leave a Reply

Your email address will not be published. Required fields are marked *

Comment

You may use these tags : <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>