Created
July 13, 2015 09:48
-
-
Save lordi/d7c2969779500cc48b84 to your computer and use it in GitHub Desktop.
Flexible SRID/SRS GeometryField
This file contains 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
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