Created
November 5, 2009 23:54
-
-
Save springmeyer/227536 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# turn a django model into a mapnik datasource | |
def model_to_mapnik_ds(model,geom_name=None): | |
params = {'user':settings.DATABASE_USER, | |
'dbname':settings.DATABASE_NAME, | |
'password':settings.DATABASE_PASSWORD, | |
'user':settings.DATABASE_USER | |
} | |
if geom_name: | |
geom_field = [f for f in model._meta.fields if f.name == geom_name][0] | |
else: | |
geom_field = [f for f in model._meta.fields if isinstance(f, GeometryField)][0] | |
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 | |
ds = mapnik.PostGIS(**params) | |
ds.srs = SpatialReference(geom_field.srid).proj4 | |
ds.geom_type = geom_field.geom_type.lower() | |
return ds | |
# helper to convert a queryset's sql (to-be) into a mapnik datasource SQL query for the 'table' param | |
def get_mapnik_sql(qs): | |
cols, sql, args = qs._get_sql_clause() | |
raw_sql = "(SELECT %s %s) as django_table" % (', '.join(cols), | |
sql % tuple(args)) | |
unquoted_sql = raw_sql.replace('"', '') | |
return unquoted_sql |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment