2013-03-16 18:39:18 +08:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
from django.contrib.gis import gdal
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.contrib.gis.geos import GEOSException, GEOSGeometry
|
2013-03-16 18:39:18 +08:00
|
|
|
from django.forms.widgets import Widget
|
|
|
|
from django.template import loader
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.utils import six, translation
|
2013-03-16 18:39:18 +08:00
|
|
|
|
|
|
|
logger = logging.getLogger('django.contrib.gis')
|
|
|
|
|
|
|
|
|
|
|
|
class BaseGeometryWidget(Widget):
|
|
|
|
"""
|
|
|
|
The base class for rich geometry widgets.
|
|
|
|
Renders a map using the WKT of the geometry.
|
|
|
|
"""
|
|
|
|
geom_type = 'GEOMETRY'
|
|
|
|
map_srid = 4326
|
|
|
|
map_width = 600
|
|
|
|
map_height = 400
|
2013-08-30 16:48:36 +08:00
|
|
|
display_raw = False
|
2013-03-16 18:39:18 +08:00
|
|
|
|
|
|
|
supports_3d = False
|
|
|
|
template_name = '' # set on subclasses
|
|
|
|
|
|
|
|
def __init__(self, attrs=None):
|
|
|
|
self.attrs = {}
|
2013-08-30 16:48:36 +08:00
|
|
|
for key in ('geom_type', 'map_srid', 'map_width', 'map_height', 'display_raw'):
|
2013-03-16 18:39:18 +08:00
|
|
|
self.attrs[key] = getattr(self, key)
|
|
|
|
if attrs:
|
|
|
|
self.attrs.update(attrs)
|
|
|
|
|
2013-08-30 16:48:36 +08:00
|
|
|
def serialize(self, value):
|
|
|
|
return value.wkt if value else ''
|
|
|
|
|
|
|
|
def deserialize(self, value):
|
|
|
|
try:
|
2013-09-03 19:51:55 +08:00
|
|
|
return GEOSGeometry(value, self.map_srid)
|
2013-08-30 16:48:36 +08:00
|
|
|
except (GEOSException, ValueError) as err:
|
|
|
|
logger.error(
|
|
|
|
"Error creating geometry from value '%s' (%s)" % (
|
2013-10-18 06:27:45 +08:00
|
|
|
value, err)
|
2013-08-30 16:48:36 +08:00
|
|
|
)
|
|
|
|
return None
|
|
|
|
|
2013-03-16 18:39:18 +08:00
|
|
|
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):
|
2013-08-30 16:48:36 +08:00
|
|
|
value = self.deserialize(value)
|
2013-03-16 18:39:18 +08:00
|
|
|
|
|
|
|
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)
|
2013-08-30 16:48:36 +08:00
|
|
|
value = ogr
|
2014-12-18 05:00:05 +08:00
|
|
|
except gdal.GDALException as err:
|
2013-03-16 18:39:18 +08:00
|
|
|
logger.error(
|
|
|
|
"Error transforming geometry from srid '%s' to srid '%s' (%s)" % (
|
2013-10-18 06:27:45 +08:00
|
|
|
value.srid, self.map_srid, err)
|
2013-03-16 18:39:18 +08:00
|
|
|
)
|
|
|
|
|
2013-12-09 01:20:06 +08:00
|
|
|
context = self.build_attrs(
|
|
|
|
attrs,
|
2013-03-16 18:39:18 +08:00
|
|
|
name=name,
|
2013-10-25 01:30:03 +08:00
|
|
|
module='geodjango_%s' % name.replace('-', '_'), # JS-safe
|
2013-08-30 16:48:36 +08:00
|
|
|
serialized=self.serialize(value),
|
2013-03-16 18:39:18 +08:00
|
|
|
geom_type=gdal.OGRGeomType(self.attrs['geom_type']),
|
|
|
|
STATIC_URL=settings.STATIC_URL,
|
|
|
|
LANGUAGE_BIDI=translation.get_language_bidi(),
|
|
|
|
)
|
|
|
|
return loader.render_to_string(self.template_name, context)
|
|
|
|
|
|
|
|
|
|
|
|
class OpenLayersWidget(BaseGeometryWidget):
|
|
|
|
template_name = 'gis/openlayers.html'
|
2013-10-22 18:21:07 +08:00
|
|
|
|
2013-03-16 18:39:18 +08:00
|
|
|
class Media:
|
|
|
|
js = (
|
2013-10-13 00:07:24 +08:00
|
|
|
'http://openlayers.org/api/2.13/OpenLayers.js',
|
2013-03-16 18:39:18 +08:00
|
|
|
'gis/js/OLMapWidget.js',
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class OSMWidget(BaseGeometryWidget):
|
|
|
|
"""
|
|
|
|
An OpenLayers/OpenStreetMap-based widget.
|
|
|
|
"""
|
|
|
|
template_name = 'gis/openlayers-osm.html'
|
|
|
|
default_lon = 5
|
|
|
|
default_lat = 47
|
|
|
|
|
|
|
|
class Media:
|
|
|
|
js = (
|
2013-10-13 00:07:24 +08:00
|
|
|
'http://openlayers.org/api/2.13/OpenLayers.js',
|
2013-03-16 18:39:18 +08:00
|
|
|
'http://www.openstreetmap.org/openlayers/OpenStreetMap.js',
|
|
|
|
'gis/js/OLMapWidget.js',
|
|
|
|
)
|
|
|
|
|
2013-12-13 22:05:45 +08:00
|
|
|
def __init__(self, attrs=None):
|
|
|
|
super(OSMWidget, self).__init__()
|
|
|
|
for key in ('default_lon', 'default_lat'):
|
|
|
|
self.attrs[key] = getattr(self, key)
|
|
|
|
if attrs:
|
|
|
|
self.attrs.update(attrs)
|
|
|
|
|
2013-03-16 18:39:18 +08:00
|
|
|
@property
|
|
|
|
def map_srid(self):
|
2014-03-28 06:02:28 +08:00
|
|
|
# Use the official spherical mercator projection SRID when GDAL is
|
|
|
|
# available; otherwise, fallback to 900913.
|
|
|
|
if gdal.HAS_GDAL:
|
2013-03-16 18:39:18 +08:00
|
|
|
return 3857
|
|
|
|
else:
|
|
|
|
return 900913
|