Archive for the ‘django’ Category

Geographic Projection Web Services redux

Tuesday, January 9th, 2007

A couple of years ago I was playing around with Twisted Python and developed a simple web service for reprojecting data (this is no longer live). It was merely a glorified wrapper around the spatial reference tools in GDAL, but a few folks found it useful and I still receive an email about it here and there.

Lately, I’ve been playing around with Django, mostly as an effort to find a less punishing framework than Zope/Plone. I’m not very much of a web person however, but I’ve been following some of the recent developments like OpenLayers, GeoJSON, and, of course, the Google. One of the more common and somewhat insane things that I see as part of my daily routine hanging out on #gdal on IRC is somebody showing up asking about projection math and how they can implement it in JavaScript. The Open Source GIS world already has a world-class projections library in Proj.4, but for some reason it makes sense to a few folks to try and re-invent this wheel. I’ve always thought it was kind of silly, and any attempt to do so would probably result in a miserable development effort that retreads the misery that Gerald and Frank endured making Proj.4 in the first place.

JSON

JSON, or JavaScript Object Notation, is rapidly becoming the transport of choice for all of the AJAXian wunderkinds out there. The greatest advantage of JSON is that the parsing overhead of it for JavaScript is zero, and it can practically be eval’d in directly to the client. With these two things in mind, I set about to play around with a JSON-emitting webservice that projects geographic data (points).

The projector lives at http://projection.hobu.biz/json . It takes in these parameters:

Parameter Value Description
x -93.0 The x coordinate (longitude)
y 42.0 The y coordinate (latitude)
inref EPSG:4326 The input spatial reference of the point
outref EPSG:26915 The output spatial reference of the point
function myfunction The name of the JavaScript function to wrap the output in
id 3 An id to give the data

Here’s an example that projects a point in Iowa from EPSG:4326 to EPSG:26915 (decimal degrees WGS 84 to UTM Zone 15):

http://projection.hobu.biz/json/project/?y=43&x=-93&function=myfunction&outref=EPSG%3A26915&inref=EPSG%3A4326

Using the service with EPSG codes would probably be the most common way to work, but because the service builds on the spatial referencing library in GDAL, there are many more options. For example, we could specify our input projection in Proj.4 format instead:

Proj.4 input example

Or even get really nuts and specify our output projection in OGC WKT:

OGC WKT output example

Finally, we can output a coordinate in a spatial reference that doesn’t have an EPSG code, like USGS Albers:

USGS Albers output example

Python usage

You aren’t limited to using GET requests either (although you could probably construct one easily with urllib). For example, with jsonrpclib for Python, you can request projection of points much like you would with XMLRPC:

>>> import jsonrpclib>>> rpc = jsonrpclib.ServerProxy('http://projection.hobu.biz/json')>>> rpc.project('-93.0', '42.0', 'EPSG:4326', 'EPSG:26195')>>>{u'id': 2, u'result': {u'features': {u'center': [500000.0, 4649776.2247029999, 0.0], u’title’: u’title’, u’spatialCoordinates’: [[500000.0, 4649776.2247029999, 0.0]], u’srs’: u’EPSG:26915′, u’geometryType’: u’point’, u’id’: u’1′}}}

If you find it useful, let me know.