2013-03-16 18:39:18 +08:00
|
|
|
import logging
|
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
from django.contrib.gis import gdal
|
2018-02-06 18:51:41 +08:00
|
|
|
from django.contrib.gis.geometry import json_regex
|
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
|
2016-12-29 23:27:49 +08:00
|
|
|
from django.utils import 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.
|
2017-01-25 04:31:57 +08:00
|
|
|
Render a map using the WKT of the geometry.
|
2013-03-16 18:39:18 +08:00
|
|
|
"""
|
|
|
|
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:
|
2017-03-30 21:38:26 +08:00
|
|
|
return GEOSGeometry(value)
|
2018-02-06 18:51:41 +08:00
|
|
|
except (GEOSException, ValueError, TypeError) as err:
|
2015-12-27 04:49:52 +08:00
|
|
|
logger.error("Error creating geometry from value '%s' (%s)", value, err)
|
2013-08-30 16:48:36 +08:00
|
|
|
return None
|
|
|
|
|
2017-03-21 22:15:34 +08:00
|
|
|
def get_context(self, name, value, attrs):
|
2017-04-20 23:36:40 +08:00
|
|
|
context = super().get_context(name, value, attrs)
|
2013-03-16 18:39:18 +08:00
|
|
|
# If a string reaches here (via a validation error on another
|
|
|
|
# field) then just reconstruct the Geometry.
|
2016-12-29 23:27:49 +08:00
|
|
|
if value and isinstance(value, str):
|
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
|
2017-03-30 21:38:26 +08:00
|
|
|
if value.srid and value.srid != self.map_srid:
|
2013-03-16 18:39:18 +08:00
|
|
|
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(
|
2015-12-27 04:49:52 +08:00
|
|
|
"Error transforming geometry from srid '%s' to srid '%s' (%s)",
|
|
|
|
value.srid, self.map_srid, err
|
2013-03-16 18:39:18 +08:00
|
|
|
)
|
|
|
|
|
2020-11-26 06:36:35 +08:00
|
|
|
geom_type = gdal.OGRGeomType(self.attrs['geom_type']).name
|
2017-12-11 20:08:45 +08:00
|
|
|
context.update(self.build_attrs(self.attrs, {
|
2017-04-29 21:35:46 +08:00
|
|
|
'name': name,
|
|
|
|
'module': 'geodjango_%s' % name.replace('-', '_'), # JS-safe
|
|
|
|
'serialized': self.serialize(value),
|
2020-11-26 06:36:35 +08:00
|
|
|
'geom_type': 'Geometry' if geom_type == 'Unknown' else geom_type,
|
2017-04-29 21:35:46 +08:00
|
|
|
'STATIC_URL': settings.STATIC_URL,
|
|
|
|
'LANGUAGE_BIDI': translation.get_language_bidi(),
|
2017-12-11 20:08:45 +08:00
|
|
|
**(attrs or {}),
|
|
|
|
}))
|
2016-12-28 06:00:56 +08:00
|
|
|
return context
|
2013-03-16 18:39:18 +08:00
|
|
|
|
|
|
|
|
|
|
|
class OpenLayersWidget(BaseGeometryWidget):
|
|
|
|
template_name = 'gis/openlayers.html'
|
2017-03-17 21:57:42 +08:00
|
|
|
map_srid = 3857
|
2013-10-22 18:21:07 +08:00
|
|
|
|
2013-03-16 18:39:18 +08:00
|
|
|
class Media:
|
2015-05-25 23:31:26 +08:00
|
|
|
css = {
|
|
|
|
'all': (
|
2017-12-22 23:58:32 +08:00
|
|
|
'https://cdnjs.cloudflare.com/ajax/libs/ol3/4.6.5/ol.css',
|
2015-05-25 23:31:26 +08:00
|
|
|
'gis/css/ol3.css',
|
|
|
|
)
|
|
|
|
}
|
2013-03-16 18:39:18 +08:00
|
|
|
js = (
|
2017-12-22 23:58:32 +08:00
|
|
|
'https://cdnjs.cloudflare.com/ajax/libs/ol3/4.6.5/ol.js',
|
2013-03-16 18:39:18 +08:00
|
|
|
'gis/js/OLMapWidget.js',
|
|
|
|
)
|
|
|
|
|
2015-05-25 23:31:26 +08:00
|
|
|
def serialize(self, value):
|
|
|
|
return value.json if value else ''
|
|
|
|
|
2018-02-06 18:51:41 +08:00
|
|
|
def deserialize(self, value):
|
|
|
|
geom = super().deserialize(value)
|
|
|
|
# GeoJSON assumes WGS84 (4326). Use the map's SRID instead.
|
|
|
|
if geom and json_regex.match(value) and self.map_srid != 4326:
|
|
|
|
geom.srid = self.map_srid
|
|
|
|
return geom
|
|
|
|
|
2013-03-16 18:39:18 +08:00
|
|
|
|
2015-05-25 23:31:26 +08:00
|
|
|
class OSMWidget(OpenLayersWidget):
|
2013-03-16 18:39:18 +08:00
|
|
|
"""
|
|
|
|
An OpenLayers/OpenStreetMap-based widget.
|
|
|
|
"""
|
|
|
|
template_name = 'gis/openlayers-osm.html'
|
|
|
|
default_lon = 5
|
|
|
|
default_lat = 47
|
2017-05-15 02:31:17 +08:00
|
|
|
default_zoom = 12
|
2013-03-16 18:39:18 +08:00
|
|
|
|
2013-12-13 22:05:45 +08:00
|
|
|
def __init__(self, attrs=None):
|
2017-01-21 21:13:44 +08:00
|
|
|
super().__init__()
|
2017-05-15 02:31:17 +08:00
|
|
|
for key in ('default_lon', 'default_lat', 'default_zoom'):
|
2013-12-13 22:05:45 +08:00
|
|
|
self.attrs[key] = getattr(self, key)
|
|
|
|
if attrs:
|
|
|
|
self.attrs.update(attrs)
|