On Dec 26, 2009, at 1:27 PM, Cristian Salamea wrote: > already have models that use geodjango then rendering with Mapnik > may be the next step. Along these lines I've written code to render > django querysets and model instances to Mapnik but this code is not > public ATM. However that's the easy part and the real challenge to > getting beautiful maps is creating stylesheets for Mapnik which is a > topic indepedent of django. For that issue I'd recommend looking > into Cascadenik. > > and yes , i want to render my models with mapnik, i am reading and > testing mapnik-utils, but my first issue is render models and > querset as you said, if you can help me with a little example should > be great.
Cristian, sure - the basic approach is: 1) turn a Django Queryset or Model reference into a Mapnik PostGIS datasource object 2) Attach that datasource to a Mapnik Layer 3) Associate styles with that layer, a Mapnik Map object, then render Again the key is styling your maps. All a model or queryset represents is the datasource. If you are new to GIS, installing QGIS may be helpful to simply view your data without doing any programming using spatial concepts. Then, using Quantumnik (http://bitbucket.org/springmeyer/quantumnik ), you can play around with styling maps and can use the generated XML to style your django models. Nevertheless, the below code represents a snippet for turning a model or queryset into a datasource, as you requested: It depends upon Mapnik 0.7.0 (which is soon to be released from the 0.7 branch as http://svn.mapnik.org/branches/0.7) and should work with Django > 1.1 including trunk (with the new multi-db support). import mapnik from django.conf import settings base_params = {'user':settings.DATABASE_USER, 'dbname':settings.DATABASE_NAME, 'password':settings.DATABASE_PASSWORD, 'user':settings.DATABASE_USER, 'host':settings.DATABASE_HOST, 'port':settings.DATABASE_PORT, 'persist_connection':True, } def model_to_mapnik_ds(model,geom_name=None): if geom_name: geom_field = model._meta.get_field(geom_name) else: geom_field = model.objects.all().query._geo_field() if not geom_field: raise ValueError('Geometry field not found') params_ = base_params.copy() params_['estimate_extent'] = False params_['extent'] = '%s,%s,%s,%s' % model.objects.extent(field_name=geom_field.name) params_['table'] = str(model._meta.db_table) params_['geometry_field'] = geom_field.name params_['srid'] = geom_field.srid return mapnik.PostGIS(**params_) def qs_to_mapnik_sql(qs): try: sql, args = qs.query.as_sql() from django.contrib.gis.db.backends.util import gqn except AttributeError: # running multidb available in django 1.2 sql, args = qs.query.get_compiler(qs.db).as_sql() gqn = qs.query.get_compiler(qs.db).connection.ops.geo_quote_name if args: sql = sql % tuple([gqn(i) for i in args]) return """(%s) as django_table""" % sql def qs_to_mapnik_ds(qs,geom_name=None,geom_attribute=None): if geom_name: geom_field = qs.model._meta.get_field(geom_name) elif not geom_attribute: geom_field = qs.query._geo_field() elif geom_attribute: geom_field = getattr(qs[0],geom_attribute) geom_field.name = geom_attribute if not geom_field: raise ValueError('Geometry field not found') params_ = base_params.copy() subquery = qs_to_mapnik_sql(qs) sub_low = subquery.lower() if 'where' in sub_low or sub_low.count('from') > 1: params_['extent_from_subquery'] = True else: params_['extent_from_subquery'] = False params_['table'] = subquery params_['geometry_field'] = geom_field.name params_['srid'] = geom_field.srid return mapnik.PostGIS(**params_) _______________________________________________ Mapnik-users mailing list [email protected] https://lists.berlios.de/mailman/listinfo/mapnik-users

