2012-06-08 00:08:47 +08:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
from django import forms
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
|
2009-04-26 02:24:32 +08:00
|
|
|
# While this couples the geographic forms to the GEOS library,
|
|
|
|
# it decouples from database (by not importing SpatialBackend).
|
2013-10-18 19:25:30 +08:00
|
|
|
from django.contrib.gis.geos import GEOSException, GEOSGeometry
|
2013-03-16 18:39:18 +08:00
|
|
|
from .widgets import OpenLayersWidget
|
2013-01-26 03:50:46 +08:00
|
|
|
|
2009-04-26 02:24:32 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
class GeometryField(forms.Field):
|
2008-08-26 08:34:34 +08:00
|
|
|
"""
|
|
|
|
This is the basic form field for a Geometry. Any textual input that is
|
2009-04-26 02:24:32 +08:00
|
|
|
accepted by GEOSGeometry is accepted by this form. By default,
|
|
|
|
this includes WKT, HEXEWKB, WKB (in a buffer), and GeoJSON.
|
2008-08-26 08:34:34 +08:00
|
|
|
"""
|
2013-03-16 18:39:18 +08:00
|
|
|
widget = OpenLayersWidget
|
|
|
|
geom_type = 'GEOMETRY'
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
default_error_messages = {
|
2013-10-27 09:27:42 +08:00
|
|
|
'required': _('No geometry value provided.'),
|
|
|
|
'invalid_geom': _('Invalid geometry value.'),
|
|
|
|
'invalid_geom_type': _('Invalid geometry type.'),
|
|
|
|
'transform_error': _('An error occurred when transforming the geometry '
|
2013-12-13 04:23:24 +08:00
|
|
|
'to the SRID of the geometry form field.'),
|
2013-10-18 17:02:43 +08:00
|
|
|
}
|
2008-08-26 08:34:34 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
def __init__(self, **kwargs):
|
2009-04-26 02:24:32 +08:00
|
|
|
# Pop out attributes from the database field, or use sensible
|
|
|
|
# defaults (e.g., allow None).
|
|
|
|
self.srid = kwargs.pop('srid', None)
|
2013-03-16 18:39:18 +08:00
|
|
|
self.geom_type = kwargs.pop('geom_type', self.geom_type)
|
2008-08-06 02:13:06 +08:00
|
|
|
super(GeometryField, self).__init__(**kwargs)
|
2013-03-16 18:39:18 +08:00
|
|
|
self.widget.attrs['geom_type'] = self.geom_type
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2012-06-19 20:55:40 +08:00
|
|
|
def to_python(self, value):
|
|
|
|
"""
|
|
|
|
Transforms the value to a Geometry object.
|
|
|
|
"""
|
2013-03-16 04:45:33 +08:00
|
|
|
if value in self.empty_values:
|
|
|
|
return None
|
2013-09-03 19:51:55 +08:00
|
|
|
|
|
|
|
if not isinstance(value, GEOSGeometry):
|
|
|
|
try:
|
|
|
|
value = GEOSGeometry(value)
|
|
|
|
except (GEOSException, ValueError, TypeError):
|
|
|
|
raise forms.ValidationError(self.error_messages['invalid_geom'], code='invalid_geom')
|
2013-11-23 17:53:09 +08:00
|
|
|
|
|
|
|
# Try to set the srid
|
|
|
|
if not value.srid:
|
|
|
|
try:
|
|
|
|
value.srid = self.widget.map_srid
|
|
|
|
except AttributeError:
|
|
|
|
if self.srid:
|
|
|
|
value.srid = self.srid
|
2013-09-03 19:51:55 +08:00
|
|
|
return value
|
2012-06-19 20:55:40 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
def clean(self, value):
|
|
|
|
"""
|
|
|
|
Validates that the input value can be converted to a Geometry
|
|
|
|
object (which is returned). A ValidationError is raised if
|
|
|
|
the value cannot be instantiated as a Geometry.
|
|
|
|
"""
|
2013-03-16 04:45:33 +08:00
|
|
|
geom = super(GeometryField, self).clean(value)
|
|
|
|
if geom is None:
|
|
|
|
return geom
|
2009-04-26 02:24:32 +08:00
|
|
|
|
2008-08-26 08:34:34 +08:00
|
|
|
# Ensuring that the geometry is of the correct type (indicated
|
|
|
|
# using the OGC string label).
|
|
|
|
if str(geom.geom_type).upper() != self.geom_type and not self.geom_type == 'GEOMETRY':
|
2013-06-06 02:55:05 +08:00
|
|
|
raise forms.ValidationError(self.error_messages['invalid_geom_type'], code='invalid_geom_type')
|
2008-08-26 08:34:34 +08:00
|
|
|
|
2009-04-26 02:24:32 +08:00
|
|
|
# Transforming the geometry if the SRID was set.
|
2013-11-23 17:53:09 +08:00
|
|
|
if self.srid and self.srid != -1 and self.srid != geom.srid:
|
|
|
|
try:
|
|
|
|
geom.transform(self.srid)
|
|
|
|
except GEOSException:
|
|
|
|
raise forms.ValidationError(
|
|
|
|
self.error_messages['transform_error'], code='transform_error')
|
2009-04-26 02:24:32 +08:00
|
|
|
|
2008-08-26 08:34:34 +08:00
|
|
|
return geom
|
2013-01-26 03:50:46 +08:00
|
|
|
|
2014-08-07 10:56:23 +08:00
|
|
|
def has_changed(self, initial, data):
|
2013-01-26 03:50:46 +08:00
|
|
|
""" Compare geographic value of data with its initial value. """
|
|
|
|
|
2013-09-03 19:51:55 +08:00
|
|
|
try:
|
|
|
|
data = self.to_python(data)
|
|
|
|
initial = self.to_python(initial)
|
2013-09-07 12:56:40 +08:00
|
|
|
except forms.ValidationError:
|
2013-09-03 19:51:55 +08:00
|
|
|
return True
|
2013-01-26 03:50:46 +08:00
|
|
|
|
|
|
|
# Only do a geographic comparison if both values are available
|
|
|
|
if initial and data:
|
|
|
|
data.transform(initial.srid)
|
|
|
|
# If the initial value was not added by the browser, the geometry
|
|
|
|
# provided may be slightly different, the first time it is saved.
|
|
|
|
# The comparison is done with a very low tolerance.
|
|
|
|
return not initial.equals_exact(data, tolerance=0.000001)
|
|
|
|
else:
|
|
|
|
# Check for change of state of existence
|
|
|
|
return bool(initial) != bool(data)
|
2013-03-16 18:39:18 +08:00
|
|
|
|
|
|
|
|
|
|
|
class GeometryCollectionField(GeometryField):
|
|
|
|
geom_type = 'GEOMETRYCOLLECTION'
|
|
|
|
|
|
|
|
|
|
|
|
class PointField(GeometryField):
|
|
|
|
geom_type = 'POINT'
|
|
|
|
|
|
|
|
|
|
|
|
class MultiPointField(GeometryField):
|
|
|
|
geom_type = 'MULTIPOINT'
|
|
|
|
|
|
|
|
|
|
|
|
class LineStringField(GeometryField):
|
|
|
|
geom_type = 'LINESTRING'
|
|
|
|
|
|
|
|
|
|
|
|
class MultiLineStringField(GeometryField):
|
|
|
|
geom_type = 'MULTILINESTRING'
|
|
|
|
|
|
|
|
|
|
|
|
class PolygonField(GeometryField):
|
|
|
|
geom_type = 'POLYGON'
|
|
|
|
|
|
|
|
|
|
|
|
class MultiPolygonField(GeometryField):
|
|
|
|
geom_type = 'MULTIPOLYGON'
|