-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
678 lines (564 loc) · 20.9 KB
/
utils.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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
# -*- coding: utf-8 -*-
#########################################################################
#
# Copyright (C) 2012 OpenPlans
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#########################################################################
import os
import httplib2
import base64
import math
import copy
import string
import datetime
import re
from osgeo import ogr
from slugify import Slugify
from django.conf import settings
from django.core.exceptions import PermissionDenied
from django.utils.translation import ugettext_lazy as _
from django.shortcuts import get_object_or_404
from django.utils import simplejson as json
from django.http import HttpResponse
from django.core.cache import cache
from django.http import Http404
DEFAULT_TITLE = ""
DEFAULT_ABSTRACT = ""
INVALID_PERMISSION_MESSAGE = _("Invalid permission level.")
ALPHABET = string.ascii_uppercase + string.ascii_lowercase + \
string.digits + '-_'
ALPHABET_REVERSE = dict((c, i) for (i, c) in enumerate(ALPHABET))
BASE = len(ALPHABET)
SIGN_CHARACTER = '$'
http_client = httplib2.Http()
custom_slugify = Slugify(separator='_')
def _get_basic_auth_info(request):
"""
grab basic auth info
"""
meth, auth = request.META['HTTP_AUTHORIZATION'].split()
if meth.lower() != 'basic':
raise ValueError
username, password = base64.b64decode(auth).split(':')
return username, password
def batch_permissions(request):
# TODO
pass
def batch_delete(request):
# TODO
pass
def _split_query(query):
"""
split and strip keywords, preserve space
separated quoted blocks.
"""
qq = query.split(' ')
keywords = []
accum = None
for kw in qq:
if accum is None:
if kw.startswith('"'):
accum = kw[1:]
elif kw:
keywords.append(kw)
else:
accum += ' ' + kw
if kw.endswith('"'):
keywords.append(accum[0:-1])
accum = None
if accum is not None:
keywords.append(accum)
return [kw.strip() for kw in keywords if kw.strip()]
def bbox_to_wkt(x0, x1, y0, y1, srid="4326"):
if None not in [x0, x1, y0, y1]:
wkt = 'SRID=%s;POLYGON((%s %s,%s %s,%s %s,%s %s,%s %s))' % (
srid, x0, y0, x0, y1, x1, y1, x1, y0, x0, y0)
else:
wkt = 'SRID=4326;POLYGON((-180 -90,-180 90,180 90,180 -90,-180 -90))'
return wkt
def llbbox_to_mercator(llbbox):
minlonlat = forward_mercator([llbbox[0], llbbox[1]])
maxlonlat = forward_mercator([llbbox[2], llbbox[3]])
return [minlonlat[0], minlonlat[1], maxlonlat[0], maxlonlat[1]]
def mercator_to_llbbox(bbox):
minlonlat = inverse_mercator([bbox[0], bbox[1]])
maxlonlat = inverse_mercator([bbox[2], bbox[3]])
return [minlonlat[0], minlonlat[1], maxlonlat[0], maxlonlat[1]]
def forward_mercator(lonlat):
"""
Given geographic coordinates, return a x,y tuple in spherical mercator.
If the lat value is out of range, -inf will be returned as the y value
"""
x = lonlat[0] * 20037508.34 / 180
try:
# With data sets that only have one point the value of this
# expression becomes negative infinity. In order to continue,
# we wrap this in a try catch block.
n = math.tan((90 + lonlat[1]) * math.pi / 360)
except ValueError:
n = 0
if n <= 0:
y = float("-inf")
else:
y = math.log(n) / math.pi * 20037508.34
return (x, y)
def inverse_mercator(xy):
"""
Given coordinates in spherical mercator, return a lon,lat tuple.
"""
lon = (xy[0] / 20037508.34) * 180
lat = (xy[1] / 20037508.34) * 180
lat = 180 / math.pi * \
(2 * math.atan(math.exp(lat * math.pi / 180)) - math.pi / 2)
return (lon, lat)
def layer_from_viewer_config(model, layer, source, ordering):
"""
Parse an object out of a parsed layer configuration from a GXP
viewer.
``model`` is the type to instantiate
``layer`` is the parsed dict for the layer
``source`` is the parsed dict for the layer's source
``ordering`` is the index of the layer within the map's layer list
"""
layer_cfg = dict(layer)
for k in ["format", "name", "opacity", "styles", "transparent",
"fixed", "group", "visibility", "source", "getFeatureInfo"]:
if k in layer_cfg:
del layer_cfg[k]
source_cfg = dict(source)
for k in ["url", "projection"]:
if k in source_cfg:
del source_cfg[k]
return model(
stack_order=ordering,
format=layer.get("format", None),
name=layer.get("name", None),
opacity=layer.get("opacity", 1),
styles=layer.get("styles", None),
transparent=layer.get("transparent", False),
fixed=layer.get("fixed", False),
group=layer.get('group', None),
visibility=layer.get("visibility", True),
ows_url=source.get("url", None),
layer_params=json.dumps(layer_cfg),
source_params=json.dumps(source_cfg)
)
class GXPMapBase(object):
def viewer_json(self, user, *added_layers):
"""
Convert this map to a nested dictionary structure matching the JSON
configuration for GXP Viewers.
The ``added_layers`` parameter list allows a list of extra MapLayer
instances to append to the Map's layer list when generating the
configuration. These are not persisted; if you want to add layers you
should use ``.layer_set.create()``.
"""
if self.id and len(added_layers) == 0:
cfg = cache.get("viewer_json_" +
str(self.id) +
"_" +
str(0 if user is None else user.id))
if cfg is not None:
return cfg
layers = list(self.layers)
layers.extend(added_layers)
server_lookup = {}
sources = {}
def uniqify(seq):
"""
get a list of unique items from the input sequence.
This relies only on equality tests, so you can use it on most
things. If you have a sequence of hashables, list(set(seq)) is
better.
"""
results = []
for x in seq:
if x not in results:
results.append(x)
return results
configs = [l.source_config() for l in layers]
i = 0
for source in uniqify(configs):
while str(i) in sources:
i = i + 1
sources[str(i)] = source
server_lookup[json.dumps(source)] = str(i)
def source_lookup(source):
for k, v in sources.iteritems():
if v == source:
return k
return None
def layer_config(l, user=None):
cfg = l.layer_config(user=user)
src_cfg = l.source_config()
source = source_lookup(src_cfg)
if source:
cfg["source"] = source
return cfg
source_urls = [source['url']
for source in sources.values() if 'url' in source]
if 'geonode.geoserver' in settings.INSTALLED_APPS:
if not settings.MAP_BASELAYERS[0]['source']['url'] in source_urls:
keys = sorted(sources.keys())
settings.MAP_BASELAYERS[0]['source'][
'title'] = 'Local Geoserver'
sources[
str(int(keys[-1]) + 1)] = settings.MAP_BASELAYERS[0]['source']
def _base_source(source):
base_source = copy.deepcopy(source)
for key in ["id", "baseParams", "title"]:
if key in base_source:
del base_source[key]
return base_source
for idx, lyr in enumerate(settings.MAP_BASELAYERS):
if _base_source(
lyr["source"]) not in map(
_base_source,
sources.values()):
sources[
str(int(max(sources.keys(), key=int)) + 1)] = lyr["source"]
# adding remote services sources
from geonode.services.models import Service
index = int(max(sources.keys()))
for service in Service.objects.all():
remote_source = {
'url': service.base_url,
'remote': True,
'ptype': 'gxp_wmscsource',
'name': service.name
}
index += 1
sources[index] = remote_source
config = {
'id': self.id,
'about': {
'title': self.title,
'abstract': self.abstract
},
'defaultSourceType': "gxp_wmscsource",
'sources': sources,
'map': {
'layers': [layer_config(l, user=user) for l in layers],
'center': [self.center_x, self.center_y],
'projection': self.projection,
'zoom': self.zoom
}
}
if any(layers):
# Mark the last added layer as selected - important for data page
config["map"]["layers"][len(layers) - 1]["selected"] = True
config["map"].update(_get_viewer_projection_info(self.projection))
# Create user-specific cache of maplayer config
if self is not None:
cache.set("viewer_json_" +
str(self.id) +
"_" +
str(0 if user is None else user.id), config)
return config
class GXPMap(GXPMapBase):
def __init__(self, projection=None, title=None, abstract=None,
center_x=None, center_y=None, zoom=None):
self.id = 0
self.projection = projection
self.title = title or DEFAULT_TITLE
self.abstract = abstract or DEFAULT_ABSTRACT
_DEFAULT_MAP_CENTER = forward_mercator(settings.DEFAULT_MAP_CENTER)
self.center_x = center_x if center_x is not None else _DEFAULT_MAP_CENTER[
0]
self.center_y = center_y if center_y is not None else _DEFAULT_MAP_CENTER[
1]
self.zoom = zoom if zoom is not None else settings.DEFAULT_MAP_ZOOM
self.layers = []
class GXPLayerBase(object):
def source_config(self):
"""
Generate a dict that can be serialized to a GXP layer source
configuration suitable for loading this layer.
"""
try:
cfg = json.loads(self.source_params)
except Exception:
cfg = dict(ptype="gxp_wmscsource", restUrl="/gs/rest")
if self.ows_url:
cfg["url"] = self.ows_url
return cfg
def layer_config(self, user=None):
"""
Generate a dict that can be serialized to a GXP layer configuration
suitable for loading this layer.
The "source" property will be left unset; the layer is not aware of the
name assigned to its source plugin. See
geonode.maps.models.Map.viewer_json for an example of
generating a full map configuration.
"""
try:
cfg = json.loads(self.layer_params)
except Exception:
cfg = dict()
if self.format:
cfg['format'] = self.format
if self.name:
cfg["name"] = self.name
if self.opacity:
cfg['opacity'] = self.opacity
if self.styles:
cfg['styles'] = self.styles
if self.transparent:
cfg['transparent'] = True
cfg["fixed"] = self.fixed
if self.group:
cfg["group"] = self.group
cfg["visibility"] = self.visibility
return cfg
class GXPLayer(GXPLayerBase):
'''GXPLayer represents an object to be included in a GXP map.
'''
def __init__(self, name=None, ows_url=None, **kw):
self.format = None
self.name = name
self.opacity = 1.0
self.styles = None
self.transparent = False
self.fixed = False
self.group = None
self.visibility = True
self.ows_url = ows_url
self.layer_params = ""
self.source_params = ""
for k in kw:
setattr(self, k, kw[k])
def default_map_config():
_DEFAULT_MAP_CENTER = forward_mercator(settings.DEFAULT_MAP_CENTER)
_default_map = GXPMap(
title=DEFAULT_TITLE,
abstract=DEFAULT_ABSTRACT,
projection="EPSG:900913",
center_x=_DEFAULT_MAP_CENTER[0],
center_y=_DEFAULT_MAP_CENTER[1],
zoom=settings.DEFAULT_MAP_ZOOM
)
def _baselayer(lyr, order):
return layer_from_viewer_config(
GXPLayer,
layer=lyr,
source=lyr["source"],
ordering=order
)
DEFAULT_BASE_LAYERS = [
_baselayer(
lyr, idx) for idx, lyr in enumerate(
settings.MAP_BASELAYERS)]
DEFAULT_MAP_CONFIG = _default_map.viewer_json(None, *DEFAULT_BASE_LAYERS)
return DEFAULT_MAP_CONFIG, DEFAULT_BASE_LAYERS
_viewer_projection_lookup = {
"EPSG:900913": {
"maxResolution": 156543.03390625,
"units": "m",
"maxExtent": [-20037508.34, -20037508.34, 20037508.34, 20037508.34],
},
"EPSG:4326": {
"max_resolution": (180 - (-180)) / 256,
"units": "degrees",
"maxExtent": [-180, -90, 180, 90]
}
}
def _get_viewer_projection_info(srid):
# TODO: Look up projection details in EPSG database
return _viewer_projection_lookup.get(srid, {})
def resolve_object(request, model, query, permission='base.view_resourcebase',
permission_required=True, permission_msg=None):
"""Resolve an object using the provided query and check the optional
permission. Model views should wrap this function as a shortcut.
query - a dict to use for querying the model
permission - an optional permission to check
permission_required - if False, allow get methods to proceed
permission_msg - optional message to use in 403
"""
obj = get_object_or_404(model, **query)
obj_to_check = obj.get_self_resource()
if settings.RESOURCE_PUBLISHING:
if (not obj_to_check.is_published) and (
not request.user.has_perm('publish_resourcebase', obj_to_check)
):
raise Http404
allowed = True
if permission.split('.')[-1] in ['change_layer_data', 'change_layer_style']:
if obj.__class__.__name__ == 'Layer':
obj_to_check = obj
if permission:
if permission_required or request.method != 'GET':
allowed = request.user.has_perm(
permission,
obj_to_check)
if not allowed:
mesg = permission_msg or _('Permission Denied')
raise PermissionDenied(mesg)
return obj
def json_response(body=None, errors=None, redirect_to=None, exception=None,
content_type=None, status=None):
"""Create a proper JSON response. If body is provided, this is the response.
If errors is not None, the response is a success/errors json object.
If redirect_to is not None, the response is a success=True, redirect_to object
If the exception is provided, it will be logged. If body is a string, the
exception message will be used as a format option to that string and the
result will be a success=False, errors = body % exception
"""
if content_type is None:
content_type = "application/json"
if errors:
if isinstance(errors, basestring):
errors = [errors]
body = {
'success': False,
'errors': errors
}
elif redirect_to:
body = {
'success': True,
'redirect_to': redirect_to
}
elif exception:
if body is None:
body = "Unexpected exception %s" % exception
else:
body = body % exception
body = {
'success': False,
'errors': [body]
}
elif body:
pass
else:
raise Exception("must call with body, errors or redirect_to")
if status is None:
status = 200
if not isinstance(body, basestring):
body = json.dumps(body)
return HttpResponse(body, content_type=content_type, status=status)
def num_encode(n):
if n < 0:
return SIGN_CHARACTER + num_encode(-n)
s = []
while True:
n, r = divmod(n, BASE)
s.append(ALPHABET[r])
if n == 0:
break
return ''.join(reversed(s))
def num_decode(s):
if s[0] == SIGN_CHARACTER:
return -num_decode(s[1:])
n = 0
for c in s:
n = n * BASE + ALPHABET_REVERSE[c]
return n
def format_urls(a, values):
b = []
for i in a:
j = i.copy()
try:
j['url'] = unicode(j['url']).format(**values)
except KeyError:
j['url'] = None
b.append(j)
return b
def build_abstract(resourcebase, url=None, includeURL=True):
if resourcebase.abstract and url and includeURL:
return u"{abstract} -- [{url}]({url})".format(abstract=resourcebase.abstract, url=url)
else:
return resourcebase.abstract
def build_caveats(resourcebase):
caveats = []
if resourcebase.maintenance_frequency:
caveats.append(resourcebase.maintenance_frequency_title())
if resourcebase.license:
caveats.append(resourcebase.license_verbose)
if resourcebase.data_quality_statement:
caveats.append(resourcebase.data_quality_statement)
if len(caveats) > 0:
return u"- "+u"%0A- ".join(caveats)
else:
return u""
def build_social_links(request, resourcebase):
social_url = u"{protocol}://{host}{path}".format(
protocol=("https" if request.is_secure() else "http"),
host=request.get_host(),
path=request.get_full_path())
date = datetime.datetime.strftime(resourcebase.date, "%m/%d/%Y") if resourcebase.date else None
abstract = build_abstract(resourcebase, url=social_url, includeURL=True)
caveats = build_caveats(resourcebase)
return format_urls(
settings.SOCIAL_ORIGINS,
{
'name': resourcebase.title,
'date': date,
'abstract': abstract,
'caveats': caveats,
'url': social_url})
def check_shp_columnnames(layer):
""" Check if shapefile for a given layer has valid column names.
If not, try to fix column names and warn the user
"""
# TODO we may add in a better location this method
inShapefile = ''
for f in layer.upload_session.layerfile_set.all():
if os.path.splitext(f.file.name)[1] == '.shp':
inShapefile = f.file.path
inDriver = ogr.GetDriverByName('ESRI Shapefile')
inDataSource = inDriver.Open(inShapefile, 1)
if inDataSource is None:
print 'Could not open %s' % (inShapefile)
return False, None, None
else:
inLayer = inDataSource.GetLayer()
# TODO we may need to improve this regexp
# first character must be any letter or "_"
# following characters can be any letter, number, "#", ":"
regex = r'^[a-zA-Z,_][a-zA-Z,_,#,:\d]*$'
a = re.compile(regex)
regex_first_char = r'[a-zA-Z,_]{1}'
b = re.compile(regex_first_char)
inLayerDefn = inLayer.GetLayerDefn()
list_col_original = []
list_col = {}
for i in range(0, inLayerDefn.GetFieldCount()):
field_name = inLayerDefn.GetFieldDefn(i).GetName()
if a.match(field_name):
list_col_original.append(field_name)
try:
for i in range(0, inLayerDefn.GetFieldCount()):
field_name = unicode(inLayerDefn.GetFieldDefn(i).GetName(), layer.charset)
if not a.match(field_name):
new_field_name = custom_slugify(field_name)
if not b.match(new_field_name):
new_field_name = '_'+new_field_name
j = 0
while new_field_name in list_col_original or new_field_name in list_col.values():
if j == 0:
new_field_name += '_0'
if new_field_name.endswith('_'+str(j)):
j += 1
new_field_name = new_field_name[:-2]+'_'+str(j)
list_col.update({field_name: new_field_name})
except UnicodeDecodeError:
return False, None, None
if len(list_col) == 0:
return True, None, None
else:
for key in list_col.keys():
qry = u"ALTER TABLE {0} RENAME COLUMN \"{1}\" TO \"{2}\"".format(inLayer.GetName(), key, list_col[key])
inDataSource.ExecuteSQL(qry.encode(layer.charset))
return True, None, list_col