Skip to content

Instantly share code, notes, and snippets.

@lordi
Created July 13, 2015 09:48
Show Gist options
  • Save lordi/d7c2969779500cc48b84 to your computer and use it in GitHub Desktop.
Save lordi/d7c2969779500cc48b84 to your computer and use it in GitHub Desktop.
Flexible SRID/SRS GeometryField
from django.contrib.gis.db import models
class FlexibleSRSGeometryField(models.GeometryField):
"""
GeoDjango field that allows storing of geometries with different SRIDs.
"""
def __init__(self, *args, **kwargs):
kwargs['srid'] = -1
super(FlexibleSRSGeometryField, self).__init__(*args, **kwargs)
def get_placeholder(self, value, compiler, connection):
"""
Returns the placeholder for the geometry column for the
given value.
"""
if value is None or self.srid == -1:
return '%s'
else:
return connection.ops.get_geom_placeholder(self, value, compiler)
def get_srid(self, geom):
"Gets the SRID for the geometry, returns None if no SRID is set."
if self.srid == -1:
return geom.srid
else:
return super(FlexibleSRSGeometryField, self).get_srid(geom)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment