diff --git a/django/contrib/gis/admin/widgets.py b/django/contrib/gis/admin/widgets.py
index 056d259..4c14d66 100644
a
|
b
|
class OpenLayersWidget(Textarea):
|
25 | 25 | # Defaulting the WKT value to a blank string -- this |
26 | 26 | # will be tested in the JavaScript and the appropriate |
27 | 27 | # interface will be constructed. |
28 | | self.params['wkt'] = '' |
| 28 | self.params['serialized'] = '' |
29 | 29 | |
30 | 30 | # If a string reaches here (via a validation error on another |
31 | 31 | # field) then just reconstruct the Geometry. |
32 | 32 | if isinstance(value, six.string_types): |
33 | | try: |
34 | | value = GEOSGeometry(value) |
35 | | except (GEOSException, ValueError) as err: |
36 | | logger.error( |
37 | | "Error creating geometry from value '%s' (%s)" % ( |
38 | | value, err) |
39 | | ) |
40 | | value = None |
| 33 | value = self._deserialize(value) |
41 | 34 | |
42 | 35 | if (value and value.geom_type.upper() != self.geom_type and |
43 | 36 | self.geom_type != 'GEOMETRY'): |
… |
… |
class OpenLayersWidget(Textarea):
|
56 | 49 | self.params['module'] = 'geodjango_%s' % js_safe_name |
57 | 50 | |
58 | 51 | if value: |
59 | | # Transforming the geometry to the projection used on the |
60 | | # OpenLayers map. |
61 | | srid = self.params['srid'] |
62 | | if value.srid != srid: |
63 | | try: |
64 | | ogr = value.ogr |
65 | | ogr.transform(srid) |
66 | | wkt = ogr.wkt |
67 | | except OGRException as err: |
68 | | logger.error( |
69 | | "Error transforming geometry from srid '%s' to srid '%s' (%s)" % ( |
70 | | value.srid, srid, err) |
71 | | ) |
72 | | wkt = '' |
73 | | else: |
74 | | wkt = value.wkt |
75 | | |
76 | | # Setting the parameter WKT with that of the transformed |
77 | | # geometry. |
78 | | self.params['wkt'] = wkt |
| 52 | value = self._transform(value) |
| 53 | serialized = self._serialize(value) |
| 54 | self.params['serialized'] = serialized |
79 | 55 | |
80 | 56 | return loader.render_to_string(self.template, self.params, |
81 | 57 | context_instance=geo_context) |
82 | 58 | |
| 59 | def _deserialize(self, value): |
| 60 | try: |
| 61 | return GEOSGeometry(value) |
| 62 | except (GEOSException, ValueError) as err: |
| 63 | logger.error( |
| 64 | "Error creating geometry from value '%s' (%s)" % ( |
| 65 | value, err) |
| 66 | ) |
| 67 | return None |
| 68 | |
| 69 | def _transform(self, value): |
| 70 | # Transforming the geometry to the projection used on the |
| 71 | # OpenLayers map. |
| 72 | srid = self.params['srid'] |
| 73 | if value.srid != srid: |
| 74 | try: |
| 75 | ogr = value.ogr |
| 76 | ogr.transform(srid) |
| 77 | value = ogr |
| 78 | except OGRException as err: |
| 79 | logger.error( |
| 80 | "Error transforming geometry from srid '%s' to srid '%s' (%s)" % ( |
| 81 | value.srid, srid, err) |
| 82 | ) |
| 83 | return None |
| 84 | return value |
| 85 | |
| 86 | def _serialize(self, value): |
| 87 | # Returning the parameter WKT with that of the transformed |
| 88 | # geometry. |
| 89 | return value.wkt if value else '' |
| 90 | |
83 | 91 | def map_options(self): |
84 | 92 | "Builds the map options hash for the OpenLayers template." |
85 | 93 | |
diff --git a/django/contrib/gis/forms/widgets.py b/django/contrib/gis/forms/widgets.py
index d50c7c0..413624a 100644
a
|
b
|
class BaseGeometryWidget(Widget):
|
38 | 38 | # If a string reaches here (via a validation error on another |
39 | 39 | # field) then just reconstruct the Geometry. |
40 | 40 | if isinstance(value, six.string_types): |
41 | | try: |
42 | | value = GEOSGeometry(value) |
43 | | except (GEOSException, ValueError) as err: |
44 | | logger.error( |
45 | | "Error creating geometry from value '%s' (%s)" % ( |
46 | | value, err) |
47 | | ) |
48 | | value = None |
| 41 | value = self._deserialize(value) |
49 | 42 | |
50 | | wkt = '' |
| 43 | serialized = '' |
51 | 44 | if value: |
52 | | # Check that srid of value and map match |
53 | | if value.srid != self.map_srid: |
54 | | try: |
55 | | ogr = value.ogr |
56 | | ogr.transform(self.map_srid) |
57 | | wkt = ogr.wkt |
58 | | except gdal.OGRException as err: |
59 | | logger.error( |
60 | | "Error transforming geometry from srid '%s' to srid '%s' (%s)" % ( |
61 | | value.srid, self.map_srid, err) |
62 | | ) |
63 | | else: |
64 | | wkt = value.wkt |
| 45 | value = self._transform(value) |
| 46 | serialized = self._serialize(value) |
65 | 47 | |
66 | 48 | context = self.build_attrs(attrs, |
67 | 49 | name=name, |
68 | 50 | module='geodjango_%s' % name.replace('-','_'), # JS-safe |
69 | | wkt=wkt, |
| 51 | serialized=serialized, |
70 | 52 | geom_type=gdal.OGRGeomType(self.attrs['geom_type']), |
71 | 53 | STATIC_URL=settings.STATIC_URL, |
72 | 54 | LANGUAGE_BIDI=translation.get_language_bidi(), |
73 | 55 | ) |
74 | 56 | return loader.render_to_string(self.template_name, context) |
75 | 57 | |
| 58 | def _deserialize(self, value): |
| 59 | try: |
| 60 | return GEOSGeometry(value) |
| 61 | except (GEOSException, ValueError) as err: |
| 62 | logger.error( |
| 63 | "Error creating geometry from value '%s' (%s)" % ( |
| 64 | value, err) |
| 65 | ) |
| 66 | return None |
| 67 | |
| 68 | def _transform(self, value): |
| 69 | # Check that srid of value and map match |
| 70 | if value.srid != self.map_srid: |
| 71 | try: |
| 72 | ogr = value.ogr |
| 73 | ogr.transform(self.map_srid) |
| 74 | value = ogr |
| 75 | except gdal.OGRException as err: |
| 76 | logger.error( |
| 77 | "Error transforming geometry from srid '%s' to srid '%s' (%s)" % ( |
| 78 | value.srid, self.map_srid, err) |
| 79 | ) |
| 80 | return value |
| 81 | |
| 82 | def _serialize(self, value): |
| 83 | return value.wkt |
| 84 | |
76 | 85 | |
77 | 86 | class OpenLayersWidget(BaseGeometryWidget): |
78 | 87 | template_name = 'gis/openlayers.html' |
diff --git a/django/contrib/gis/templates/gis/admin/openlayers.html b/django/contrib/gis/templates/gis/admin/openlayers.html
index a61b689..fe42fac 100644
a
|
b
|
|
33 | 33 | <div id="{{ id }}_map"{% if LANGUAGE_BIDI %} dir="ltr"{% endif %}></div> |
34 | 34 | <a href="javascript:{{ module }}.clearFeatures()">Delete all Features</a> |
35 | 35 | {% if display_wkt %}<p> WKT debugging window:</p>{% endif %} |
36 | | <textarea id="{{ id }}" class="vWKTField required" cols="150" rows="10" name="{{ name }}">{{ wkt }}</textarea> |
| 36 | <textarea id="{{ id }}" class="vWKTField required" cols="150" rows="10" name="{{ name }}">{{ serialized }}</textarea> |
37 | 37 | <script type="text/javascript">{% block init_function %}{{ module }}.init();{% endblock %}</script> |
38 | 38 | </span> |
diff --git a/django/contrib/gis/templates/gis/openlayers.html b/django/contrib/gis/templates/gis/openlayers.html
index 281c0ba..771276b 100644
a
|
b
|
|
17 | 17 | <div id="{{ id }}_map"></div> |
18 | 18 | <span class="clear_features"><a href="javascript:{{ module }}.clearFeatures()">Delete all Features</a></span> |
19 | 19 | {% if display_wkt %}<p> WKT debugging window:</p>{% endif %} |
20 | | <textarea id="{{ id }}" class="vWKTField required" cols="150" rows="10" name="{{ name }}">{{ wkt }}</textarea> |
| 20 | <textarea id="{{ id }}" class="vWKTField required" cols="150" rows="10" name="{{ name }}">{{ serialized }}</textarea> |
21 | 21 | <script type="text/javascript"> |
22 | 22 | {% block map_options %}var map_options = {};{% endblock %} |
23 | 23 | {% block options %}var options = { |