2015-01-15 03:48:55 +08:00
|
|
|
import warnings
|
|
|
|
|
2014-12-01 15:28:01 +08:00
|
|
|
from django.contrib.gis.db.models.fields import (
|
2015-01-28 20:35:27 +08:00
|
|
|
GeometryField, LineStringField, PointField, get_srid_info,
|
2014-12-01 15:28:01 +08:00
|
|
|
)
|
|
|
|
from django.contrib.gis.db.models.lookups import GISLookup
|
|
|
|
from django.contrib.gis.db.models.sql import (
|
|
|
|
AreaField, DistanceField, GeomField, GMLField,
|
|
|
|
)
|
2009-12-22 23:18:51 +08:00
|
|
|
from django.contrib.gis.geometry.backend import Geometry
|
2008-08-06 02:13:06 +08:00
|
|
|
from django.contrib.gis.measure import Area, Distance
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.db import connections
|
2015-10-10 09:03:04 +08:00
|
|
|
from django.db.models.constants import LOOKUP_SEP
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.db.models.expressions import RawSQL
|
|
|
|
from django.db.models.fields import Field
|
|
|
|
from django.db.models.query import QuerySet
|
2012-07-20 20:22:00 +08:00
|
|
|
from django.utils import six
|
2015-09-03 07:55:55 +08:00
|
|
|
from django.utils.deprecation import RemovedInDjango20Warning
|
2012-07-20 20:22:00 +08:00
|
|
|
|
2012-08-23 16:16:16 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
class GeoQuerySet(QuerySet):
|
|
|
|
"The Geographic QuerySet."
|
|
|
|
|
2015-02-06 02:25:34 +08:00
|
|
|
# ### GeoQuerySet Methods ###
|
2008-08-06 02:13:06 +08:00
|
|
|
def area(self, tolerance=0.05, **kwargs):
|
|
|
|
"""
|
2009-01-15 19:06:34 +08:00
|
|
|
Returns the area of the geographic field in an `area` attribute on
|
2008-08-06 02:13:06 +08:00
|
|
|
each element of this GeoQuerySet.
|
|
|
|
"""
|
2014-04-27 01:18:45 +08:00
|
|
|
# Performing setup here rather than in `_spatial_attribute` so that
|
2008-08-06 02:13:06 +08:00
|
|
|
# we can get the units for `AreaField`.
|
2014-12-01 15:28:01 +08:00
|
|
|
procedure_args, geo_field = self._spatial_setup(
|
2015-05-14 02:51:18 +08:00
|
|
|
'area', field_name=kwargs.get('field_name'))
|
2013-10-27 09:27:42 +08:00
|
|
|
s = {'procedure_args': procedure_args,
|
|
|
|
'geo_field': geo_field,
|
|
|
|
'setup': False,
|
2008-08-06 02:13:06 +08:00
|
|
|
}
|
2009-12-22 23:18:51 +08:00
|
|
|
connection = connections[self.db]
|
|
|
|
backend = connection.ops
|
|
|
|
if backend.oracle:
|
2008-08-06 02:13:06 +08:00
|
|
|
s['procedure_fmt'] = '%(geo_col)s,%(tolerance)s'
|
|
|
|
s['procedure_args']['tolerance'] = tolerance
|
2013-11-03 05:02:56 +08:00
|
|
|
s['select_field'] = AreaField('sq_m') # Oracle returns area in units of meters.
|
2009-12-22 23:18:51 +08:00
|
|
|
elif backend.postgis or backend.spatialite:
|
2010-10-13 01:13:27 +08:00
|
|
|
if backend.geography:
|
|
|
|
# Geography fields support area calculation, returns square meters.
|
|
|
|
s['select_field'] = AreaField('sq_m')
|
|
|
|
elif not geo_field.geodetic(connection):
|
2008-08-06 02:13:06 +08:00
|
|
|
# Getting the area units of the geographic field.
|
2009-12-22 23:18:51 +08:00
|
|
|
s['select_field'] = AreaField(Area.unit_attname(geo_field.units_name(connection)))
|
2008-08-06 02:13:06 +08:00
|
|
|
else:
|
|
|
|
# TODO: Do we want to support raw number areas for geodetic fields?
|
|
|
|
raise Exception('Area on geodetic coordinate systems not supported.')
|
|
|
|
return self._spatial_attribute('area', s, **kwargs)
|
|
|
|
|
|
|
|
def centroid(self, **kwargs):
|
|
|
|
"""
|
|
|
|
Returns the centroid of the geographic field in a `centroid`
|
|
|
|
attribute on each element of this GeoQuerySet.
|
|
|
|
"""
|
|
|
|
return self._geom_attribute('centroid', **kwargs)
|
|
|
|
|
|
|
|
def difference(self, geom, **kwargs):
|
|
|
|
"""
|
|
|
|
Returns the spatial difference of the geographic field in a `difference`
|
|
|
|
attribute on each element of this GeoQuerySet.
|
|
|
|
"""
|
|
|
|
return self._geomset_attribute('difference', geom, **kwargs)
|
|
|
|
|
|
|
|
def distance(self, geom, **kwargs):
|
|
|
|
"""
|
|
|
|
Returns the distance from the given geographic field name to the
|
|
|
|
given geometry in a `distance` attribute on each element of the
|
|
|
|
GeoQuerySet.
|
|
|
|
|
|
|
|
Keyword Arguments:
|
|
|
|
`spheroid` => If the geometry field is geodetic and PostGIS is
|
2009-01-15 19:06:34 +08:00
|
|
|
the spatial database, then the more accurate
|
2008-08-06 02:13:06 +08:00
|
|
|
spheroid calculation will be used instead of the
|
|
|
|
quicker sphere calculation.
|
2009-01-15 19:06:34 +08:00
|
|
|
|
|
|
|
`tolerance` => Used only for Oracle. The tolerance is
|
|
|
|
in meters -- a default of 5 centimeters (0.05)
|
2008-08-06 02:13:06 +08:00
|
|
|
is used.
|
|
|
|
"""
|
|
|
|
return self._distance_attribute('distance', geom, **kwargs)
|
|
|
|
|
|
|
|
def envelope(self, **kwargs):
|
|
|
|
"""
|
2009-01-15 19:06:34 +08:00
|
|
|
Returns a Geometry representing the bounding box of the
|
2008-08-06 02:13:06 +08:00
|
|
|
Geometry field in an `envelope` attribute on each element of
|
2009-01-15 19:06:34 +08:00
|
|
|
the GeoQuerySet.
|
2008-08-06 02:13:06 +08:00
|
|
|
"""
|
|
|
|
return self._geom_attribute('envelope', **kwargs)
|
|
|
|
|
2010-01-29 10:20:58 +08:00
|
|
|
def force_rhr(self, **kwargs):
|
|
|
|
"""
|
|
|
|
Returns a modified version of the Polygon/MultiPolygon in which
|
|
|
|
all of the vertices follow the Right-Hand-Rule. By default,
|
|
|
|
this is attached as the `force_rhr` attribute on each element
|
|
|
|
of the GeoQuerySet.
|
|
|
|
"""
|
|
|
|
return self._geom_attribute('force_rhr', **kwargs)
|
|
|
|
|
2009-04-03 00:50:44 +08:00
|
|
|
def geojson(self, precision=8, crs=False, bbox=False, **kwargs):
|
|
|
|
"""
|
2014-03-02 22:25:53 +08:00
|
|
|
Returns a GeoJSON representation of the geometry field in a `geojson`
|
2009-04-03 00:50:44 +08:00
|
|
|
attribute on each element of the GeoQuerySet.
|
|
|
|
|
2013-07-22 22:32:09 +08:00
|
|
|
The `crs` and `bbox` keywords may be set to True if the user wants
|
2009-04-03 00:50:44 +08:00
|
|
|
the coordinate reference system and the bounding box to be included
|
|
|
|
in the GeoJSON representation of the geometry.
|
|
|
|
"""
|
2009-12-22 23:18:51 +08:00
|
|
|
backend = connections[self.db].ops
|
|
|
|
if not backend.geojson:
|
2015-10-31 03:04:00 +08:00
|
|
|
raise NotImplementedError('Only PostGIS and SpatiaLite support GeoJSON serialization.')
|
2009-12-22 23:18:51 +08:00
|
|
|
|
2012-07-20 18:45:19 +08:00
|
|
|
if not isinstance(precision, six.integer_types):
|
2009-04-03 00:50:44 +08:00
|
|
|
raise TypeError('Precision keyword must be set with an integer.')
|
2009-12-22 23:18:51 +08:00
|
|
|
|
2014-07-27 04:55:31 +08:00
|
|
|
options = 0
|
|
|
|
if crs and bbox:
|
|
|
|
options = 3
|
|
|
|
elif bbox:
|
|
|
|
options = 1
|
|
|
|
elif crs:
|
|
|
|
options = 2
|
2013-10-27 09:27:42 +08:00
|
|
|
s = {'desc': 'GeoJSON',
|
|
|
|
'procedure_args': {'precision': precision, 'options': options},
|
|
|
|
'procedure_fmt': '%(geo_col)s,%(precision)s,%(options)s',
|
2009-04-03 00:50:44 +08:00
|
|
|
}
|
|
|
|
return self._spatial_attribute('geojson', s, **kwargs)
|
|
|
|
|
2010-01-29 10:46:07 +08:00
|
|
|
def geohash(self, precision=20, **kwargs):
|
|
|
|
"""
|
|
|
|
Returns a GeoHash representation of the given field in a `geohash`
|
|
|
|
attribute on each element of the GeoQuerySet.
|
|
|
|
|
|
|
|
The `precision` keyword may be used to custom the number of
|
|
|
|
_characters_ used in the output GeoHash, the default is 20.
|
|
|
|
"""
|
2013-10-27 09:27:42 +08:00
|
|
|
s = {'desc': 'GeoHash',
|
2010-01-29 10:46:07 +08:00
|
|
|
'procedure_args': {'precision': precision},
|
|
|
|
'procedure_fmt': '%(geo_col)s,%(precision)s',
|
|
|
|
}
|
|
|
|
return self._spatial_attribute('geohash', s, **kwargs)
|
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
def gml(self, precision=8, version=2, **kwargs):
|
|
|
|
"""
|
|
|
|
Returns GML representation of the given field in a `gml` attribute
|
|
|
|
on each element of the GeoQuerySet.
|
|
|
|
"""
|
2009-12-22 23:18:51 +08:00
|
|
|
backend = connections[self.db].ops
|
2013-10-27 09:27:42 +08:00
|
|
|
s = {'desc': 'GML', 'procedure_args': {'precision': precision}}
|
2009-12-22 23:18:51 +08:00
|
|
|
if backend.postgis:
|
2014-07-27 04:55:31 +08:00
|
|
|
s['procedure_fmt'] = '%(version)s,%(geo_col)s,%(precision)s'
|
2013-10-27 09:27:42 +08:00
|
|
|
s['procedure_args'] = {'precision': precision, 'version': version}
|
2014-09-21 15:51:59 +08:00
|
|
|
if backend.oracle:
|
|
|
|
s['select_field'] = GMLField()
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
return self._spatial_attribute('gml', s, **kwargs)
|
|
|
|
|
|
|
|
def intersection(self, geom, **kwargs):
|
|
|
|
"""
|
|
|
|
Returns the spatial intersection of the Geometry field in
|
|
|
|
an `intersection` attribute on each element of this
|
|
|
|
GeoQuerySet.
|
|
|
|
"""
|
|
|
|
return self._geomset_attribute('intersection', geom, **kwargs)
|
|
|
|
|
|
|
|
def kml(self, **kwargs):
|
|
|
|
"""
|
|
|
|
Returns KML representation of the geometry field in a `kml`
|
|
|
|
attribute on each element of this GeoQuerySet.
|
|
|
|
"""
|
2013-10-27 09:27:42 +08:00
|
|
|
s = {'desc': 'KML',
|
|
|
|
'procedure_fmt': '%(geo_col)s,%(precision)s',
|
|
|
|
'procedure_args': {'precision': kwargs.pop('precision', 8)},
|
2008-08-06 02:13:06 +08:00
|
|
|
}
|
|
|
|
return self._spatial_attribute('kml', s, **kwargs)
|
|
|
|
|
|
|
|
def length(self, **kwargs):
|
|
|
|
"""
|
|
|
|
Returns the length of the geometry field as a `Distance` object
|
|
|
|
stored in a `length` attribute on each element of this GeoQuerySet.
|
|
|
|
"""
|
|
|
|
return self._distance_attribute('length', None, **kwargs)
|
|
|
|
|
|
|
|
def mem_size(self, **kwargs):
|
|
|
|
"""
|
|
|
|
Returns the memory size (number of bytes) that the geometry field takes
|
|
|
|
in a `mem_size` attribute on each element of this GeoQuerySet.
|
|
|
|
"""
|
|
|
|
return self._spatial_attribute('mem_size', {}, **kwargs)
|
|
|
|
|
|
|
|
def num_geom(self, **kwargs):
|
|
|
|
"""
|
|
|
|
Returns the number of geometries if the field is a
|
|
|
|
GeometryCollection or Multi* Field in a `num_geom`
|
|
|
|
attribute on each element of this GeoQuerySet; otherwise
|
|
|
|
the sets with None.
|
|
|
|
"""
|
|
|
|
return self._spatial_attribute('num_geom', {}, **kwargs)
|
|
|
|
|
|
|
|
def num_points(self, **kwargs):
|
|
|
|
"""
|
2009-01-15 19:06:34 +08:00
|
|
|
Returns the number of points in the first linestring in the
|
2008-08-06 02:13:06 +08:00
|
|
|
Geometry field in a `num_points` attribute on each element of
|
|
|
|
this GeoQuerySet; otherwise sets with None.
|
|
|
|
"""
|
|
|
|
return self._spatial_attribute('num_points', {}, **kwargs)
|
|
|
|
|
|
|
|
def perimeter(self, **kwargs):
|
|
|
|
"""
|
|
|
|
Returns the perimeter of the geometry field as a `Distance` object
|
|
|
|
stored in a `perimeter` attribute on each element of this GeoQuerySet.
|
|
|
|
"""
|
|
|
|
return self._distance_attribute('perimeter', None, **kwargs)
|
|
|
|
|
|
|
|
def point_on_surface(self, **kwargs):
|
|
|
|
"""
|
|
|
|
Returns a Point geometry guaranteed to lie on the surface of the
|
|
|
|
Geometry field in a `point_on_surface` attribute on each element
|
|
|
|
of this GeoQuerySet; otherwise sets with None.
|
|
|
|
"""
|
|
|
|
return self._geom_attribute('point_on_surface', **kwargs)
|
|
|
|
|
2010-01-29 10:46:07 +08:00
|
|
|
def reverse_geom(self, **kwargs):
|
2010-01-29 10:20:58 +08:00
|
|
|
"""
|
|
|
|
Reverses the coordinate order of the geometry, and attaches as a
|
|
|
|
`reverse` attribute on each element of this GeoQuerySet.
|
|
|
|
"""
|
2013-10-25 01:30:03 +08:00
|
|
|
s = {'select_field': GeomField()}
|
2010-01-29 10:46:07 +08:00
|
|
|
kwargs.setdefault('model_att', 'reverse_geom')
|
2010-01-29 10:20:58 +08:00
|
|
|
if connections[self.db].ops.oracle:
|
|
|
|
s['geo_field_type'] = LineStringField
|
|
|
|
return self._spatial_attribute('reverse', s, **kwargs)
|
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
def scale(self, x, y, z=0.0, **kwargs):
|
|
|
|
"""
|
|
|
|
Scales the geometry to a new size by multiplying the ordinates
|
|
|
|
with the given x,y,z scale factors.
|
|
|
|
"""
|
2009-12-22 23:18:51 +08:00
|
|
|
if connections[self.db].ops.spatialite:
|
2009-03-31 01:15:49 +08:00
|
|
|
if z != 0.0:
|
|
|
|
raise NotImplementedError('SpatiaLite does not support 3D scaling.')
|
2013-10-27 09:27:42 +08:00
|
|
|
s = {'procedure_fmt': '%(geo_col)s,%(x)s,%(y)s',
|
|
|
|
'procedure_args': {'x': x, 'y': y},
|
|
|
|
'select_field': GeomField(),
|
2009-03-31 01:15:49 +08:00
|
|
|
}
|
|
|
|
else:
|
2013-10-27 09:27:42 +08:00
|
|
|
s = {'procedure_fmt': '%(geo_col)s,%(x)s,%(y)s,%(z)s',
|
|
|
|
'procedure_args': {'x': x, 'y': y, 'z': z},
|
|
|
|
'select_field': GeomField(),
|
2009-03-31 01:15:49 +08:00
|
|
|
}
|
2008-08-06 02:13:06 +08:00
|
|
|
return self._spatial_attribute('scale', s, **kwargs)
|
|
|
|
|
2009-04-03 00:50:44 +08:00
|
|
|
def snap_to_grid(self, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
Snap all points of the input geometry to the grid. How the
|
|
|
|
geometry is snapped to the grid depends on how many arguments
|
|
|
|
were given:
|
|
|
|
- 1 argument : A single size to snap both the X and Y grids to.
|
|
|
|
- 2 arguments: X and Y sizes to snap the grid to.
|
|
|
|
- 4 arguments: X, Y sizes and the X, Y origins.
|
|
|
|
"""
|
2012-07-20 18:45:19 +08:00
|
|
|
if False in [isinstance(arg, (float,) + six.integer_types) for arg in args]:
|
2009-04-03 00:50:44 +08:00
|
|
|
raise TypeError('Size argument(s) for the grid must be a float or integer values.')
|
|
|
|
|
|
|
|
nargs = len(args)
|
|
|
|
if nargs == 1:
|
|
|
|
size = args[0]
|
|
|
|
procedure_fmt = '%(geo_col)s,%(size)s'
|
2013-10-27 09:27:42 +08:00
|
|
|
procedure_args = {'size': size}
|
2009-04-03 00:50:44 +08:00
|
|
|
elif nargs == 2:
|
|
|
|
xsize, ysize = args
|
|
|
|
procedure_fmt = '%(geo_col)s,%(xsize)s,%(ysize)s'
|
2013-10-27 09:27:42 +08:00
|
|
|
procedure_args = {'xsize': xsize, 'ysize': ysize}
|
2009-04-03 00:50:44 +08:00
|
|
|
elif nargs == 4:
|
|
|
|
xsize, ysize, xorigin, yorigin = args
|
|
|
|
procedure_fmt = '%(geo_col)s,%(xorigin)s,%(yorigin)s,%(xsize)s,%(ysize)s'
|
2013-10-27 09:27:42 +08:00
|
|
|
procedure_args = {'xsize': xsize, 'ysize': ysize,
|
|
|
|
'xorigin': xorigin, 'yorigin': yorigin}
|
2009-04-03 00:50:44 +08:00
|
|
|
else:
|
|
|
|
raise ValueError('Must provide 1, 2, or 4 arguments to `snap_to_grid`.')
|
|
|
|
|
2013-10-27 09:27:42 +08:00
|
|
|
s = {'procedure_fmt': procedure_fmt,
|
|
|
|
'procedure_args': procedure_args,
|
|
|
|
'select_field': GeomField(),
|
2009-04-03 00:50:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return self._spatial_attribute('snap_to_grid', s, **kwargs)
|
|
|
|
|
2009-04-08 04:12:08 +08:00
|
|
|
def svg(self, relative=False, precision=8, **kwargs):
|
2008-08-06 02:13:06 +08:00
|
|
|
"""
|
|
|
|
Returns SVG representation of the geographic field in a `svg`
|
|
|
|
attribute on each element of this GeoQuerySet.
|
2009-04-08 04:12:08 +08:00
|
|
|
|
|
|
|
Keyword Arguments:
|
|
|
|
`relative` => If set to True, this will evaluate the path in
|
|
|
|
terms of relative moves (rather than absolute).
|
|
|
|
|
|
|
|
`precision` => May be used to set the maximum number of decimal
|
2009-12-22 23:18:51 +08:00
|
|
|
digits used in output (defaults to 8).
|
2008-08-06 02:13:06 +08:00
|
|
|
"""
|
2009-04-08 04:12:08 +08:00
|
|
|
relative = int(bool(relative))
|
2012-07-20 18:45:19 +08:00
|
|
|
if not isinstance(precision, six.integer_types):
|
2009-04-08 04:12:08 +08:00
|
|
|
raise TypeError('SVG precision keyword argument must be an integer.')
|
2013-12-09 01:20:06 +08:00
|
|
|
s = {
|
|
|
|
'desc': 'SVG',
|
|
|
|
'procedure_fmt': '%(geo_col)s,%(rel)s,%(precision)s',
|
|
|
|
'procedure_args': {
|
|
|
|
'rel': relative,
|
|
|
|
'precision': precision,
|
|
|
|
}
|
|
|
|
}
|
2008-08-06 02:13:06 +08:00
|
|
|
return self._spatial_attribute('svg', s, **kwargs)
|
|
|
|
|
|
|
|
def sym_difference(self, geom, **kwargs):
|
|
|
|
"""
|
2009-01-15 19:06:34 +08:00
|
|
|
Returns the symmetric difference of the geographic field in a
|
2008-08-06 02:13:06 +08:00
|
|
|
`sym_difference` attribute on each element of this GeoQuerySet.
|
|
|
|
"""
|
|
|
|
return self._geomset_attribute('sym_difference', geom, **kwargs)
|
|
|
|
|
|
|
|
def translate(self, x, y, z=0.0, **kwargs):
|
|
|
|
"""
|
|
|
|
Translates the geometry to a new location using the given numeric
|
|
|
|
parameters as offsets.
|
|
|
|
"""
|
2009-12-22 23:18:51 +08:00
|
|
|
if connections[self.db].ops.spatialite:
|
2009-03-31 01:15:49 +08:00
|
|
|
if z != 0.0:
|
|
|
|
raise NotImplementedError('SpatiaLite does not support 3D translation.')
|
2013-10-27 09:27:42 +08:00
|
|
|
s = {'procedure_fmt': '%(geo_col)s,%(x)s,%(y)s',
|
|
|
|
'procedure_args': {'x': x, 'y': y},
|
|
|
|
'select_field': GeomField(),
|
2009-03-31 01:15:49 +08:00
|
|
|
}
|
|
|
|
else:
|
2013-10-27 09:27:42 +08:00
|
|
|
s = {'procedure_fmt': '%(geo_col)s,%(x)s,%(y)s,%(z)s',
|
|
|
|
'procedure_args': {'x': x, 'y': y, 'z': z},
|
|
|
|
'select_field': GeomField(),
|
2009-03-31 01:15:49 +08:00
|
|
|
}
|
2008-08-06 02:13:06 +08:00
|
|
|
return self._spatial_attribute('translate', s, **kwargs)
|
|
|
|
|
|
|
|
def transform(self, srid=4326, **kwargs):
|
|
|
|
"""
|
|
|
|
Transforms the given geometry field to the given SRID. If no SRID is
|
|
|
|
provided, the transformation will default to using 4326 (WGS84).
|
|
|
|
"""
|
2012-07-20 18:45:19 +08:00
|
|
|
if not isinstance(srid, six.integer_types):
|
2008-08-06 02:13:06 +08:00
|
|
|
raise TypeError('An integer SRID must be provided.')
|
2015-05-14 02:51:18 +08:00
|
|
|
field_name = kwargs.get('field_name')
|
2014-12-01 15:28:01 +08:00
|
|
|
self._spatial_setup('transform', field_name=field_name)
|
|
|
|
self.query.add_context('transformed_srid', srid)
|
2008-08-06 02:13:06 +08:00
|
|
|
return self._clone()
|
|
|
|
|
|
|
|
def union(self, geom, **kwargs):
|
|
|
|
"""
|
|
|
|
Returns the union of the geographic field with the given
|
|
|
|
Geometry in a `union` attribute on each element of this GeoQuerySet.
|
|
|
|
"""
|
|
|
|
return self._geomset_attribute('union', geom, **kwargs)
|
|
|
|
|
2015-02-06 02:25:34 +08:00
|
|
|
# ### Private API -- Abstracted DRY routines. ###
|
2009-01-15 19:06:34 +08:00
|
|
|
def _spatial_setup(self, att, desc=None, field_name=None, geo_field_type=None):
|
2008-08-06 02:13:06 +08:00
|
|
|
"""
|
|
|
|
Performs set up for executing the spatial function.
|
|
|
|
"""
|
|
|
|
# Does the spatial backend support this?
|
2009-12-22 23:18:51 +08:00
|
|
|
connection = connections[self.db]
|
|
|
|
func = getattr(connection.ops, att, False)
|
2013-10-17 16:17:41 +08:00
|
|
|
if desc is None:
|
|
|
|
desc = att
|
2009-12-22 23:18:51 +08:00
|
|
|
if not func:
|
|
|
|
raise NotImplementedError('%s stored procedure not available on '
|
|
|
|
'the %s backend.' %
|
|
|
|
(desc, connection.ops.name))
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2009-01-15 19:06:34 +08:00
|
|
|
# Initializing the procedure arguments.
|
2013-10-27 09:27:42 +08:00
|
|
|
procedure_args = {'function': func}
|
2009-01-15 19:06:34 +08:00
|
|
|
|
|
|
|
# Is there a geographic field in the model to perform this
|
2008-08-06 02:13:06 +08:00
|
|
|
# operation on?
|
2014-12-01 15:28:01 +08:00
|
|
|
geo_field = self._geo_field(field_name)
|
2008-08-06 02:13:06 +08:00
|
|
|
if not geo_field:
|
|
|
|
raise TypeError('%s output only available on GeometryFields.' % func)
|
|
|
|
|
2009-01-15 19:06:34 +08:00
|
|
|
# If the `geo_field_type` keyword was used, then enforce that
|
2008-08-06 02:13:06 +08:00
|
|
|
# type limitation.
|
2014-03-31 03:11:05 +08:00
|
|
|
if geo_field_type is not None and not isinstance(geo_field, geo_field_type):
|
2009-01-15 19:06:34 +08:00
|
|
|
raise TypeError('"%s" stored procedures may only be called on %ss.' % (func, geo_field_type.__name__))
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
# Setting the procedure args.
|
2009-01-15 19:06:34 +08:00
|
|
|
procedure_args['geo_col'] = self._geocol_select(geo_field, field_name)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
return procedure_args, geo_field
|
|
|
|
|
|
|
|
def _spatial_attribute(self, att, settings, field_name=None, model_att=None):
|
|
|
|
"""
|
|
|
|
DRY routine for calling a spatial stored procedure on a geometry column
|
|
|
|
and attaching its output as an attribute of the model.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
att:
|
|
|
|
The name of the spatial attribute that holds the spatial
|
|
|
|
SQL function to call.
|
|
|
|
|
|
|
|
settings:
|
2015-12-03 07:55:50 +08:00
|
|
|
Dictionary of internal settings to customize for the spatial procedure.
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
Public Keyword Arguments:
|
|
|
|
|
|
|
|
field_name:
|
|
|
|
The name of the geographic field to call the spatial
|
|
|
|
function on. May also be a lookup to a geometry field
|
|
|
|
as part of a foreign key relation.
|
|
|
|
|
|
|
|
model_att:
|
|
|
|
The name of the model attribute to attach the output of
|
|
|
|
the spatial function to.
|
|
|
|
"""
|
2015-01-30 03:12:08 +08:00
|
|
|
warnings.warn(
|
|
|
|
"The %s GeoQuerySet method is deprecated. See GeoDjango Functions "
|
|
|
|
"documentation to find the expression-based replacement." % att,
|
2015-06-23 01:54:35 +08:00
|
|
|
RemovedInDjango20Warning, stacklevel=2
|
2015-01-30 03:12:08 +08:00
|
|
|
)
|
2008-08-06 02:13:06 +08:00
|
|
|
# Default settings.
|
|
|
|
settings.setdefault('desc', None)
|
|
|
|
settings.setdefault('geom_args', ())
|
|
|
|
settings.setdefault('geom_field', None)
|
|
|
|
settings.setdefault('procedure_args', {})
|
|
|
|
settings.setdefault('procedure_fmt', '%(geo_col)s')
|
|
|
|
settings.setdefault('select_params', [])
|
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
connection = connections[self.db]
|
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
# Performing setup for the spatial column, unless told not to.
|
|
|
|
if settings.get('setup', True):
|
2014-12-01 15:28:01 +08:00
|
|
|
default_args, geo_field = self._spatial_setup(
|
|
|
|
att, desc=settings['desc'], field_name=field_name,
|
2015-05-14 02:51:18 +08:00
|
|
|
geo_field_type=settings.get('geo_field_type'))
|
2013-10-17 16:17:41 +08:00
|
|
|
for k, v in six.iteritems(default_args):
|
|
|
|
settings['procedure_args'].setdefault(k, v)
|
2008-08-06 02:13:06 +08:00
|
|
|
else:
|
|
|
|
geo_field = settings['geo_field']
|
2009-01-15 19:06:34 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
# The attribute to attach to the model.
|
2013-10-17 16:17:41 +08:00
|
|
|
if not isinstance(model_att, six.string_types):
|
|
|
|
model_att = att
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
# Special handling for any argument that is a geometry.
|
|
|
|
for name in settings['geom_args']:
|
2009-12-22 23:18:51 +08:00
|
|
|
# Using the field's get_placeholder() routine to get any needed
|
|
|
|
# transformation SQL.
|
|
|
|
geom = geo_field.get_prep_value(settings['procedure_args'][name])
|
2016-05-02 08:05:19 +08:00
|
|
|
params = geo_field._get_db_prep_lookup('contains', geom, connection=connection)
|
2013-12-25 21:13:18 +08:00
|
|
|
geom_placeholder = geo_field.get_placeholder(geom, None, connection)
|
2009-12-22 23:18:51 +08:00
|
|
|
|
2009-01-15 19:06:34 +08:00
|
|
|
# Replacing the procedure format with that of any needed
|
2008-08-06 02:13:06 +08:00
|
|
|
# transformation SQL.
|
|
|
|
old_fmt = '%%(%s)s' % name
|
2009-12-22 23:18:51 +08:00
|
|
|
new_fmt = geom_placeholder % '%%s'
|
2008-08-06 02:13:06 +08:00
|
|
|
settings['procedure_fmt'] = settings['procedure_fmt'].replace(old_fmt, new_fmt)
|
|
|
|
settings['select_params'].extend(params)
|
|
|
|
|
|
|
|
# Getting the format for the stored procedure.
|
|
|
|
fmt = '%%(function)s(%s)' % settings['procedure_fmt']
|
2009-01-15 19:06:34 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
# If the result of this function needs to be converted.
|
2015-05-14 02:51:18 +08:00
|
|
|
if settings.get('select_field'):
|
2014-12-01 15:28:01 +08:00
|
|
|
select_field = settings['select_field']
|
2009-12-22 23:18:51 +08:00
|
|
|
if connection.ops.oracle:
|
2014-12-01 15:28:01 +08:00
|
|
|
select_field.empty_strings_allowed = False
|
|
|
|
else:
|
|
|
|
select_field = Field()
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2009-01-15 19:06:34 +08:00
|
|
|
# Finally, setting the extra selection attribute with
|
2008-08-06 02:13:06 +08:00
|
|
|
# the format string expanded with the stored procedure
|
|
|
|
# arguments.
|
2014-12-01 15:28:01 +08:00
|
|
|
self.query.add_annotation(
|
|
|
|
RawSQL(fmt % settings['procedure_args'], settings['select_params'], select_field),
|
|
|
|
model_att)
|
|
|
|
return self
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
def _distance_attribute(self, func, geom=None, tolerance=0.05, spheroid=False, **kwargs):
|
|
|
|
"""
|
|
|
|
DRY routine for GeoQuerySet distance attribute routines.
|
|
|
|
"""
|
|
|
|
# Setting up the distance procedure arguments.
|
2015-05-14 02:51:18 +08:00
|
|
|
procedure_args, geo_field = self._spatial_setup(func, field_name=kwargs.get('field_name'))
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
# If geodetic defaulting distance attribute to meters (Oracle and
|
|
|
|
# PostGIS spherical distances return meters). Otherwise, use the
|
|
|
|
# units of the geometry field.
|
2009-12-22 23:18:51 +08:00
|
|
|
connection = connections[self.db]
|
|
|
|
geodetic = geo_field.geodetic(connection)
|
|
|
|
geography = geo_field.geography
|
|
|
|
|
|
|
|
if geodetic:
|
2008-08-06 02:13:06 +08:00
|
|
|
dist_att = 'm'
|
|
|
|
else:
|
2009-12-22 23:18:51 +08:00
|
|
|
dist_att = Distance.unit_attname(geo_field.units_name(connection))
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2009-11-17 02:49:00 +08:00
|
|
|
# Shortcut booleans for what distance function we're using and
|
|
|
|
# whether the geometry field is 3D.
|
2008-08-06 02:13:06 +08:00
|
|
|
distance = func == 'distance'
|
|
|
|
length = func == 'length'
|
|
|
|
perimeter = func == 'perimeter'
|
2009-01-15 19:06:34 +08:00
|
|
|
if not (distance or length or perimeter):
|
2008-08-06 02:13:06 +08:00
|
|
|
raise ValueError('Unknown distance function: %s' % func)
|
2009-11-17 02:49:00 +08:00
|
|
|
geom_3d = geo_field.dim == 3
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2016-05-02 08:05:19 +08:00
|
|
|
# The field's _get_db_prep_lookup() is used to get any
|
2008-08-06 02:13:06 +08:00
|
|
|
# extra distance parameters. Here we set up the
|
|
|
|
# parameters that will be passed in to field's function.
|
|
|
|
lookup_params = [geom or 'POINT (0 0)', 0]
|
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
# Getting the spatial backend operations.
|
|
|
|
backend = connection.ops
|
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
# If the spheroid calculation is desired, either by the `spheroid`
|
2009-03-31 01:15:49 +08:00
|
|
|
# keyword or when calculating the length of geodetic field, make
|
2008-08-06 02:13:06 +08:00
|
|
|
# sure the 'spheroid' distance setting string is passed in so we
|
2009-01-15 19:06:34 +08:00
|
|
|
# get the correct spatial stored procedure.
|
2009-12-22 23:18:51 +08:00
|
|
|
if spheroid or (backend.postgis and geodetic and
|
|
|
|
(not geography) and length):
|
2009-01-15 19:06:34 +08:00
|
|
|
lookup_params.append('spheroid')
|
2009-12-22 23:18:51 +08:00
|
|
|
lookup_params = geo_field.get_prep_value(lookup_params)
|
2016-05-02 08:05:19 +08:00
|
|
|
params = geo_field._get_db_prep_lookup('distance_lte', lookup_params, connection=connection)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2009-01-15 19:06:34 +08:00
|
|
|
# The `geom_args` flag is set to true if a geometry parameter was
|
2008-08-06 02:13:06 +08:00
|
|
|
# passed in.
|
|
|
|
geom_args = bool(geom)
|
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
if backend.oracle:
|
2008-08-06 02:13:06 +08:00
|
|
|
if distance:
|
|
|
|
procedure_fmt = '%(geo_col)s,%(geom)s,%(tolerance)s'
|
|
|
|
elif length or perimeter:
|
|
|
|
procedure_fmt = '%(geo_col)s,%(tolerance)s'
|
|
|
|
procedure_args['tolerance'] = tolerance
|
|
|
|
else:
|
|
|
|
# Getting whether this field is in units of degrees since the field may have
|
|
|
|
# been transformed via the `transform` GeoQuerySet method.
|
2014-12-01 15:28:01 +08:00
|
|
|
srid = self.query.get_context('transformed_srid')
|
|
|
|
if srid:
|
|
|
|
u, unit_name, s = get_srid_info(srid, connection)
|
2014-01-22 09:54:55 +08:00
|
|
|
geodetic = unit_name.lower() in geo_field.geodetic_units
|
2009-01-15 19:06:34 +08:00
|
|
|
|
2016-11-16 20:07:36 +08:00
|
|
|
if geodetic and (not connection.features.supports_distance_geodetic or connection.ops.spatialite):
|
2014-09-04 20:15:09 +08:00
|
|
|
raise ValueError(
|
|
|
|
'This database does not support linear distance '
|
|
|
|
'calculations on geodetic coordinate systems.'
|
|
|
|
)
|
2009-03-31 01:15:49 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
if distance:
|
2014-12-01 15:28:01 +08:00
|
|
|
if srid:
|
2008-08-06 02:13:06 +08:00
|
|
|
# Setting the `geom_args` flag to false because we want to handle
|
|
|
|
# transformation SQL here, rather than the way done by default
|
|
|
|
# (which will transform to the original SRID of the field rather
|
|
|
|
# than to what was transformed to).
|
|
|
|
geom_args = False
|
2014-12-01 15:28:01 +08:00
|
|
|
procedure_fmt = '%s(%%(geo_col)s, %s)' % (backend.transform, srid)
|
|
|
|
if geom.srid is None or geom.srid == srid:
|
2009-01-15 19:06:34 +08:00
|
|
|
# If the geom parameter srid is None, it is assumed the coordinates
|
2008-08-06 02:13:06 +08:00
|
|
|
# are in the transformed units. A placeholder is used for the
|
2009-03-31 01:15:49 +08:00
|
|
|
# geometry parameter. `GeomFromText` constructor is also needed
|
|
|
|
# to wrap geom placeholder for SpatiaLite.
|
2009-12-22 23:18:51 +08:00
|
|
|
if backend.spatialite:
|
2014-12-01 15:28:01 +08:00
|
|
|
procedure_fmt += ', %s(%%%%s, %s)' % (backend.from_text, srid)
|
2009-03-31 01:15:49 +08:00
|
|
|
else:
|
|
|
|
procedure_fmt += ', %%s'
|
2008-08-06 02:13:06 +08:00
|
|
|
else:
|
|
|
|
# We need to transform the geom to the srid specified in `transform()`,
|
|
|
|
# so wrapping the geometry placeholder in transformation SQL.
|
2009-03-31 01:15:49 +08:00
|
|
|
# SpatiaLite also needs geometry placeholder wrapped in `GeomFromText`
|
|
|
|
# constructor.
|
2009-12-22 23:18:51 +08:00
|
|
|
if backend.spatialite:
|
2014-12-01 15:28:01 +08:00
|
|
|
procedure_fmt += (', %s(%s(%%%%s, %s), %s)' % (
|
|
|
|
backend.transform, backend.from_text,
|
|
|
|
geom.srid, srid))
|
2009-03-31 01:15:49 +08:00
|
|
|
else:
|
2014-12-01 15:28:01 +08:00
|
|
|
procedure_fmt += ', %s(%%%%s, %s)' % (backend.transform, srid)
|
2008-08-06 02:13:06 +08:00
|
|
|
else:
|
|
|
|
# `transform()` was not used on this GeoQuerySet.
|
2013-10-22 21:31:43 +08:00
|
|
|
procedure_fmt = '%(geo_col)s,%(geom)s'
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
if not geography and geodetic:
|
2008-08-06 02:13:06 +08:00
|
|
|
# Spherical distance calculation is needed (because the geographic
|
2009-01-15 19:06:34 +08:00
|
|
|
# field is geodetic). However, the PostGIS ST_distance_sphere/spheroid()
|
2008-08-06 02:13:06 +08:00
|
|
|
# procedures may only do queries from point columns to point geometries
|
|
|
|
# some error checking is required.
|
2010-01-28 07:09:46 +08:00
|
|
|
if not backend.geography:
|
|
|
|
if not isinstance(geo_field, PointField):
|
|
|
|
raise ValueError('Spherical distance calculation only supported on PointFields.')
|
2014-05-28 15:22:53 +08:00
|
|
|
if not str(Geometry(six.memoryview(params[0].ewkb)).geom_type) == 'Point':
|
2014-09-04 20:15:09 +08:00
|
|
|
raise ValueError(
|
|
|
|
'Spherical distance calculation only supported with '
|
|
|
|
'Point Geometry parameters'
|
|
|
|
)
|
2008-08-06 02:13:06 +08:00
|
|
|
# The `function` procedure argument needs to be set differently for
|
|
|
|
# geodetic distance calculations.
|
|
|
|
if spheroid:
|
|
|
|
# Call to distance_spheroid() requires spheroid param as well.
|
2009-12-22 23:18:51 +08:00
|
|
|
procedure_fmt += ",'%(spheroid)s'"
|
2013-10-27 09:27:42 +08:00
|
|
|
procedure_args.update({'function': backend.distance_spheroid, 'spheroid': params[1]})
|
2008-08-06 02:13:06 +08:00
|
|
|
else:
|
2013-10-27 09:27:42 +08:00
|
|
|
procedure_args.update({'function': backend.distance_sphere})
|
2008-08-06 02:13:06 +08:00
|
|
|
elif length or perimeter:
|
|
|
|
procedure_fmt = '%(geo_col)s'
|
2009-12-22 23:18:51 +08:00
|
|
|
if not geography and geodetic and length:
|
2009-11-17 02:49:00 +08:00
|
|
|
# There's no `length_sphere`, and `length_spheroid` also
|
|
|
|
# works on 3D geometries.
|
2009-12-22 23:18:51 +08:00
|
|
|
procedure_fmt += ",'%(spheroid)s'"
|
2013-10-27 09:27:42 +08:00
|
|
|
procedure_args.update({'function': backend.length_spheroid, 'spheroid': params[1]})
|
2014-08-22 00:47:57 +08:00
|
|
|
elif geom_3d and connection.features.supports_3d_functions:
|
|
|
|
# Use 3D variants of perimeter and length routines on supported backends.
|
2009-11-17 02:49:00 +08:00
|
|
|
if perimeter:
|
2013-10-27 09:27:42 +08:00
|
|
|
procedure_args.update({'function': backend.perimeter3d})
|
2009-11-17 02:49:00 +08:00
|
|
|
elif length:
|
2013-10-27 09:27:42 +08:00
|
|
|
procedure_args.update({'function': backend.length3d})
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
# Setting up the settings for `_spatial_attribute`.
|
2013-10-27 09:27:42 +08:00
|
|
|
s = {'select_field': DistanceField(dist_att),
|
|
|
|
'setup': False,
|
|
|
|
'geo_field': geo_field,
|
|
|
|
'procedure_args': procedure_args,
|
|
|
|
'procedure_fmt': procedure_fmt,
|
2008-08-06 02:13:06 +08:00
|
|
|
}
|
2009-01-15 19:06:34 +08:00
|
|
|
if geom_args:
|
2008-08-06 02:13:06 +08:00
|
|
|
s['geom_args'] = ('geom',)
|
|
|
|
s['procedure_args']['geom'] = geom
|
|
|
|
elif geom:
|
|
|
|
# The geometry is passed in as a parameter because we handled
|
|
|
|
# transformation conditions in this routine.
|
2009-12-22 23:18:51 +08:00
|
|
|
s['select_params'] = [backend.Adapter(geom)]
|
2008-08-06 02:13:06 +08:00
|
|
|
return self._spatial_attribute(func, s, **kwargs)
|
|
|
|
|
|
|
|
def _geom_attribute(self, func, tolerance=0.05, **kwargs):
|
|
|
|
"""
|
|
|
|
DRY routine for setting up a GeoQuerySet method that attaches a
|
|
|
|
Geometry attribute (e.g., `centroid`, `point_on_surface`).
|
|
|
|
"""
|
2013-10-25 01:30:03 +08:00
|
|
|
s = {'select_field': GeomField()}
|
2009-12-22 23:18:51 +08:00
|
|
|
if connections[self.db].ops.oracle:
|
2008-08-06 02:13:06 +08:00
|
|
|
s['procedure_fmt'] = '%(geo_col)s,%(tolerance)s'
|
2013-10-27 09:27:42 +08:00
|
|
|
s['procedure_args'] = {'tolerance': tolerance}
|
2008-08-06 02:13:06 +08:00
|
|
|
return self._spatial_attribute(func, s, **kwargs)
|
2009-01-15 19:06:34 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
def _geomset_attribute(self, func, geom, tolerance=0.05, **kwargs):
|
|
|
|
"""
|
|
|
|
DRY routine for setting up a GeoQuerySet method that attaches a
|
|
|
|
Geometry attribute and takes a Geoemtry parameter. This is used
|
2009-01-15 19:06:34 +08:00
|
|
|
for geometry set-like operations (e.g., intersection, difference,
|
2008-08-06 02:13:06 +08:00
|
|
|
union, sym_difference).
|
|
|
|
"""
|
2013-12-09 01:20:06 +08:00
|
|
|
s = {
|
|
|
|
'geom_args': ('geom',),
|
|
|
|
'select_field': GeomField(),
|
|
|
|
'procedure_fmt': '%(geo_col)s,%(geom)s',
|
|
|
|
'procedure_args': {'geom': geom},
|
|
|
|
}
|
2009-12-22 23:18:51 +08:00
|
|
|
if connections[self.db].ops.oracle:
|
2008-08-06 02:13:06 +08:00
|
|
|
s['procedure_fmt'] += ',%(tolerance)s'
|
|
|
|
s['procedure_args']['tolerance'] = tolerance
|
|
|
|
return self._spatial_attribute(func, s, **kwargs)
|
|
|
|
|
2009-01-15 19:06:34 +08:00
|
|
|
def _geocol_select(self, geo_field, field_name):
|
2008-08-06 02:13:06 +08:00
|
|
|
"""
|
|
|
|
Helper routine for constructing the SQL to select the geographic
|
|
|
|
column. Takes into account if the geographic field is in a
|
|
|
|
ForeignKey relation to the current model.
|
|
|
|
"""
|
2014-12-01 15:28:01 +08:00
|
|
|
compiler = self.query.get_compiler(self.db)
|
2008-11-06 01:51:11 +08:00
|
|
|
opts = self.model._meta
|
2014-03-31 03:11:05 +08:00
|
|
|
if geo_field not in opts.fields:
|
2008-11-06 01:51:11 +08:00
|
|
|
# Is this operation going to be on a related geographic field?
|
2008-08-06 02:13:06 +08:00
|
|
|
# If so, it'll have to be added to the select related information
|
2015-10-10 09:03:04 +08:00
|
|
|
# (e.g., if 'location__point' was given as the field name, then
|
|
|
|
# chop the non-relational field and add select_related('location')).
|
2014-12-01 15:28:01 +08:00
|
|
|
# Note: the operation really is defined as "must add select related!"
|
2015-10-10 09:03:04 +08:00
|
|
|
self.query.add_select_related([field_name.rsplit(LOOKUP_SEP, 1)[0]])
|
2014-12-01 15:28:01 +08:00
|
|
|
# Call pre_sql_setup() so that compiler.select gets populated.
|
2009-12-22 23:18:51 +08:00
|
|
|
compiler.pre_sql_setup()
|
2014-12-01 15:28:01 +08:00
|
|
|
for col, _, _ in compiler.select:
|
|
|
|
if col.output_field == geo_field:
|
|
|
|
return col.as_sql(compiler, compiler.connection)[0]
|
|
|
|
raise ValueError("%r not in compiler's related_select_cols" % geo_field)
|
2014-03-31 03:11:05 +08:00
|
|
|
elif geo_field not in opts.local_fields:
|
2008-11-06 01:51:11 +08:00
|
|
|
# This geographic field is inherited from another model, so we have to
|
|
|
|
# use the db table for the _parent_ model instead.
|
2015-01-07 08:16:35 +08:00
|
|
|
parent_model = geo_field.model._meta.concrete_model
|
2014-12-01 15:28:01 +08:00
|
|
|
return self._field_column(compiler, geo_field, parent_model._meta.db_table)
|
|
|
|
else:
|
|
|
|
return self._field_column(compiler, geo_field)
|
|
|
|
|
|
|
|
# Private API utilities, subject to change.
|
|
|
|
def _geo_field(self, field_name=None):
|
|
|
|
"""
|
|
|
|
Returns the first Geometry field encountered or the one specified via
|
|
|
|
the `field_name` keyword. The `field_name` may be a string specifying
|
|
|
|
the geometry field on this GeoQuerySet's model, or a lookup string
|
|
|
|
to a geometry field via a ForeignKey relation.
|
|
|
|
"""
|
|
|
|
if field_name is None:
|
|
|
|
# Incrementing until the first geographic field is found.
|
|
|
|
for field in self.model._meta.fields:
|
|
|
|
if isinstance(field, GeometryField):
|
|
|
|
return field
|
|
|
|
return False
|
2008-08-06 02:13:06 +08:00
|
|
|
else:
|
2014-12-01 15:28:01 +08:00
|
|
|
# Otherwise, check by the given field name -- which may be
|
|
|
|
# a lookup to a _related_ geographic field.
|
|
|
|
return GISLookup._check_geo_field(self.model._meta, field_name)
|
|
|
|
|
|
|
|
def _field_column(self, compiler, field, table_alias=None, column=None):
|
|
|
|
"""
|
|
|
|
Helper function that returns the database column for the given field.
|
|
|
|
The table and column are returned (quoted) in the proper format, e.g.,
|
|
|
|
`"geoapp_city"."point"`. If `table_alias` is not specified, the
|
|
|
|
database table associated with the model of this `GeoQuerySet` will be
|
|
|
|
used. If `column` is specified, it will be used instead of the value
|
|
|
|
in `field.column`.
|
|
|
|
"""
|
|
|
|
if table_alias is None:
|
|
|
|
table_alias = compiler.query.get_meta().db_table
|
|
|
|
return "%s.%s" % (compiler.quote_name_unless_alias(table_alias),
|
|
|
|
compiler.connection.ops.quote_name(column or field.column))
|