Author: jbronn
Date: 2008-08-25 19:34:34 -0500 (Mon, 25 Aug 2008)
New Revision: 8565

Modified:
   django/trunk/django/contrib/gis/forms/fields.py
Log:
Fixed the `GeometryField` form to catch more than just GEOS exceptions.


Modified: django/trunk/django/contrib/gis/forms/fields.py
===================================================================
--- django/trunk/django/contrib/gis/forms/fields.py     2008-08-26 00:09:29 UTC 
(rev 8564)
+++ django/trunk/django/contrib/gis/forms/fields.py     2008-08-26 00:34:34 UTC 
(rev 8565)
@@ -1,16 +1,21 @@
 from django import forms
-from django.contrib.gis.geos import GEOSGeometry, GEOSException
+from django.contrib.gis.db.backend import SpatialBackend
 from django.utils.translation import ugettext_lazy as _
 
 class GeometryField(forms.Field):
-    # By default a Textarea widget is used.
+    """
+    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.
+    """
     widget = forms.Textarea
 
     default_error_messages = {
         'no_geom' : _(u'No geometry value provided.'),
-        'invalid_geom' : _(u'Invalid Geometry value.'),
-        'invalid_geom_type' : _(u'Invalid Geometry type.'),
+        'invalid_geom' : _(u'Invalid geometry value.'),
+        'invalid_geom_type' : _(u'Invalid geometry type.'),
     }
+
     def __init__(self, **kwargs):
         self.null = kwargs.pop('null')
         self.geom_type = kwargs.pop('geom_type')
@@ -28,10 +33,16 @@
                 return None
             else:
                 raise forms.ValidationError(self.error_messages['no_geom'])
+     
         try:
-            geom = GEOSGeometry(value)
-            if geom.geom_type.upper() != self.geom_type:
-                raise 
forms.ValidationError(self.error_messages['invalid_geom_type'])
-            return geom
-        except GEOSException:
+            # Trying to create a Geometry object from the form value.
+            geom = SpatialBackend.Geometry(value)
+        except:
             raise forms.ValidationError(self.error_messages['invalid_geom'])
+  
+        # 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


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to