~ubuntu-branches/debian/sid/python-django/sid

« back to all changes in this revision

Viewing changes to django/contrib/gis/forms/widgets.py

  • Committer: Package Import Robot
  • Author(s): Luke Faraone
  • Date: 2013-11-07 15:33:49 UTC
  • mfrom: (1.3.12)
  • Revision ID: package-import@ubuntu.com-20131107153349-e31sc149l2szs3jb
Tags: 1.6-1
* New upstream version. Closes: #557474, #724637.
* python-django now also suggests the installation of ipython,
  bpython, python-django-doc, and libgdal1.
  Closes: #636511, #686333, #704203
* Set package maintainer to Debian Python Modules Team.
* Bump standards version to 3.9.5, no changes needed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from __future__ import unicode_literals
 
2
 
 
3
import logging
 
4
 
 
5
from django.conf import settings
 
6
from django.contrib.gis import gdal
 
7
from django.contrib.gis.geos import GEOSGeometry, GEOSException
 
8
from django.forms.widgets import Widget
 
9
from django.template import loader
 
10
from django.utils import six
 
11
from django.utils import translation
 
12
 
 
13
logger = logging.getLogger('django.contrib.gis')
 
14
 
 
15
 
 
16
class BaseGeometryWidget(Widget):
 
17
    """
 
18
    The base class for rich geometry widgets.
 
19
    Renders a map using the WKT of the geometry.
 
20
    """
 
21
    geom_type = 'GEOMETRY'
 
22
    map_srid = 4326
 
23
    map_width = 600
 
24
    map_height = 400
 
25
    display_raw = False
 
26
 
 
27
    supports_3d = False
 
28
    template_name = ''  # set on subclasses
 
29
 
 
30
    def __init__(self, attrs=None):
 
31
        self.attrs = {}
 
32
        for key in ('geom_type', 'map_srid', 'map_width', 'map_height', 'display_raw'):
 
33
            self.attrs[key] = getattr(self, key)
 
34
        if attrs:
 
35
            self.attrs.update(attrs)
 
36
 
 
37
    def serialize(self, value):
 
38
        return value.wkt if value else ''
 
39
 
 
40
    def deserialize(self, value):
 
41
        try:
 
42
            return GEOSGeometry(value, self.map_srid)
 
43
        except (GEOSException, ValueError) as err:
 
44
            logger.error(
 
45
                "Error creating geometry from value '%s' (%s)" % (
 
46
                value, err)
 
47
            )
 
48
        return None
 
49
 
 
50
    def render(self, name, value, attrs=None):
 
51
        # If a string reaches here (via a validation error on another
 
52
        # field) then just reconstruct the Geometry.
 
53
        if isinstance(value, six.string_types):
 
54
            value = self.deserialize(value)
 
55
 
 
56
        if value:
 
57
            # Check that srid of value and map match
 
58
            if value.srid != self.map_srid:
 
59
                try:
 
60
                    ogr = value.ogr
 
61
                    ogr.transform(self.map_srid)
 
62
                    value = ogr
 
63
                except gdal.OGRException as err:
 
64
                    logger.error(
 
65
                        "Error transforming geometry from srid '%s' to srid '%s' (%s)" % (
 
66
                        value.srid, self.map_srid, err)
 
67
                    )
 
68
 
 
69
        context = self.build_attrs(attrs,
 
70
            name=name,
 
71
            module='geodjango_%s' % name.replace('-','_'),  # JS-safe
 
72
            serialized=self.serialize(value),
 
73
            geom_type=gdal.OGRGeomType(self.attrs['geom_type']),
 
74
            STATIC_URL=settings.STATIC_URL,
 
75
            LANGUAGE_BIDI=translation.get_language_bidi(),
 
76
        )
 
77
        return loader.render_to_string(self.template_name, context)
 
78
 
 
79
 
 
80
class OpenLayersWidget(BaseGeometryWidget):
 
81
    template_name = 'gis/openlayers.html'
 
82
    class Media:
 
83
        js = (
 
84
            'http://openlayers.org/api/2.11/OpenLayers.js',
 
85
            'gis/js/OLMapWidget.js',
 
86
        )
 
87
 
 
88
 
 
89
class OSMWidget(BaseGeometryWidget):
 
90
    """
 
91
    An OpenLayers/OpenStreetMap-based widget.
 
92
    """
 
93
    template_name = 'gis/openlayers-osm.html'
 
94
    default_lon = 5
 
95
    default_lat = 47
 
96
 
 
97
    class Media:
 
98
        js = (
 
99
            'http://openlayers.org/api/2.11/OpenLayers.js',
 
100
            'http://www.openstreetmap.org/openlayers/OpenStreetMap.js',
 
101
            'gis/js/OLMapWidget.js',
 
102
        )
 
103
 
 
104
    @property
 
105
    def map_srid(self):
 
106
        # Use the official spherical mercator projection SRID on versions
 
107
        # of GDAL that support it; otherwise, fallback to 900913.
 
108
        if gdal.HAS_GDAL and gdal.GDAL_VERSION >= (1, 7):
 
109
            return 3857
 
110
        else:
 
111
            return 900913
 
112
 
 
113
    def render(self, name, value, attrs=None):
 
114
        default_attrs = {
 
115
            'default_lon': self.default_lon,
 
116
            'default_lat': self.default_lat,
 
117
        }
 
118
        if attrs:
 
119
            default_attrs.update(attrs)
 
120
        return super(OSMWidget, self).render(name, value, default_attrs)