I have a openlayers website like this one that gives the user the ability to draw polygons and then creates a GeoJSON file. I want to process this GeoJSON with a python script. The python script returns in the end a string that should be returned to the website and displayed as a info box.

What kind of frameworks/apps/steps should I look into to accomplish this project?


Server: You need a framework on the server side, there are good python options: Django, Flask, Bottle, webpy. That framework will receive a (ajax) request process the GeoJson and returns the string.
After the information enters the framework you are now "pythonic" and can use the tool of you preference to read and work with the GeoJSON, that could be for example Shapely or Osgeo OGR Python bindings. If you choose Django, it already come with it's geometry helpers - GeoDjango.

Client: On the client side, you need something to make the steeps of making the request, receiving the answer and showing the info box easier. For that you could use frameworks/tools like ExtJS, Jquery (UI), Dojo, KendoUI

Personally I use a combination of Django + ExtJS to accomplish this kind of thing. It's a heavy load combination, I mean, it's has far more resources than you need. But is so easy to use and it's so very well documented that I wouldn't recommend other thing to get started. After doing the basic tutorial of both (Django, Extjs) you have enough information to accomplish the task stated on the question. Later you can try other options.

Btw, Extjs if free only for open source projects, take that into account.

Btw2, to run the python framework on the server you will need a web server with wsgi. My option is Nginx+uwsgi. Take a look at the deploy section of django documentation.


Pablo's suggestion of Flask (http://flask.pocoo.org/) is right on. Unless you've got other unmentioned requirements, Django is overkill.

Use jQuery.post to send your GeoJSON string to your Flask app, where you decode it using json.loads. Shapely (https://pypi.python.org/pypi/Shapely) is much simpler to use than OGR's Python bindings and has 2 functions dedicated to serializing GeoJSON, so use that to operate on your GeoJSON geometry. Say you just want to "clean" geometries:

import json
from shapely.geometry import mapping, shape

# 'data' is a GeoJSON string.
geom = shape(json.loads(data))

# The 0 buffer cleaning trick.
cleaned = geom.buffer(0.0)

return json.dumps(mapping(cleaned))

In jQuery.post's success callback, you get the returned GeoJSON and do whatever you want: show a message, redraw the feature, whatever.

  • Thanks for your recommendations. My python script already uses OGR's Python bindings. So I think I will look into jQuery on the Client side and Flask, like you recommended, on the Server side. I will use OGR's Python bindings to handel the GeoJSON string inside my python script (it has a GeoJSON driver). – ustroetz Apr 17 '13 at 17:22
  • Using OGR's GeoJSON driver instead of Python's built-in and better json module is not a good idea. – sgillies Apr 17 '13 at 17:40
  • Well I want to process the GeoJSON string with OGR. So I think it makes sense. But I will see once I get to it. – ustroetz Apr 17 '13 at 18:57

For geometry representation on client side,OpenLayers has the ability to read/write data in designated format & projection which can be done via OpenLayers.Format class as described here.I suggest GeoJSON format for vector operations like this.

In your example link,geometry is read,your app should take feature/vector into consideration

For Server Side,As Pablo suggested,there are some bindings for django framework for python.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.