On Apr 14, 2010, at 3:43 AM, Gilles Bassière wrote:

Hi,

I mostly use Django for my projects and I enjoy really much the GEOS and OGR integrated bindings. I've sometimes wished there was a simple way to
display the objects of a geo-model. I know that GeoDjango already have
GeoFeeds to stream objects as vector data, olWidget is also nice in
order to display/edit a geometric field. Unfortunately, I can't find a
clean and easy way to stream maps of my objects (WMS or equivalent). I
believe Mapnik and its Python bindings would fit very well for this purpose.

So do I  :)

I can see at least three different use-cases within Django that Mapnik could be used to solve nicely.

1) being able to get an instant view of geodata within the interpreter (or at breakpoints in code)

>>> from models import World
>>> World.objects.filter(name='China').show() # which would pop open a basic graphic of the polygon making up china with default styles

2) being able to render map graphics from django queries. So, static images, that get rendered once, then likely cached, sort of like sorl- thumbnail but for geo data.

3) being able to roll a quick TMS or WMS service based on a django model's data


I could set up ogcserver and configure a datasource reading in the
underlying database but in such architecture, map rendering lives
completely outside Django and this is really "static" and involves many
limitations.

My assumption is that having map serving live outside of django is a better approach performance-wise. But for small apps where performance is not critical, certainly having an automatic WMS/TMS via django would be a great thing, and maybe not all that slow... Will be interesting to test.

What I would like to do is the following:

   # Import my Django model
   from models import MySuperModel

   # Import mapnik and something that ease integration with Django
   import mapnik
   from ... import MapnikLayer

   # Instantiate a Mapnik map
   m = mapnik.Map(600,300,"+proj=latlong +datum=WGS84")

   # create style here

   # Create a mapnik layer based on my Django model
   lyr = MapnikLayer(name='supermodel',
                     queryset=MySuperModel.objects.all())

   # finalize map here

Using a Django queryset would allow simple filtering of objects through
Django's ORM. For example, one could use such code in a Django view in
order to render a map of the current user's objects.

Yes, exactly.


I can imagine two ways of implementing this:
- The first is to write a Mapnik plug-in but I am not sure it's even
possible. I don't know much about C++ but I believe that the C++ plug-in
code can't use Django's ORM functionalities, am I right?

The only thing I can see needing to be done on the Mapnik side is to expose the ability to pass in WKT/WKB and being able to create Mapnik features in memory. But this would only be useful for small sets of data, and the approach below would be much faster...

- The second is to write an helper function or class which detect the
back-end, the queryset SQL query, the queryset geometric attribute and
instantiate the proper mapnik Layer with this information.


Yep. I've had code to do this for a while, and I just pushed publicly it for your review:

http://bitbucket.org/springmeyer/djmapnik/

Consider it really only a proof-of-concept right now as much may change.

I'm merely thinking out loud here. I've not yet planned any development but I may try to code something like this in the future and, if so, I'd be pleased to get your comments as I'm not sure if it is feasible or if
I'm on the right track.

I think you are certainly on the right track. Feel free to code something up independently and share where you get, or take a look at what I have so far and let me know what you would change/fix. The 'djmapnik' name is kind of a joke - I'm not sure what to call these helper functions, but ideally they could get tested enough to become part of GeoDjango some day.

Dane


Cheers

--
Gilles Bassière - Web/GIS software engineer
http://gbassiere.free.fr/
_______________________________________________
Mapnik-users mailing list
[email protected]
https://lists.berlios.de/mailman/listinfo/mapnik-users

_______________________________________________
Mapnik-users mailing list
[email protected]
https://lists.berlios.de/mailman/listinfo/mapnik-users

Reply via email to