diff --git a/django/contrib/gis/forms/widgets.py b/django/contrib/gis/forms/widgets.py index d50c7c005a5..0102ab6745f 100644 --- a/django/contrib/gis/forms/widgets.py +++ b/django/contrib/gis/forms/widgets.py @@ -22,51 +22,54 @@ class BaseGeometryWidget(Widget): map_srid = 4326 map_width = 600 map_height = 400 - display_wkt = False + display_raw = False supports_3d = False template_name = '' # set on subclasses def __init__(self, attrs=None): self.attrs = {} - for key in ('geom_type', 'map_srid', 'map_width', 'map_height', 'display_wkt'): + for key in ('geom_type', 'map_srid', 'map_width', 'map_height', 'display_raw'): self.attrs[key] = getattr(self, key) if attrs: self.attrs.update(attrs) + def serialize(self, value): + return value.wkt if value else '' + + def deserialize(self, value): + try: + return GEOSGeometry(value) + except (GEOSException, ValueError) as err: + logger.error( + "Error creating geometry from value '%s' (%s)" % ( + value, err) + ) + return None + def render(self, name, value, attrs=None): # If a string reaches here (via a validation error on another # field) then just reconstruct the Geometry. if isinstance(value, six.string_types): - try: - value = GEOSGeometry(value) - except (GEOSException, ValueError) as err: - logger.error( - "Error creating geometry from value '%s' (%s)" % ( - value, err) - ) - value = None + value = self.deserialize(value) - wkt = '' if value: # Check that srid of value and map match if value.srid != self.map_srid: try: ogr = value.ogr ogr.transform(self.map_srid) - wkt = ogr.wkt + value = ogr except gdal.OGRException as err: logger.error( "Error transforming geometry from srid '%s' to srid '%s' (%s)" % ( value.srid, self.map_srid, err) ) - else: - wkt = value.wkt context = self.build_attrs(attrs, name=name, module='geodjango_%s' % name.replace('-','_'), # JS-safe - wkt=wkt, + serialized=self.serialize(value), geom_type=gdal.OGRGeomType(self.attrs['geom_type']), STATIC_URL=settings.STATIC_URL, LANGUAGE_BIDI=translation.get_language_bidi(), diff --git a/django/contrib/gis/templates/gis/openlayers.html b/django/contrib/gis/templates/gis/openlayers.html index 281c0badd63..4884f48106c 100644 --- a/django/contrib/gis/templates/gis/openlayers.html +++ b/django/contrib/gis/templates/gis/openlayers.html @@ -2,7 +2,7 @@ #{{ id }}_map { width: {{ map_width }}px; height: {{ map_height }}px; } #{{ id }}_map .aligned label { float: inherit; } #{{ id }}_div_map { position: relative; vertical-align: top; float: {{ LANGUAGE_BIDI|yesno:"right,left" }}; } - {% if not display_wkt %}#{{ id }} { display: none; }{% endif %} + {% if not display_raw %}#{{ id }} { display: none; }{% endif %} .olControlEditingToolbar .olControlModifyFeatureItemActive { background-image: url("{{ STATIC_URL }}admin/img/gis/move_vertex_on.png"); background-repeat: no-repeat; @@ -16,8 +16,8 @@
Delete all Features - {% if display_wkt %}

WKT debugging window:

{% endif %} - + {% if display_raw %}

Debugging window (serialized value):

{% endif %} +