Ticket #20998: refactor-gis-widgets-WIP.diff

File refactor-gis-widgets-WIP.diff, 7.9 KB (added by Mathieu Leplatre, 11 years ago)

Draft of modifications intended

  • django/contrib/gis/admin/widgets.py

    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):  
    2525        # Defaulting the WKT value to a blank string -- this
    2626        # will be tested in the JavaScript and the appropriate
    2727        # interface will be constructed.
    28         self.params['wkt'] = ''
     28        self.params['serialized'] = ''
    2929
    3030        # If a string reaches here (via a validation error on another
    3131        # field) then just reconstruct the Geometry.
    3232        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)
    4134
    4235        if (value and value.geom_type.upper() != self.geom_type and
    4336                self.geom_type != 'GEOMETRY'):
    class OpenLayersWidget(Textarea):  
    5649        self.params['module'] = 'geodjango_%s' % js_safe_name
    5750
    5851        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
    7955
    8056        return loader.render_to_string(self.template, self.params,
    8157                                       context_instance=geo_context)
    8258
     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
    8391    def map_options(self):
    8492        "Builds the map options hash for the OpenLayers template."
    8593
  • django/contrib/gis/forms/widgets.py

    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):  
    3838        # If a string reaches here (via a validation error on another
    3939        # field) then just reconstruct the Geometry.
    4040        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)
    4942
    50         wkt = ''
     43        serialized = ''
    5144        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)
    6547
    6648        context = self.build_attrs(attrs,
    6749            name=name,
    6850            module='geodjango_%s' % name.replace('-','_'),  # JS-safe
    69             wkt=wkt,
     51            serialized=serialized,
    7052            geom_type=gdal.OGRGeomType(self.attrs['geom_type']),
    7153            STATIC_URL=settings.STATIC_URL,
    7254            LANGUAGE_BIDI=translation.get_language_bidi(),
    7355        )
    7456        return loader.render_to_string(self.template_name, context)
    7557
     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
    7685
    7786class OpenLayersWidget(BaseGeometryWidget):
    7887    template_name = 'gis/openlayers.html'
  • django/contrib/gis/templates/gis/admin/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  
    3333<div id="{{ id }}_map"{% if LANGUAGE_BIDI %} dir="ltr"{% endif %}></div>
    3434<a href="javascript:{{ module }}.clearFeatures()">Delete all Features</a>
    3535{% 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>
    3737<script type="text/javascript">{% block init_function %}{{ module }}.init();{% endblock %}</script>
    3838</span>
  • django/contrib/gis/templates/gis/openlayers.html

    diff --git a/django/contrib/gis/templates/gis/openlayers.html b/django/contrib/gis/templates/gis/openlayers.html
    index 281c0ba..771276b 100644
    a b  
    1717    <div id="{{ id }}_map"></div>
    1818    <span class="clear_features"><a href="javascript:{{ module }}.clearFeatures()">Delete all Features</a></span>
    1919    {% 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>
    2121    <script type="text/javascript">
    2222        {% block map_options %}var map_options = {};{% endblock %}
    2323        {% block options %}var options = {
Back to Top