/
geonode.py
568 lines (515 loc) · 27.2 KB
/
geonode.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
# -*- coding: utf-8 -*-
#########################################################################
#
# Copyright 2018, GeoSolutions Sas.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
#
#########################################################################
from __future__ import absolute_import, unicode_literals
from urllib import parse
from six import string_types
try:
import json
except ImportError:
from django.utils import simplejson as json
import logging
import traceback
from ..utils import (
GoogleZoom,
get_wfs_endpoint,
get_valid_number,
to_json)
from ..settings import (
MAP_BASELAYERS,
CATALOGUE_SERVICES,
CATALOGUE_SELECTED_SERVICE)
from ..converters import BaseMapStore2ConfigConverter
from django.contrib.gis.geos import Polygon
from django.contrib.gis.gdal import SpatialReference, CoordTransform
from django.core.serializers.json import DjangoJSONEncoder
from django.conf import settings
logger = logging.getLogger(__name__)
unsafe_chars = {
'&': '\\u0026',
'<': '\\u003c',
'>': '\\u003e',
'\u2028': '\\u2028',
'\u2029': '\\u2029'
}
class GeoNodeMapStore2ConfigConverter(BaseMapStore2ConfigConverter):
def convert(self, viewer, request):
"""
input: GeoNode JSON Gxp Config
output: MapStore2 compliant str(config)
"""
# Initialization
viewer_obj = json.loads(viewer)
map_id = None
if 'id' in viewer_obj and viewer_obj['id']:
try:
map_id = int(viewer_obj['id'])
except Exception:
pass
data = {}
data['version'] = 2
# Map Definition
try:
# Map Definition
ms2_map = {}
ms2_map['projection'] = viewer_obj['map']['projection']
ms2_map['units'] = viewer_obj['map']['units']
ms2_map['zoom'] = viewer_obj['map']['zoom'] if viewer_obj['map']['zoom'] > 0 else 2
ms2_map['maxExtent'] = viewer_obj['map']['maxExtent']
ms2_map['maxResolution'] = viewer_obj['map']['maxResolution']
# Backgrouns
backgrounds = self.getBackgrounds(viewer, MAP_BASELAYERS)
if backgrounds:
ms2_map['layers'] = backgrounds
else:
ms2_map['layers'] = MAP_BASELAYERS + [
# TODO: covnert Viewer Background Layers
# Add here more backgrounds e.g.:
# {
# "type": "wms",
# "url": "https://demo.geo-solutions.it/geoserver/wms",
# "visibility": True,
# "opacity": 0.5,
# "title": "Weather data",
# "name": "nurc:Arc_Sample",
# "group": "Meteo",
# "format": "image/png",
# "bbox": {
# "bounds": {
# "minx": -25.6640625,
# "miny": 26.194876675795218,
# "maxx": 48.1640625,
# "maxy": 56.80087831233043
# },
# "crs": "EPSG:4326"
# }
# }, ...
]
if settings.BING_API_KEY:
ms2_map['bingApiKey'] = settings.BING_API_KEY
# Security Info
info = {}
info['canDelete'] = False
info['canEdit'] = False
info['description'] = viewer_obj['about']['abstract']
info['id'] = map_id
info['name'] = viewer_obj['about']['title']
ms2_map['info'] = info
# Overlays
overlays, selected = self.get_overlays(viewer, request=request)
if selected and 'name' in selected and selected['name'] and not map_id:
# We are generating a Layer Details View
center, zoom = self.get_center_and_zoom(viewer_obj['map'], selected)
ms2_map['center'] = center
ms2_map['zoom'] = zoom
try:
# - extract from GeoNode guardian
from geonode.layers.views import (_resolve_layer,
_PERMISSION_MSG_MODIFY,
_PERMISSION_MSG_DELETE)
if _resolve_layer(request,
selected['name'],
'base.change_resourcebase',
_PERMISSION_MSG_MODIFY):
info['canEdit'] = True
if _resolve_layer(request,
selected['name'],
'base.delete_resourcebase',
_PERMISSION_MSG_DELETE):
info['canDelete'] = True
except Exception:
tb = traceback.format_exc()
logger.debug(tb)
else:
# We are getting the configuration of a Map
# On GeoNode model the Map Center is always saved in 4326
_x = get_valid_number(viewer_obj['map']['center'][0])
_y = get_valid_number(viewer_obj['map']['center'][1])
_crs = 'EPSG:4326'
if _x > 360.0 or _x < -360.0:
_crs = viewer_obj['map']['projection']
ms2_map['center'] = {
'x': _x,
'y': _y,
'crs': _crs
}
try:
# - extract from GeoNode guardian
from geonode.maps.views import (_resolve_map,
_PERMISSION_MSG_SAVE,
_PERMISSION_MSG_DELETE)
if _resolve_map(request,
str(map_id),
'base.change_resourcebase',
_PERMISSION_MSG_SAVE):
info['canEdit'] = True
if _resolve_map(request,
str(map_id),
'base.delete_resourcebase',
_PERMISSION_MSG_DELETE):
info['canDelete'] = True
except Exception:
tb = traceback.format_exc()
logger.debug(tb)
for overlay in overlays:
if 'name' in overlay and overlay['name']:
ms2_map['layers'].append(overlay)
data['map'] = ms2_map
except Exception:
# traceback.print_exc()
tb = traceback.format_exc()
logger.debug(tb)
# Additional Configurations
if map_id:
from mapstore2_adapter import fixup_map
from mapstore2_adapter.api.models import MapStoreResource
try:
fixup_map(map_id)
ms2_resource = MapStoreResource.objects.get(id=map_id)
ms2_map_data = ms2_resource.data.blob
if isinstance(ms2_map_data, string_types):
ms2_map_data = json.loads(ms2_map_data)
if 'map' in ms2_map_data:
for _k, _v in ms2_map_data['map'].items():
if _k not in data['map']:
data['map'][_k] = ms2_map_data['map'][_k]
del ms2_map_data['map']
data.update(ms2_map_data)
except Exception:
# traceback.print_exc()
tb = traceback.format_exc()
logger.debug(tb)
# Default Catalogue Services Definition
try:
ms2_catalogue = {}
ms2_catalogue['selectedService'] = CATALOGUE_SELECTED_SERVICE
ms2_catalogue['services'] = CATALOGUE_SERVICES
data['catalogServices'] = ms2_catalogue
except Exception:
# traceback.print_exc()
tb = traceback.format_exc()
logger.debug(tb)
json_str = json.dumps(data, cls=DjangoJSONEncoder, sort_keys=True)
for (c, d) in unsafe_chars.items():
json_str = json_str.replace(c, d)
return json_str
def getBackgrounds(self, viewer, defaults):
import copy
backgrounds = copy.deepcopy(defaults)
def_background = None
for bg in backgrounds:
if bg['visibility']:
def_background = bg
break
try:
viewer_obj = json.loads(viewer)
layers = viewer_obj['map']['layers']
for bg in backgrounds:
bg['visibility'] = False
any_visible = False
for layer in layers:
if 'group' in layer and layer['group'] == "background" and layer['visibility']:
def_local_background = [bg for bg in backgrounds if bg['name'] == layer['name']]
def_background = def_local_background[0] if def_local_background else None
if def_background:
def_background['opacity'] = layer['opacity'] if 'opacity' in layer else 1.0
def_background['visibility'] = True
any_visible = True
break
if any_visible and def_background:
for bg in backgrounds:
if bg['name'] == def_background['name']:
bg['visibility'] = True
break
else:
backgrounds = copy.deepcopy(defaults)
except Exception:
# traceback.print_exc()
backgrounds = copy.copy(defaults)
tb = traceback.format_exc()
logger.debug(tb)
return backgrounds
def get_overlays(self, viewer, request=None):
overlays = []
selected = None
try:
viewer_obj = json.loads(viewer)
layers = viewer_obj['map']['layers']
sources = viewer_obj['sources']
for layer in layers:
if 'group' not in layer or layer['group'] != "background":
source = sources[layer['source']]
overlay = {}
if 'url' in source:
overlay['type'] = "wms" if 'ptype' not in source or \
source['ptype'] != 'gxp_arcrestsource' else 'arcgis'
_p_url = parse.urlparse(source['url'])
if _p_url.query:
overlay['params'] = dict(parse.parse_qsl(_p_url.query))
overlay['url'] = source['url']
overlay['visibility'] = layer['visibility'] if 'visibility' in layer else True
overlay['singleTile'] = layer['singleTile'] if 'singleTile' in layer else False
overlay['selected'] = layer['selected'] if 'selected' in layer else False
overlay['hidden'] = layer['hidden'] if 'hidden' in layer else False
overlay['handleClickOnLayer'] = layer['handleClickOnLayer'] if \
'handleClickOnLayer' in layer else False
overlay['wrapDateLine'] = layer['wrapDateLine'] if 'wrapDateLine' in layer else False
overlay['hideLoading'] = layer['hideLoading'] if 'hideLoading' in layer else False
overlay['useForElevation'] = layer['useForElevation'] if 'useForElevation' in layer else False
overlay['fixed'] = layer['fixed'] if 'fixed' in layer else False
overlay['opacity'] = layer['opacity'] if 'opacity' in layer else 1.0
overlay['title'] = layer['title'] if 'title' in layer else ''
overlay['name'] = layer['name'] if 'name' in layer else ''
overlay['store'] = layer['store'] if 'store' in layer else ''
overlay['group'] = layer['group'] if 'group' in layer else ''
overlay['format'] = layer['format'] if 'format' in layer else "image/png"
overlay['bbox'] = {}
if 'dimensions' in layer:
overlay['dimensions'] = layer['dimensions']
if 'search' in layer:
overlay['search'] = layer['search']
if 'style' in layer:
overlay['style'] = layer['style']
if 'styles' in layer:
overlay['styles'] = layer['styles']
if 'layerFilter' in layer:
overlay['layerFilter'] = layer['layerFilter']
if 'capability' in layer:
capa = layer['capability']
if 'store' in capa:
overlay['store'] = capa['store']
if 'styles' in capa:
overlay['styles'] = capa['styles']
if 'style' in capa:
overlay['style'] = capa['style']
if 'abstract' in capa:
overlay['abstract'] = capa['abstract']
if 'attribution' in capa:
overlay['attribution'] = capa['attribution']
if 'keywords' in capa:
overlay['keywords'] = capa['keywords']
if 'dimensions' in capa and capa['dimensions']:
overlay['dimensions'] = self.get_layer_dimensions(dimensions=capa['dimensions'])
if 'storeType' in capa and capa['storeType'] == 'dataStore':
overlay['search'] = {
"url": get_wfs_endpoint(request),
"type": "wfs"
}
if 'llbbox' in capa:
bbox = capa['llbbox']
# Must be in the form xmin, ymin, xmax, ymax
llbbox = [
get_valid_number(bbox[0]),
get_valid_number(bbox[2]),
get_valid_number(bbox[1]),
get_valid_number(bbox[3]),
]
overlay['llbbox'] = llbbox
overlay['bbox']['bounds'] = {
"minx": llbbox[0],
"miny": llbbox[1],
"maxx": llbbox[2],
"maxy": llbbox[3]
}
overlay['bbox']['crs'] = 'EPSG:4326'
elif 'bbox' in capa:
bbox = capa['bbox']
if viewer_obj['map']['projection'] in bbox:
proj = viewer_obj['map']['projection']
bbox = capa['bbox'][proj]
overlay['bbox']['bounds'] = {
"minx": get_valid_number(bbox['bbox'][0]),
"miny": get_valid_number(bbox['bbox'][1]),
"maxx": get_valid_number(bbox['bbox'][2]),
"maxy": get_valid_number(bbox['bbox'][3])
}
overlay['bbox']['crs'] = bbox['srs']
if 'nativeCrs' in layer:
overlay['nativeCrs'] = layer['nativeCrs']
else:
try:
from geonode.layers.models import Layer
_gn_layer = Layer.objects.get(
store=overlay['store'],
alternate=overlay['name'])
if _gn_layer.srid:
overlay['nativeCrs'] = _gn_layer.srid
except Exception:
tb = traceback.format_exc()
logger.debug(tb)
if 'bbox' in layer and not overlay['bbox']:
if 'bounds' in layer['bbox']:
overlay['bbox'] = layer['bbox']
else:
overlay['bbox']['bounds'] = {
"minx": get_valid_number(layer['bbox'][0],
default=layer['bbox'][2],
complementar=True),
"miny": get_valid_number(layer['bbox'][1],
default=layer['bbox'][3],
complementar=True),
"maxx": get_valid_number(layer['bbox'][2],
default=layer['bbox'][0],
complementar=True),
"maxy": get_valid_number(layer['bbox'][3],
default=layer['bbox'][1],
complementar=True)
}
overlay['bbox']['crs'] = layer['srs'] if 'srs' in layer else \
viewer_obj['map']['projection']
if 'ftInfoTemplate' in layer and layer['ftInfoTemplate']:
featureInfo = {'format': 'TEMPLATE'}
featureInfo['template'] = layer['ftInfoTemplate']
overlay['featureInfo'] = featureInfo
elif 'getFeatureInfo' in layer and layer['getFeatureInfo']:
if 'fields' in layer['getFeatureInfo'] and layer['getFeatureInfo']['fields'] and \
'propertyNames' in layer['getFeatureInfo'] and \
layer['getFeatureInfo']['propertyNames']:
fields = layer['getFeatureInfo']['fields']
propertyNames = layer['getFeatureInfo']['propertyNames']
displayTypes = layer['getFeatureInfo']['displayTypes'] if 'displayTypes' in layer['getFeatureInfo'] else dict()
featureInfo = {'format': 'TEMPLATE'}
_template = '<div>'
for _field in fields:
_label = propertyNames[_field] if propertyNames[_field] else _field
_template += '<div class="row">'
if _field in displayTypes and displayTypes[_field] == 'type_href':
_template += '<div class="col-xs-6" style="font-weight: bold; word-wrap: break-word;">%s:</div> \
<div class="col-xs-6" style="word-wrap: break-word;"><a href="${properties.%s}" target="_new">${properties.%s}</a></div>' % \
(_label, _field, _field)
elif _field in displayTypes and displayTypes[_field] == 'type_image':
_template += '<div class="col-xs-12" align="center" style="font-weight: bold; word-wrap: break-word;"> \
<a href="${properties.%s}" target="_new"><img width="100%%" height="auto" src="${properties.%s}" title="%s" alt="%s"/></a></div>' % \
(_field, _field, _label, _label)
elif _field in displayTypes and 'type_video' in displayTypes[_field]:
if 'youtube' in displayTypes[_field]:
_template += '<div class="col-xs-12" align="center" style="font-weight: bold; word-wrap: break-word;"> \
<iframe src="${properties.%s}" width="100%%" height="360" frameborder="0" allowfullscreen></iframe></div>' % \
(_field)
else:
_type = "video/%s" % (displayTypes[_field][11:])
_template += '<div class="col-xs-12" align="center" style="font-weight: bold; word-wrap: break-word;"> \
<video width="100%%" height="360" controls><source src="${properties.%s}" type="%s">Your browser does not support the video tag.</video></div>' % \
(_field, _type)
elif _field in displayTypes and displayTypes[_field] == 'type_audio':
_template += '<div class="col-xs-12" align="center" style="font-weight: bold; word-wrap: break-word;"> \
<audio controls><source src="${properties.%s}" type="audio/mpeg">Your browser does not support the audio element.</audio></div>' % \
(_field)
elif _field in displayTypes and displayTypes[_field] == 'type_iframe':
_template += '<div class="col-xs-12" align="center" style="font-weight: bold; word-wrap: break-word;"> \
<iframe src="/proxy/?url=${properties.%s}" width="100%%" height="360" frameborder="0" allowfullscreen></iframe></div>' % \
(_field)
else:
_template += '<div class="col-xs-6" style="font-weight: bold; word-wrap: break-word;">%s:</div> \
<div class="col-xs-6" style="word-wrap: break-word;">${properties.%s}</div>' % \
(propertyNames[_field] if propertyNames[_field] else _field, _field)
_template += '</div>'
_template += '</div>'
featureInfo['template'] = _template
overlay['featureInfo'] = featureInfo
# Push extraParams into GeoNode layerParams
if 'extraParams' in layer and layer['extraParams']:
overlay['extraParams'] = layer['extraParams']
elif 'name' in layer and layer['name'] == 'Annotations':
overlay = layer
# Restore the id of ms2 layer
if "extraParams" in layer and "msId" in layer["extraParams"]:
overlay["id"] = layer["extraParams"]["msId"]
overlays.append(overlay)
if not selected or ('selected' in layer and layer['selected']):
selected = overlay
except Exception:
tb = traceback.format_exc()
logger.debug(tb)
return (overlays, selected)
def get_layer_dimensions(self, dimensions):
url = getattr(settings, "GEOSERVER_PUBLIC_LOCATION", "")
if url.endswith('ows'):
url = url[:-3]
url += "gwc/service/wmts"
dim = []
for attr, value in dimensions.items():
if attr == "time":
nVal = {"name": attr, "source": {"type": "multidim-extension", "url": url}}
dim.append(nVal)
else:
value["name"] = attr
dim.append(value)
return dim
def get_center_and_zoom(self, view_map, overlay):
center = {
"x": get_valid_number(
overlay['bbox']['bounds']['minx'] + (
overlay['bbox']['bounds']['maxx'] - overlay['bbox']['bounds']['minx']
) / 2),
"y": get_valid_number(
overlay['bbox']['bounds']['miny'] + (
overlay['bbox']['bounds']['maxy'] - overlay['bbox']['bounds']['miny']
) / 2),
"crs": overlay['bbox']['crs']
}
zoom = view_map['zoom']
# max_extent = view_map['maxExtent']
# map_crs = view_map['projection']
ov_bbox = [get_valid_number(overlay['bbox']['bounds']['minx']),
get_valid_number(overlay['bbox']['bounds']['miny']),
get_valid_number(overlay['bbox']['bounds']['maxx']),
get_valid_number(overlay['bbox']['bounds']['maxy']), ]
ov_crs = overlay['bbox']['crs']
(center_m, zoom_m) = self.project_to_WGS84(ov_bbox, ov_crs, center=None)
if center_m is not None and zoom_m is not None:
zoom_m = zoom_m if zoom_m > 0 else 1
return (center_m, zoom_m)
else:
return (center, zoom)
def project_to_WGS84(self, ov_bbox, ov_crs, center=None):
try:
srid = int(ov_crs.split(':')[1])
srid = 3857 if srid == 900913 else srid
poly = Polygon((
(ov_bbox[0], ov_bbox[1]),
(ov_bbox[0], ov_bbox[3]),
(ov_bbox[2], ov_bbox[3]),
(ov_bbox[2], ov_bbox[1]),
(ov_bbox[0], ov_bbox[1])), srid=srid)
if srid != 4326:
gcoord = SpatialReference(4326)
ycoord = SpatialReference(srid)
trans = CoordTransform(ycoord, gcoord)
poly.transform(trans)
try:
if not center:
center = {
"x": get_valid_number(poly.centroid.coords[0]),
"y": get_valid_number(poly.centroid.coords[1]),
"crs": "EPSG:4326"
}
zoom = GoogleZoom().get_zoom(poly) + 1
except Exception:
center = (0, 0)
zoom = 0
tb = traceback.format_exc()
logger.debug(tb)
except Exception:
tb = traceback.format_exc()
logger.debug(tb)
return (center, zoom)
def viewer_json(self, viewer, request):
"""
input: MapStore2 compliant str(config)
output: GeoNode JSON Gxp Config
"""
# MapStore uses x0,y0,x1,y1 ordering of bbox coords
viewer = to_json(viewer)
if viewer.get('map', None) and viewer['map'].get('bbox', None):
ms2_bbox = viewer['map'].get('bbox')
config_bbox = [ms2_bbox[0], ms2_bbox[2], ms2_bbox[1], ms2_bbox[3]]
viewer['map']['bbox'] = config_bbox
return viewer