2008-08-06 02:13:06 +08:00
|
|
|
from django import forms
|
2008-08-26 08:34:34 +08:00
|
|
|
from django.contrib.gis.db.backend import SpatialBackend
|
2008-08-06 02:13:06 +08:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
|
|
|
|
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
|
|
|
|
accepted by SpatialBackend.Geometry is accepted by this form. By default,
|
|
|
|
this is GEOSGeometry, which accepts WKT, HEXEWKB, WKB, and GeoJSON.
|
|
|
|
"""
|
2008-08-06 02:13:06 +08:00
|
|
|
widget = forms.Textarea
|
|
|
|
|
|
|
|
default_error_messages = {
|
|
|
|
'no_geom' : _(u'No geometry value provided.'),
|
2008-08-26 08:34:34 +08:00
|
|
|
'invalid_geom' : _(u'Invalid geometry value.'),
|
|
|
|
'invalid_geom_type' : _(u'Invalid geometry type.'),
|
2008-08-06 02:13:06 +08:00
|
|
|
}
|
2008-08-26 08:34:34 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
def __init__(self, **kwargs):
|
|
|
|
self.null = kwargs.pop('null')
|
|
|
|
self.geom_type = kwargs.pop('geom_type')
|
|
|
|
super(GeometryField, self).__init__(**kwargs)
|
|
|
|
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
if not value:
|
|
|
|
if self.null:
|
|
|
|
# The geometry column allows NULL, return None.
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
raise forms.ValidationError(self.error_messages['no_geom'])
|
2008-08-26 08:34:34 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
try:
|
2008-08-26 08:34:34 +08:00
|
|
|
# Trying to create a Geometry object from the form value.
|
|
|
|
geom = SpatialBackend.Geometry(value)
|
|
|
|
except:
|
2008-08-06 02:13:06 +08:00
|
|
|
raise forms.ValidationError(self.error_messages['invalid_geom'])
|
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':
|
|
|
|
raise forms.ValidationError(self.error_messages['invalid_geom_type'])
|
|
|
|
|
|
|
|
return geom
|