#21003 closed Cleanup/optimization (fixed)
BaseGeometryWidget is not idempotent
Reported by: | Mathieu Leplatre | Owned by: | nobody |
---|---|---|---|
Component: | GIS | Version: | 1.6-beta-1 |
Severity: | Normal | Keywords: | |
Cc: | Claude Paroz | Triage Stage: | Accepted |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | yes | UI/UX: | no |
Description
Currently, the geometry data is serialized as wkt :
https://github.com/django/django/blob/1.6b2/django/contrib/gis/forms/widgets.py#L69
https://github.com/django/django/blob/1.6b2/django/contrib/gis/templates/gis/openlayers.html#L20
And is read back here:
https://github.com/django/django/blob/1.6b2/django/contrib/gis/forms/fields.py#L95
But transform() will fail, because the instantiated Geometry won't have any projection associated (wkt does not carry srid).
`
GEOSException at ...
Calling transform() with no SRID set is not supported
`
https://github.com/django/django/blob/1.6b2/django/contrib/gis/forms/fields.py#L96
There are several possible solutions :
- Use EWKT instead of WKT
- Add srid=self.map_srid parameter to fromstr() when instantiating GEOSGeometry()
- Concatenate 'SRID=%s' % self.map_srid to the received data (this is currently being done like this in JS in openlayers widget code https://github.com/django/django/blob/1.6b2/django/contrib/gis/static/gis/js/OLMapWidget.js#L263-L273)
Attachments (3)
Change History (11)
comment:1 by , 11 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:2 by , 11 years ago
Hum, unless I don't get it, it's wrong.
In your proposition, you assign the database field srid to the data coming for the widget, whereas you should be using the widget's srid !
Something like this :
::
if not data.srid:
data.srid = self.widget.map_srid
If you want to see another approach, for example, in django-leaflet, I did that to fix that problem:
https://github.com/makinacorpus/django-leaflet/blob/add_leaflet_form_widget_and_admin/leaflet/templates/leaflet/widget.html#L34
Which is a consequence of the lack of modularity mentionned in #20998: because in this particular case, what is transmitted between the field and and the widgets is EWKT !
comment:3 by , 11 years ago
Has patch: | set |
---|
comment:4 by , 11 years ago
Since SRID is ignored while parsing GeoJSON ("crs" attribute), this bug prevents from using other serialization format than e/wkt.
Indeed, data coming from widget is read without assigning srid during deserialization. If the serialized format cannot carry it, the resulting geometry has no srid. Which fails at first transform() operation...
by , 11 years ago
Attachment: | 21003-3.diff added |
---|
comment:5 by , 11 years ago
Here's a slightly different patch. I didn't like that we had to reconstruct a Geometry object at two different places in the same file. And I prefer to test the srid after having created the object, because you never know if the client has provided a value containing a parsable srid through a clever thing. Criticism welcome!
comment:7 by , 11 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
What about: