from django.contrib.gis import forms from django.contrib.gis.db.models.lookups import gis_lookups from django.contrib.gis.db.models.proxy import SpatialProxy from django.contrib.gis.gdal import HAS_GDAL from django.contrib.gis.geometry.backend import Geometry, GeometryException from django.core.exceptions import ImproperlyConfigured from django.db.models.expressions import Expression from django.db.models.fields import Field from django.utils import six from django.utils.translation import ugettext_lazy as _ # Local cache of the spatial_ref_sys table, which holds SRID data for each # spatial database alias. This cache exists so that the database isn't queried # for SRID info each time a distance query is constructed. _srid_cache = {} def get_srid_info(srid, connection): """ Returns the units, unit name, and spheroid WKT associated with the given SRID from the `spatial_ref_sys` (or equivalent) spatial database table for the given database connection. These results are cached. """ global _srid_cache try: # The SpatialRefSys model for the spatial backend. SpatialRefSys = connection.ops.spatial_ref_sys() except NotImplementedError: # No `spatial_ref_sys` table in spatial backend (e.g., MySQL). return None, None, None if connection.alias not in _srid_cache: # Initialize SRID dictionary for database if it doesn't exist.
params = [connection.ops.Adapter(value[0])] # Getting the distance parameter in the units of the field. params += self.get_distance(value[1:], lookup_type, connection) else: params = [connection.ops.Adapter(value)] return params def get_db_prep_save(self, value, connection): "Prepares the value for saving in the database." if not value: return None else: return connection.ops.Adapter(self.get_prep_value(value)) for klass in gis_lookups.values(): GeometryField.register_lookup(klass) # The OpenGIS Geometry Type Fields class PointField(GeometryField): geom_type = 'POINT' form_class = forms.PointField description = _("Point") class LineStringField(GeometryField): geom_type = 'LINESTRING' form_class = forms.LineStringField description = _("Line string")
raise ValueError( 'Cannot use object with type %s for a spatial lookup parameter.' % type(obj).__name__) # Assigning the SRID value. obj.srid = self.get_srid(obj) if seq_value: lookup_val = [obj] lookup_val.extend(value[1:]) return tuple(lookup_val) else: return obj for klass in gis_lookups.values(): BaseSpatialField.register_lookup(klass) class GeometryField(GeoSelectFormatMixin, BaseSpatialField): """ The base Geometry field -- maps to the OpenGIS Specification Geometry type. """ description = _( "The base Geometry field -- maps to the OpenGIS Specification Geometry type." ) form_class = forms.GeometryField # The OpenGIS Geometry name. geom_type = 'GEOMETRY' def __init__(self, verbose_name=None, dim=2, geography=False, **kwargs):