2012-10-17 17:55:38 +08:00
|
|
|
import logging
|
|
|
|
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.contrib.gis.gdal import GDALException
|
|
|
|
from django.contrib.gis.geos import GEOSException, GEOSGeometry
|
2008-08-06 02:13:06 +08:00
|
|
|
from django.forms.widgets import Textarea
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.utils import six, translation
|
2011-06-30 17:06:19 +08:00
|
|
|
|
2009-01-13 10:08:26 +08:00
|
|
|
# Creating a template context that contains Django settings
|
|
|
|
# values needed by admin map templates.
|
2014-12-14 17:17:18 +08:00
|
|
|
geo_context = {'LANGUAGE_BIDI': translation.get_language_bidi()}
|
2012-10-17 17:55:38 +08:00
|
|
|
logger = logging.getLogger('django.contrib.gis')
|
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
class OpenLayersWidget(Textarea):
|
|
|
|
"""
|
|
|
|
Renders an OpenLayers map using the WKT of the geometry.
|
|
|
|
"""
|
2016-12-28 06:00:56 +08:00
|
|
|
def get_context(self, name, value, attrs=None):
|
2008-08-06 02:13:06 +08:00
|
|
|
# Update the template parameters with any attributes passed in.
|
2013-10-17 16:17:41 +08:00
|
|
|
if attrs:
|
|
|
|
self.params.update(attrs)
|
2014-08-05 00:14:45 +08:00
|
|
|
self.params['editable'] = self.params['modifiable']
|
|
|
|
else:
|
|
|
|
self.params['editable'] = True
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
# Defaulting the WKT value to a blank string -- this
|
|
|
|
# will be tested in the JavaScript and the appropriate
|
2009-03-31 06:10:36 +08:00
|
|
|
# interface will be constructed.
|
2008-08-06 02:13:06 +08:00
|
|
|
self.params['wkt'] = ''
|
|
|
|
|
|
|
|
# If a string reaches here (via a validation error on another
|
|
|
|
# field) then just reconstruct the Geometry.
|
2016-08-06 17:14:41 +08:00
|
|
|
if value and isinstance(value, six.string_types):
|
2008-08-06 02:13:06 +08:00
|
|
|
try:
|
|
|
|
value = GEOSGeometry(value)
|
2012-10-17 17:55:38 +08:00
|
|
|
except (GEOSException, ValueError) as err:
|
2015-12-27 04:49:52 +08:00
|
|
|
logger.error("Error creating geometry from value '%s' (%s)", value, err)
|
2008-08-06 02:13:06 +08:00
|
|
|
value = None
|
|
|
|
|
2013-03-09 23:01:33 +08:00
|
|
|
if (value and value.geom_type.upper() != self.geom_type and
|
|
|
|
self.geom_type != 'GEOMETRY'):
|
2008-08-06 02:13:06 +08:00
|
|
|
value = None
|
|
|
|
|
|
|
|
# Constructing the dictionary of the map options.
|
|
|
|
self.params['map_options'] = self.map_options()
|
|
|
|
|
2009-04-26 14:08:26 +08:00
|
|
|
# Constructing the JavaScript module name using the name of
|
2008-08-06 02:13:06 +08:00
|
|
|
# the GeometryField (passed in via the `attrs` keyword).
|
2009-04-26 14:08:26 +08:00
|
|
|
# Use the 'name' attr for the field name (rather than 'field')
|
|
|
|
self.params['name'] = name
|
|
|
|
# note: we must switch out dashes for underscores since js
|
|
|
|
# functions are created using the module variable
|
2013-10-25 01:30:03 +08:00
|
|
|
js_safe_name = self.params['name'].replace('-', '_')
|
2009-04-26 14:08:26 +08:00
|
|
|
self.params['module'] = 'geodjango_%s' % js_safe_name
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
if value:
|
|
|
|
# Transforming the geometry to the projection used on the
|
|
|
|
# OpenLayers map.
|
|
|
|
srid = self.params['srid']
|
2009-03-31 06:10:36 +08:00
|
|
|
if value.srid != srid:
|
2008-08-06 02:13:06 +08:00
|
|
|
try:
|
2009-01-13 10:08:26 +08:00
|
|
|
ogr = value.ogr
|
|
|
|
ogr.transform(srid)
|
|
|
|
wkt = ogr.wkt
|
2014-12-18 05:00:05 +08:00
|
|
|
except GDALException as err:
|
2012-10-17 17:55:38 +08:00
|
|
|
logger.error(
|
2015-12-27 04:49:52 +08:00
|
|
|
"Error transforming geometry from srid '%s' to srid '%s' (%s)",
|
|
|
|
value.srid, srid, err
|
2012-10-17 17:55:38 +08:00
|
|
|
)
|
2008-08-06 02:13:06 +08:00
|
|
|
wkt = ''
|
|
|
|
else:
|
|
|
|
wkt = value.wkt
|
2009-03-31 06:10:36 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
# Setting the parameter WKT with that of the transformed
|
|
|
|
# geometry.
|
|
|
|
self.params['wkt'] = wkt
|
|
|
|
|
2014-12-14 17:17:18 +08:00
|
|
|
self.params.update(geo_context)
|
2016-12-28 06:00:56 +08:00
|
|
|
return self.params
|
2009-03-31 06:10:36 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
def map_options(self):
|
|
|
|
"Builds the map options hash for the OpenLayers template."
|
|
|
|
|
|
|
|
# JavaScript construction utilities for the Bounds and Projection.
|
|
|
|
def ol_bounds(extent):
|
|
|
|
return 'new OpenLayers.Bounds(%s)' % str(extent)
|
2013-10-22 18:21:07 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
def ol_projection(srid):
|
|
|
|
return 'new OpenLayers.Projection("EPSG:%s")' % srid
|
|
|
|
|
|
|
|
# An array of the parameter name, the name of their OpenLayers
|
|
|
|
# counterpart, and the type of variable they are.
|
2009-03-31 06:10:36 +08:00
|
|
|
map_types = [('srid', 'projection', 'srid'),
|
|
|
|
('display_srid', 'displayProjection', 'srid'),
|
2008-08-06 02:13:06 +08:00
|
|
|
('units', 'units', str),
|
|
|
|
('max_resolution', 'maxResolution', float),
|
|
|
|
('max_extent', 'maxExtent', 'bounds'),
|
|
|
|
('num_zoom', 'numZoomLevels', int),
|
|
|
|
('max_zoom', 'maxZoomLevels', int),
|
|
|
|
('min_zoom', 'minZoomLevel', int),
|
|
|
|
]
|
|
|
|
|
|
|
|
# Building the map options hash.
|
|
|
|
map_options = {}
|
|
|
|
for param_name, js_name, option_type in map_types:
|
|
|
|
if self.params.get(param_name, False):
|
|
|
|
if option_type == 'srid':
|
|
|
|
value = ol_projection(self.params[param_name])
|
|
|
|
elif option_type == 'bounds':
|
|
|
|
value = ol_bounds(self.params[param_name])
|
|
|
|
elif option_type in (float, int):
|
|
|
|
value = self.params[param_name]
|
|
|
|
elif option_type in (str,):
|
|
|
|
value = '"%s"' % self.params[param_name]
|
|
|
|
else:
|
|
|
|
raise TypeError
|
|
|
|
map_options[js_name] = value
|
|
|
|
return map_options
|