2009-12-22 23:18:51 +08:00
|
|
|
from django.db.models.fields import Field
|
2010-01-19 05:49:13 +08:00
|
|
|
from django.db.models.sql.expressions import SQLEvaluator
|
2009-12-17 02:13:34 +08:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2008-08-06 02:13:06 +08:00
|
|
|
from django.contrib.gis import forms
|
|
|
|
from django.contrib.gis.db.models.proxy import GeometryProxy
|
2009-12-22 23:18:51 +08:00
|
|
|
from django.contrib.gis.geometry.backend import Geometry, GeometryException
|
2009-03-31 01:15:49 +08:00
|
|
|
|
2010-01-19 05:49:13 +08:00
|
|
|
# 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 = {}
|
2009-12-22 23:18:51 +08:00
|
|
|
|
|
|
|
def get_srid_info(srid, connection):
|
2009-04-11 02:08:37 +08:00
|
|
|
"""
|
|
|
|
Returns the units, unit name, and spheroid WKT associated with the
|
|
|
|
given SRID from the `spatial_ref_sys` (or equivalent) spatial database
|
2010-01-19 05:49:13 +08:00
|
|
|
table for the given database connection. These results are cached.
|
2009-04-11 02:08:37 +08:00
|
|
|
"""
|
|
|
|
global _srid_cache
|
|
|
|
|
2010-01-19 05:49:13 +08:00
|
|
|
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).
|
2009-04-11 02:08:37 +08:00
|
|
|
return None, None, None
|
|
|
|
|
2010-01-19 05:49:13 +08:00
|
|
|
if not connection.alias in _srid_cache:
|
|
|
|
# Initialize SRID dictionary for database if it doesn't exist.
|
|
|
|
_srid_cache[connection.alias] = {}
|
|
|
|
|
|
|
|
if not srid in _srid_cache[connection.alias]:
|
|
|
|
# Use `SpatialRefSys` model to query for spatial reference info.
|
|
|
|
sr = SpatialRefSys.objects.using(connection.alias).get(srid=srid)
|
2009-04-11 02:08:37 +08:00
|
|
|
units, units_name = sr.units
|
|
|
|
spheroid = SpatialRefSys.get_spheroid(sr.wkt)
|
2010-01-19 05:49:13 +08:00
|
|
|
_srid_cache[connection.alias][srid] = (units, units_name, spheroid)
|
2009-04-11 02:08:37 +08:00
|
|
|
|
2010-01-19 05:49:13 +08:00
|
|
|
return _srid_cache[connection.alias][srid]
|
2009-04-11 01:04:35 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
class GeometryField(Field):
|
2009-12-17 02:13:34 +08:00
|
|
|
"The base GIS field -- maps to the OpenGIS Specification Geometry type."
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
# The OpenGIS Geometry name.
|
2009-03-31 01:15:49 +08:00
|
|
|
geom_type = 'GEOMETRY'
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
# Geodetic units.
|
|
|
|
geodetic_units = ('Decimal Degree', 'degree')
|
|
|
|
|
2009-12-17 02:13:34 +08:00
|
|
|
description = _("The base GIS field -- maps to the OpenGIS Specification Geometry type.")
|
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def __init__(self, verbose_name=None, srid=4326, spatial_index=True, dim=2,
|
|
|
|
geography=False, **kwargs):
|
2008-08-06 02:13:06 +08:00
|
|
|
"""
|
|
|
|
The initialization function for geometry fields. Takes the following
|
|
|
|
as keyword arguments:
|
|
|
|
|
|
|
|
srid:
|
|
|
|
The spatial reference system identifier, an OGC standard.
|
|
|
|
Defaults to 4326 (WGS84).
|
|
|
|
|
|
|
|
spatial_index:
|
|
|
|
Indicates whether to create a spatial index. Defaults to True.
|
|
|
|
Set this instead of 'db_index' for geographic fields since index
|
|
|
|
creation is different for geometry columns.
|
2008-08-26 12:55:56 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
dim:
|
|
|
|
The number of dimensions for this geometry. Defaults to 2.
|
2009-12-22 23:18:51 +08:00
|
|
|
|
|
|
|
extent:
|
|
|
|
Customize the extent, in a 4-tuple of WGS 84 coordinates, for the
|
|
|
|
geometry field entry in the `USER_SDO_GEOM_METADATA` table. Defaults
|
|
|
|
to (-180.0, -90.0, 180.0, 90.0).
|
|
|
|
|
|
|
|
tolerance:
|
|
|
|
Define the tolerance, in meters, to use for the geometry field
|
|
|
|
entry in the `USER_SDO_GEOM_METADATA` table. Defaults to 0.05.
|
2008-08-06 02:13:06 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
# Setting the index flag with the value of the `spatial_index` keyword.
|
2009-03-31 01:15:49 +08:00
|
|
|
self.spatial_index = spatial_index
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2008-08-26 12:55:56 +08:00
|
|
|
# Setting the SRID and getting the units. Unit information must be
|
2008-08-06 02:13:06 +08:00
|
|
|
# easily available in the field instance for distance queries.
|
2009-03-31 01:15:49 +08:00
|
|
|
self.srid = srid
|
2009-04-11 00:58:29 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
# Setting the dimension of the geometry field.
|
2009-03-31 01:15:49 +08:00
|
|
|
self.dim = dim
|
2008-08-26 12:55:56 +08:00
|
|
|
|
|
|
|
# Setting the verbose_name keyword argument with the positional
|
2008-08-07 05:40:00 +08:00
|
|
|
# first parameter, so this works like normal fields.
|
|
|
|
kwargs['verbose_name'] = verbose_name
|
2008-08-26 12:55:56 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
# Is this a geography rather than a geometry column?
|
|
|
|
self.geography = geography
|
|
|
|
|
|
|
|
# Oracle-specific private attributes for creating the entrie in
|
|
|
|
# `USER_SDO_GEOM_METADATA`
|
|
|
|
self._extent = kwargs.pop('extent', (-180.0, -90.0, 180.0, 90.0))
|
|
|
|
self._tolerance = kwargs.pop('tolerance', 0.05)
|
|
|
|
|
|
|
|
super(GeometryField, self).__init__(**kwargs)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
# The following functions are used to get the units, their name, and
|
2009-04-11 02:08:37 +08:00
|
|
|
# the spheroid corresponding to the SRID of the GeometryField.
|
2009-12-22 23:18:51 +08:00
|
|
|
def _get_srid_info(self, connection):
|
2009-04-11 02:08:37 +08:00
|
|
|
# Get attributes from `get_srid_info`.
|
2009-12-22 23:18:51 +08:00
|
|
|
self._units, self._units_name, self._spheroid = get_srid_info(self.srid, connection)
|
2009-04-11 02:08:37 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def spheroid(self, connection):
|
2009-04-11 02:08:37 +08:00
|
|
|
if not hasattr(self, '_spheroid'):
|
2009-12-22 23:18:51 +08:00
|
|
|
self._get_srid_info(connection)
|
2009-04-11 02:08:37 +08:00
|
|
|
return self._spheroid
|
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def units(self, connection):
|
2009-04-11 02:08:37 +08:00
|
|
|
if not hasattr(self, '_units'):
|
2009-12-22 23:18:51 +08:00
|
|
|
self._get_srid_info(connection)
|
2009-04-11 02:08:37 +08:00
|
|
|
return self._units
|
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def units_name(self, connection):
|
2009-04-11 02:08:37 +08:00
|
|
|
if not hasattr(self, '_units_name'):
|
2009-12-22 23:18:51 +08:00
|
|
|
self._get_srid_info(connection)
|
2009-04-11 02:08:37 +08:00
|
|
|
return self._units_name
|
2009-04-11 00:58:29 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
### Routines specific to GeometryField ###
|
2009-12-22 23:18:51 +08:00
|
|
|
def geodetic(self, connection):
|
2008-08-06 02:13:06 +08:00
|
|
|
"""
|
|
|
|
Returns true if this field's SRID corresponds with a coordinate
|
|
|
|
system that uses non-projected units (e.g., latitude/longitude).
|
|
|
|
"""
|
2009-12-22 23:18:51 +08:00
|
|
|
return self.units_name(connection) in self.geodetic_units
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def get_distance(self, value, lookup_type, connection):
|
2008-08-06 02:13:06 +08:00
|
|
|
"""
|
2008-08-26 12:55:56 +08:00
|
|
|
Returns a distance number in units of the field. For example, if
|
2008-08-06 02:13:06 +08:00
|
|
|
`D(km=1)` was passed in and the units of the field were in meters,
|
|
|
|
then 1000 would be returned.
|
|
|
|
"""
|
2009-12-22 23:18:51 +08:00
|
|
|
return connection.ops.get_distance(self, value, lookup_type)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def get_prep_value(self, value):
|
2008-08-06 02:13:06 +08:00
|
|
|
"""
|
2009-12-22 23:18:51 +08:00
|
|
|
Spatial lookup values are either a parameter that is (or may be
|
|
|
|
converted to) a geometry, or a sequence of lookup values that
|
|
|
|
begins with a geometry. This routine will setup the geometry
|
|
|
|
value properly, and preserve any other lookup parameters before
|
|
|
|
returning to the caller.
|
2008-08-06 02:13:06 +08:00
|
|
|
"""
|
2009-12-22 23:18:51 +08:00
|
|
|
if isinstance(value, SQLEvaluator):
|
|
|
|
return value
|
|
|
|
elif isinstance(value, (tuple, list)):
|
2008-08-06 02:13:06 +08:00
|
|
|
geom = value[0]
|
2009-12-22 23:18:51 +08:00
|
|
|
seq_value = True
|
2008-08-06 02:13:06 +08:00
|
|
|
else:
|
|
|
|
geom = value
|
2009-12-22 23:18:51 +08:00
|
|
|
seq_value = False
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
# When the input is not a GEOS geometry, attempt to construct one
|
|
|
|
# from the given string input.
|
2009-12-22 23:18:51 +08:00
|
|
|
if isinstance(geom, Geometry):
|
2008-08-06 02:13:06 +08:00
|
|
|
pass
|
2009-12-22 23:18:51 +08:00
|
|
|
elif isinstance(geom, basestring) or hasattr(geom, '__geo_interface__'):
|
2008-08-06 02:13:06 +08:00
|
|
|
try:
|
2009-12-22 23:18:51 +08:00
|
|
|
geom = Geometry(geom)
|
|
|
|
except GeometryException:
|
|
|
|
raise ValueError('Could not create geometry from lookup value.')
|
2008-08-06 02:13:06 +08:00
|
|
|
else:
|
2009-12-22 23:18:51 +08:00
|
|
|
raise ValueError('Cannot use parameter of `%s` type as lookup parameter.' % type(value))
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
# Assigning the SRID value.
|
|
|
|
geom.srid = self.get_srid(geom)
|
2008-08-26 12:55:56 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
if seq_value:
|
|
|
|
lookup_val = [geom]
|
|
|
|
lookup_val.extend(value[1:])
|
|
|
|
return tuple(lookup_val)
|
|
|
|
else:
|
|
|
|
return geom
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
def get_srid(self, geom):
|
|
|
|
"""
|
|
|
|
Returns the default SRID for the given geometry, taking into account
|
|
|
|
the SRID set for the field. For example, if the input geometry
|
|
|
|
has no SRID, then that of the field will be returned.
|
|
|
|
"""
|
|
|
|
gsrid = geom.srid # SRID of given geometry.
|
2009-03-31 01:15:49 +08:00
|
|
|
if gsrid is None or self.srid == -1 or (gsrid == -1 and self.srid != -1):
|
|
|
|
return self.srid
|
2008-08-06 02:13:06 +08:00
|
|
|
else:
|
|
|
|
return gsrid
|
|
|
|
|
|
|
|
### Routines overloaded from Field ###
|
|
|
|
def contribute_to_class(self, cls, name):
|
|
|
|
super(GeometryField, self).contribute_to_class(cls, name)
|
2008-08-26 12:55:56 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
# Setup for lazy-instantiated Geometry object.
|
2009-12-22 23:18:51 +08:00
|
|
|
setattr(cls, self.attname, GeometryProxy(Geometry, self))
|
|
|
|
|
|
|
|
def db_type(self, connection):
|
|
|
|
return connection.ops.geo_db_type(self)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
def formfield(self, **kwargs):
|
2008-08-26 12:55:56 +08:00
|
|
|
defaults = {'form_class' : forms.GeometryField,
|
2008-08-06 02:13:06 +08:00
|
|
|
'null' : self.null,
|
2009-03-31 01:15:49 +08:00
|
|
|
'geom_type' : self.geom_type,
|
|
|
|
'srid' : self.srid,
|
2008-08-06 02:13:06 +08:00
|
|
|
}
|
|
|
|
defaults.update(kwargs)
|
|
|
|
return super(GeometryField, self).formfield(**defaults)
|
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def get_db_prep_lookup(self, lookup_type, value, connection, prepared=False):
|
2008-08-06 02:13:06 +08:00
|
|
|
"""
|
2009-12-22 23:18:51 +08:00
|
|
|
Prepare for the database lookup, and return any spatial parameters
|
|
|
|
necessary for the query. This includes wrapping any geometry
|
|
|
|
parameters with a backend-specific adapter and formatting any distance
|
|
|
|
parameters into the correct units for the coordinate system of the
|
|
|
|
field.
|
2008-08-06 02:13:06 +08:00
|
|
|
"""
|
2009-12-22 23:18:51 +08:00
|
|
|
if lookup_type in connection.ops.gis_terms:
|
2008-08-06 02:13:06 +08:00
|
|
|
# special case for isnull lookup
|
2009-12-22 23:18:51 +08:00
|
|
|
if lookup_type == 'isnull':
|
|
|
|
return []
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
# Populating the parameters list, and wrapping the Geometry
|
|
|
|
# with the Adapter of the spatial backend.
|
2008-08-06 02:13:06 +08:00
|
|
|
if isinstance(value, (tuple, list)):
|
2009-12-22 23:18:51 +08:00
|
|
|
params = [connection.ops.Adapter(value[0])]
|
|
|
|
if lookup_type in connection.ops.distance_functions:
|
2008-08-06 02:13:06 +08:00
|
|
|
# Getting the distance parameter in the units of the field.
|
2009-12-22 23:18:51 +08:00
|
|
|
params += self.get_distance(value[1:], lookup_type, connection)
|
2008-08-06 02:13:06 +08:00
|
|
|
else:
|
2009-12-22 23:18:51 +08:00
|
|
|
params += value[1:]
|
|
|
|
elif isinstance(value, SQLEvaluator):
|
|
|
|
params = []
|
|
|
|
else:
|
|
|
|
params = [connection.ops.Adapter(value)]
|
|
|
|
|
|
|
|
return params
|
2008-08-06 02:13:06 +08:00
|
|
|
else:
|
|
|
|
raise TypeError("Field has invalid lookup: %s" % lookup_type)
|
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def get_prep_lookup(self, lookup_type, value):
|
|
|
|
if lookup_type == 'isnull':
|
|
|
|
return bool(value)
|
|
|
|
else:
|
|
|
|
return self.get_prep_value(value)
|
|
|
|
|
|
|
|
def get_db_prep_save(self, value, connection):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Prepares the value for saving in the database."
|
|
|
|
if value is None:
|
|
|
|
return None
|
|
|
|
else:
|
2009-12-22 23:18:51 +08:00
|
|
|
return connection.ops.Adapter(self.get_prep_value(value))
|
|
|
|
|
|
|
|
def get_placeholder(self, value, connection):
|
|
|
|
"""
|
|
|
|
Returns the placeholder for the geometry column for the
|
|
|
|
given value.
|
|
|
|
"""
|
|
|
|
return connection.ops.get_geom_placeholder(self, value)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
# The OpenGIS Geometry Type Fields
|
|
|
|
class PointField(GeometryField):
|
2009-03-31 01:15:49 +08:00
|
|
|
geom_type = 'POINT'
|
2009-12-17 02:13:34 +08:00
|
|
|
description = _("Point")
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
class LineStringField(GeometryField):
|
2009-03-31 01:15:49 +08:00
|
|
|
geom_type = 'LINESTRING'
|
2009-12-17 02:13:34 +08:00
|
|
|
description = _("Line string")
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
class PolygonField(GeometryField):
|
2009-03-31 01:15:49 +08:00
|
|
|
geom_type = 'POLYGON'
|
2009-12-17 02:13:34 +08:00
|
|
|
description = _("Polygon")
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
class MultiPointField(GeometryField):
|
2009-03-31 01:15:49 +08:00
|
|
|
geom_type = 'MULTIPOINT'
|
2009-12-17 02:13:34 +08:00
|
|
|
description = _("Multi-point")
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
class MultiLineStringField(GeometryField):
|
2009-03-31 01:15:49 +08:00
|
|
|
geom_type = 'MULTILINESTRING'
|
2009-12-17 02:13:34 +08:00
|
|
|
description = _("Multi-line string")
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
class MultiPolygonField(GeometryField):
|
2009-03-31 01:15:49 +08:00
|
|
|
geom_type = 'MULTIPOLYGON'
|
2009-12-17 02:13:34 +08:00
|
|
|
description = _("Multi polygon")
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
class GeometryCollectionField(GeometryField):
|
2009-03-31 01:15:49 +08:00
|
|
|
geom_type = 'GEOMETRYCOLLECTION'
|
2009-12-17 02:13:34 +08:00
|
|
|
description = _("Geometry collection")
|