| 1 |
import logging |
| 2 |
|
| 3 |
from django.conf import settings |
| 4 |
from django.contrib.gis import gdal |
| 5 |
from django.contrib.gis.geometry import json_regex |
| 6 |
from django.contrib.gis.geos import GEOSException, GEOSGeometry |
| 7 |
from django.forms.widgets import Widget |
| 8 |
from django.utils import translation |
| 9 |
|
| 10 |
logger = logging.getLogger("django.contrib.gis") |
| 11 |
|
| 12 |
|
| 13 |
class BaseGeometryWidget(Widget): |
| 14 |
""" |
| 15 |
The base class for rich geometry widgets. |
| 16 |
Render a map using the WKT of the geometry. |
| 17 |
""" |
| 18 |
|
| 19 |
geom_type = "GEOMETRY" |
| 20 |
map_srid = 4326 |
| 21 |
display_raw = False |
| 22 |
|
| 23 |
supports_3d = False |
| 24 |
template_name = "" # set on subclasses |
| 25 |
|
| 26 |
def __init__(self, attrs=None): |
| 27 |
self.attrs = {} |
| 28 |
for key in ("geom_type", "map_srid", "display_raw"): |
| 29 |
self.attrs[key] = getattr(self, key) |
| 30 |
if attrs: |
| 31 |
self.attrs.update(attrs) |
| 32 |
|
| 33 |
def serialize(self, value): |
| 34 |
return value.wkt if value else "" |
| 35 |
|
| 36 |
def deserialize(self, value): |
| 37 |
try: |
| 38 |
return GEOSGeometry(value) |
| 39 |
except (GEOSException, ValueError, TypeError) as err: |
| 40 |
logger.error("Error creating geometry from value '%s' (%s)", value, err) |
| 41 |
return None |
| 42 |
|
| 43 |
def get_context(self, name, value, attrs): |
| 44 |
context = super().get_context(name, value, attrs) |
| 45 |
# If a string reaches here (via a validation error on another |
| 46 |
# field) then just reconstruct the Geometry. |
| 47 |
if value and isinstance(value, str): |
| 48 |
value = self.deserialize(value) |
| 49 |
|
| 50 |
if value: |
| 51 |
# Check that srid of value and map match |
| 52 |
if value.srid and value.srid != self.map_srid: |
| 53 |
try: |
| 54 |
ogr = value.ogr |
| 55 |
ogr.transform(self.map_srid) |
| 56 |
value = ogr |
| 57 |
except gdal.GDALException as err: |
| 58 |
logger.error( |
| 59 |
"Error transforming geometry from srid '%s' to srid '%s' (%s)", |
| 60 |
value.srid, |
| 61 |
self.map_srid, |
| 62 |
err, |
| 63 |
) |
| 64 |
|
| 65 |
geom_type = gdal.OGRGeomType(self.attrs["geom_type"]).name |
| 66 |
context.update( |
| 67 |
self.build_attrs( |
| 68 |
self.attrs, |
| 69 |
{ |
| 70 |
"name": name, |
| 71 |
"module": "geodjango_%s" % name.replace("-", "_"), # JS-safe |
| 72 |
"serialized": self.serialize(value), |
| 73 |
"geom_type": "Geometry" if geom_type == "Unknown" else geom_type, |
| 74 |
"STATIC_URL": settings.STATIC_URL, |
| 75 |
"LANGUAGE_BIDI": translation.get_language_bidi(), |
| 76 |
**(attrs or {}), |
| 77 |
}, |
| 78 |
) |
| 79 |
) |
| 80 |
return context |
| 81 |
|
| 82 |
|
| 83 |
class OpenLayersWidget(BaseGeometryWidget): |
| 84 |
template_name = "gis/openlayers.html" |
| 85 |
map_srid = 3857 |
| 86 |
|
| 87 |
class Media: |
| 88 |
css = { |
| 89 |
"all": ( |
| 90 |
"https://cdn.jsdelivr.net/npm/[email protected]/ol.css", |
| 91 |
"gis/css/ol3.css", |
| 92 |
) |
| 93 |
} |
| 94 |
js = ( |
| 95 |
"https://cdn.jsdelivr.net/npm/[email protected]/dist/ol.js", |
| 96 |
"gis/js/OLMapWidget.js", |
| 97 |
) |
| 98 |
|
| 99 |
def serialize(self, value): |
| 100 |
return value.json if value else "" |
| 101 |
|
| 102 |
def deserialize(self, value): |
| 103 |
geom = super().deserialize(value) |
| 104 |
# GeoJSON assumes WGS84 (4326). Use the map's SRID instead. |
| 105 |
if geom and json_regex.match(value) and self.map_srid != 4326: |
| 106 |
geom.srid = self.map_srid |
| 107 |
return geom |
| 108 |
|
| 109 |
|
| 110 |
class OSMWidget(OpenLayersWidget): |
| 111 |
""" |
| 112 |
An OpenLayers/OpenStreetMap-based widget. |
| 113 |
""" |
| 114 |
|
| 115 |
template_name = "gis/openlayers-osm.html" |
| 116 |
default_lon = 5 |
| 117 |
default_lat = 47 |
| 118 |
default_zoom = 12 |
| 119 |
|
| 120 |
def __init__(self, attrs=None): |
| 121 |
super().__init__() |
| 122 |
for key in ("default_lon", "default_lat", "default_zoom"): |
| 123 |
self.attrs[key] = getattr(self, key) |
| 124 |
if attrs: |
| 125 |
self.attrs.update(attrs) |