-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeo.py
More file actions
816 lines (692 loc) · 24 KB
/
geo.py
File metadata and controls
816 lines (692 loc) · 24 KB
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
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
# ===========================================================================
# Copyright (C) 2010 ISAW, New York University
#
# 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 2 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, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# About Pleiades
# --------------
#
# Pleiades is an international research network and associated web portal and
# content management system devoted to the study of ancient geography.
#
# See http://pleiades.stoa.org
#
# Funding for the creation of this software was provided by a grant from the
# U.S. National Endowment for the Humanities (http://www.neh.gov).
# ===========================================================================
from Acquisition import aq_parent
from AccessControl import getSecurityManager
from AccessControl.SecurityManagement import newSecurityManager
from AccessControl.SecurityManagement import setSecurityManager
from AccessControl.User import nobody
from collective.geo.geographer.interfaces import IGeoreferenced
from pleiades.capgrids import Grid, parseURL
from pleiades.geographer.interfaces import IConnected, IExtent
from pleiades.geographer.interfaces import IFootprint, IRepresentativePoint
from plone.memoize.instance import memoize
from Products.CMFCore.utils import getToolByName
from Products.PleiadesEntity.content.interfaces import ILocation
from Products.PleiadesEntity.content.interfaces import IPlace
from Products.PleiadesEntity.time import temporal_overlap
from shapely.geometry import asShape, mapping, MultiPoint, shape
from shapely.ops import unary_union
from zope.interface import implements
import logging
import math
import simplejson as json
log = logging.getLogger('pleiades.geographer')
PRECISIONS = ['precise', 'related', 'rough', 'unlocated']
class NotLocatedError(Exception):
pass
class LocationGeoItem(object):
implements(IGeoreferenced)
def __init__(self, context):
"""Expect getGeometry() returns a string like
'Point:[-105.0, 40.0]'
"""
self.context = context
dc_coverage = self.context.getLocation()
if context._getGeometryRaw():
self.geo = json.loads(context.getGeometryJSON())
g = asShape(self.geo)
self.geo.update(bbox=g.bounds)
elif dc_coverage.startswith('http://atlantides.org/capgrids'):
try:
mapid, gridsquare = parseURL(dc_coverage)
grid = Grid(mapid, gridsquare)
self.geo = dict(
bbox=grid.bounds,
relation='relates',
type=grid.type,
coordinates=grid.coordinates,
)
except:
raise NotLocatedError(
"Could not determine CAP grid square {}".format(
dc_coverage))
else:
raise ValueError("Context is unlocated")
@property
def __geo_interface__(self):
return self.geo # .__geo_interface__
@property
def bounds(self):
return self.geo['bbox']
@property
def type(self):
return self.geo['type']
@property
def coordinates(self):
return self.geo['coordinates']
@property
def precision(self):
return 'rough' * (
self.geo.get('relation', None) is not None) or 'precise'
@property
def crs(self):
return None
class FeatureGeoItem(object):
implements(IGeoreferenced)
def __init__(self, context):
"""Initialize adapter."""
self.context = context
self._adapter = None
x = list(self.context.getLocations())
if len(x) == 0:
raise ValueError("Unlocated: could not adapt %s" % str(context))
else:
self._adapter = IGeoreferenced(x[0])
@property
def type(self):
return self._adapter.type
@property
def coordinates(self):
return self._adapter.coordinates
@property
def crs(self):
return None
@property
def __geo_interface__(self):
context = self.context
return dict(
type='Feature',
id=context.getId(),
geometry=self._adapter.__geo_interface__,
)
class PlaceGeoItem(object):
"""Python expression of a GeoRSS simple item.
"""
implements(IGeoreferenced)
def __init__(self, context):
"""Initialize adapter."""
self.context = context
self.geo = None
x = []
for o in self.context.getLocations():
try:
x.append(IGeoreferenced(o))
except ValueError:
continue
if len(x) > 0:
precise = [xx for xx in x if xx.precision == 'precise']
if len(precise) >= 1:
x = precise
self.geo = self._geo(x)
else:
geo_parts = []
for ob in self.context.getFeatures():
geo_parts.append(IGeoreferenced(ob))
if geo_parts:
self.geo = self._geo(geo_parts)
if self.geo is None:
raise NotLocatedError("Location cannot be determined")
def _geo(self, obs):
# Returns a geometric object or a bounding box for multiple objects
if len(obs) == 1:
return obs[0].geo
else:
xs = []
ys = []
fuzzy = []
for o in obs:
if 'relation' in o.__geo_interface__:
fuzzy.append(o)
continue
b = o.bounds
xs += b[0::2]
ys += b[1::2]
for o in fuzzy:
b = o.bounds
xs += b[0::2]
ys += b[1::2]
try:
x0, x1, y0, y1 = (min(xs), max(xs), min(ys), max(ys))
except Exception as e:
log.warn("Failed to adapt %s in _geo(): %s", obs, str(e))
return None
coords = [[[x0, y0], [x0, y1], [x1, y1], [x1, y0], [x0, y0]]]
return dict(
bbox=(x0, y0, x1, y1),
relation='relates' * int(bool(fuzzy)) or None,
type='Polygon',
coordinates=coords,
)
@property
def bounds(self):
return self.geo['bbox']
@property
def type(self):
return self.geo['type']
@property
def coordinates(self):
return self.geo['coordinates']
@property
def precision(self):
return 'rough' * (
self.geo.get('relation', None) is not None) or 'precise'
@property
def crs(self):
return None
@property
def __geo_interface__(self):
context = self.context
return dict(
type='Feature',
id=context.getId(),
bbox=self.geo['bbox'],
geometry=self.geo
)
class NameGeoItem(PlaceGeoItem):
"""Python expression of a GeoRSS simple item, temporally sensitive
"""
implements(IGeoreferenced)
def __init__(self, context):
"""Initialize adapter."""
self.context = context
self.geo = None
x = []
place = self.context.aq_parent
for o in filter(
lambda x: temporal_overlap(self.context, x),
place.getLocations()):
try:
x.append(IGeoreferenced(o))
except ValueError:
continue
if len(x) > 0:
self.geo = self._geo(x)
else:
geo_parts = []
for ob in self.context.getFeatures():
geo_parts.append(IGeoreferenced(ob))
if geo_parts:
self.geo = self._geo(geo_parts)
if self.geo is None:
raise NotLocatedError("Location cannot be determined")
def createGeoItem(context):
"""Factory for adapters."""
if IPlace.providedBy(context):
return PlaceGeoItem(context)
else:
return FeatureGeoItem(context)
class RepresentativePoint(object):
"""Adapter for Locations and Names."""
implements(IRepresentativePoint)
def __init__(self, context):
self.context = context
try:
g = IGeoreferenced(context)
self.precision = g.precision
if not g.coordinates:
raise NotLocatedError("Adapter has no coordinates")
if g.type == 'Point':
self.coords = tuple(g.coordinates)
else:
self.coords = tuple(asShape(
{'type': g.type, 'coordinates': g.coordinates}
).centroid.coords)[0]
except (ValueError, NotLocatedError):
self.precision = "unlocated"
self.coords = None
@property
def x(self):
try:
return self.coords[0]
except TypeError:
raise NotLocatedError("Context %s is unlocated" % self.context)
@property
def y(self):
try:
return self.coords[1]
except TypeError:
raise NotLocatedError("Context %s is unlocated" % self.context)
# Functions to support synoptic views of Pleiades data.
def extent(obj):
# "unlocated", "rough", "related", "precise"
# Executed as anonymous to keep unpublished data out
sm = getSecurityManager()
try:
newSecurityManager(None, nobody.__of__(obj.acl_users))
ex = IExtent(obj)
res = {'extent': ex.extent, 'precision': ex.precision}
except NotLocatedError:
res = {'extent': None, 'precision': 'unlocated'}
except:
log.warn("Failed to adapt %s in 'location_precision'", obj)
res = None
finally:
setSecurityManager(sm)
return res
def footprint(obj):
"""Footprint geometry for a place, executed as Anonymous."""
sm = getSecurityManager()
try:
newSecurityManager(None, nobody.__of__(obj.acl_users))
fp = IFootprint(obj)
return fp.footprint()
except Exception:
log.warn("Failed to adapt %s in 'footprint'", obj)
return None
finally:
setSecurityManager(sm)
def is_clockwise(poly):
total = poly[-1][0] * poly[0][1] - poly[0][0] * poly[-1][1]
for i in range(len(poly) - 1):
total += poly[i][0] * poly[i + 1][1] - poly[i + 1][0] * poly[i][1]
return total <= 0
def location_precision(obj):
# "unlocated", "rough", "related", "precise"
# Executed as anonymous to keep unpublished data out
ex = extent(obj)
return ex and ex.get('precision', 'unlocated') or None
def representative_point(obj):
# Get a representative point as Anonymous."""
sm = getSecurityManager()
try:
newSecurityManager(None, nobody.__of__(obj.acl_users))
pt = IRepresentativePoint(obj)
res = {'precision': pt.precision, 'coords': pt.coords}
except NotLocatedError:
res = {'precision': 'unlocated', 'coords': None}
except:
log.warn("Failed to adapt %s in 'representative_point'", obj)
res = None
finally:
setSecurityManager(sm)
return res
def zgeo_geometry_centroid(brain):
"""For use on catalog brains"""
geom = brain.zgeo_geometry
if geom['type'] == 'Point':
return tuple(geom['coordinates'])
else:
return tuple(asShape(geom).centroid.coords)[0]
# New implementation of IConnected
def geometry(o):
"""Returning a mapping or None."""
if ILocation.providedBy(o) and o._getGeometryRaw():
geom = json.loads(o.getGeometryJSON())
g = shape(geom)
geom.update(bbox=g.bounds)
return geom
else:
return None
def isPrecise(o):
try:
return (
ILocation.providedBy(o) and o._getGeometryRaw() and
IGeoreferenced(o)
)
except (ValueError, NotLocatedError):
return False
def isGridded(o):
return ILocation.providedBy(o) and o.getLocation(
).startswith('http://atlantides.org/capgrids')
class PlaceLocated(object):
def __init__(self, context):
self.context = context
self.locations = self.context.getLocations()
def preciseGeoms(self, sort_key=None):
locations = filter(isPrecise, self.locations)
if sort_key:
locations = sorted(locations, key=sort_key)
return [geometry(o) for o in locations]
def gridGeoms(self):
res = []
for o in filter(isGridded, self.locations):
try:
item = LocationGeoItem(o)
except NotLocatedError:
pass
else:
res.append(mapping(item))
return res
class PlaceConnected(object):
implements(IConnected)
def __init__(self, context):
self.context = context
self.connections = context.getConnectedPlaces()
def preciseExtents(self):
return map(lambda x: x.extent,
filter(
lambda x: x.precision == "precise",
(PlaceExtent(o, depth=0) for o in self.connections)))
def relatedExtents(self):
return map(lambda x: x.extent,
filter(
lambda x: x.precision in ("related", "rough"),
(PlaceExtent(o, depth=0) for o in self.connections)))
# Extents
def explode(coords):
"""Explode a GeoJSON geometry's coordinates object and yield
coordinate tuples."""
for e in coords:
if isinstance(e, (float, int, long)):
yield coords
break
else:
for f in explode(e):
yield f
def hull(points):
return mapping(MultiPoint(points).convex_hull)
class PlaceExtent(object):
implements(IExtent)
def __init__(self, context, depth=1):
self.context = context
# We only look for connections of this place if depth > 0.
self.depth = depth
@memoize
def reprExtent(self):
points = []
located = PlaceLocated(self.context)
geoms = located.preciseGeoms()
if geoms:
for g in geoms:
points.extend(list(explode(g['coordinates'])))
return hull(points), "precise"
connected = PlaceConnected(self.context)
extents = self.depth and connected.preciseExtents() or None
if extents:
for g in extents:
points.extend(list(explode(g['coordinates'])))
return hull(points), "related"
gridGeoms = located.gridGeoms()
if gridGeoms:
for g in gridGeoms:
points.extend(list(explode(g['coordinates'])))
return hull(points), "rough"
extents = self.depth and connected.relatedExtents() or None
if extents:
for g in extents:
points.extend(list(explode(g['coordinates'])))
return hull(points), "rough"
# The end.
return None, "unlocated"
@property
def extent(self):
"""A Polygon."""
return self.reprExtent()[0]
@property
def precision(self):
return self.reprExtent()[1]
def meters_to_degrees(distance_meters, latitude):
"""Approximate meters-to-degrees conversion with latitude adjustment."""
try:
dist = float(distance_meters)
lat_rad = math.radians(float(latitude))
deg_lat = dist / 111320.0
# Avoid divide-by-zero near the poles
deg_lon = deg_lat / max(math.cos(lat_rad), 1.0e-6)
return (deg_lat + deg_lon) / 2.0
except (TypeError, ValueError):
return None
class PlaceFootprint(object):
implements(IFootprint)
def __init__(self, context):
self.context = context
def _workflow_state(self, obj):
try:
wf = getToolByName(self.context, 'portal_workflow')
except Exception:
return None
try:
return wf.getInfoFor(obj, 'review_state')
except Exception:
return None
def _is_published(self, obj):
return self._workflow_state(obj) == 'published'
def _accuracy_value(self, location):
accuracy = getattr(location, 'getAccuracy', lambda: None)()
if not accuracy:
return None
try:
return accuracy.getField('value').get(accuracy)
except Exception:
return None
def _location_buffers(self):
buffers = []
for loc in self.context.getLocations():
if not self._is_published(loc):
continue
try:
georef = IGeoreferenced(loc)
except (ValueError, NotLocatedError):
continue
if georef.precision == 'rough':
continue
accuracy_val = self._accuracy_value(loc)
if accuracy_val in (None, ''):
continue
geom_mapping = geometry(loc)
if not geom_mapping:
continue
g = shape(geom_mapping)
dist = meters_to_degrees(accuracy_val, g.centroid.y)
if dist is None:
continue
buffers.append(g.buffer(dist))
return buffers
def _connected_footprints(self, visited):
geoms = []
inbound_types = {
'at',
'capital',
'part_of_admin',
'part_of_analytical',
'part_of_physical',
'part_of_regional',
'same_as',
}
outbound_types = {'same_as'}
for conn in self.context.getReverseConnections():
if conn.relationshipType not in inbound_types:
continue
if not self._is_published(conn):
continue
place = aq_parent(conn)
if not IPlace.providedBy(place) or not self._is_published(place):
continue
uid = place.UID()
if uid in visited:
continue
visited.add(uid)
try:
geom = IFootprint(place).footprint(visited=visited)
except Exception:
geom = None
if geom:
geoms.append(shape(geom))
for conn in self.context.getSubConnections():
if conn.relationshipType not in outbound_types:
continue
if not self._is_published(conn):
continue
place = conn.getConnection()
if not place or not self._is_published(place):
continue
uid = place.UID()
if uid in visited:
continue
visited.add(uid)
try:
geom = IFootprint(place).footprint(visited=visited)
except Exception:
geom = None
if geom:
geoms.append(shape(geom))
return geoms
def footprint(self, visited=None):
root_call = visited is None
if root_call and hasattr(self, '_cached_footprint'):
return self._cached_footprint
if visited is None:
visited = set()
visited.add(self.context.UID())
geoms = []
geoms.extend(self._location_buffers())
geoms.extend(self._connected_footprints(visited))
if not geoms:
return None
try:
merged = unary_union(geoms)
except Exception as exc:
log.warn("Failed to union footprint geometries for %s: %s", self.context, exc)
return None
if merged.is_empty:
return None
if merged.geom_type in ('Polygon', 'MultiPolygon'):
footprint_geom = merged
else:
footprint_geom = merged.convex_hull
result = mapping(footprint_geom)
if root_call:
self._cached_footprint = result
return result
class LocationExtent(PlaceExtent):
implements(IExtent)
@memoize
def reprExtent(self):
try:
g = IGeoreferenced(self.context)
points = list(explode(g.coordinates))
return hull(points), g.precision
except:
return None, "unlocated"
# Repesentative Point
class PlaceReprPt(object):
implements(IRepresentativePoint)
def __init__(self, context):
self.context = context
@staticmethod
def average_centroid_of_geometries(geometries):
centroids = []
for g in geometries:
centroid = shape(g).centroid
centroids.append(centroid)
average_x = sum(centroid.x for centroid in centroids) / len(centroids)
average_y = sum(centroid.y for centroid in centroids) / len(centroids)
return average_x, average_y
def centroid_by_connection_type(self, relationshipTypes, bidirectional=False):
connections = self.context.getReverseConnections() # Find inbound connections only by default
if bidirectional:
connections += self.context.getSubConnections()
matching_connections = filter(lambda conn: conn.relationshipType in relationshipTypes, connections)
if matching_connections:
extents = [PlaceExtent(o, depth=0) for o in matching_connections]
extents = filter(lambda x: x.precision == "precise", extents)
if extents:
centroid = self.average_centroid_of_geometries(extent.extent for extent in extents)
return centroid
raise ValueError("No centroid found")
@memoize
def reprPoint(self):
located = PlaceLocated(self.context)
geoms = located.preciseGeoms()
if geoms:
centroid = self.average_centroid_of_geometries(geoms)
return centroid[0], centroid[1], "precise"
try:
centroid = self.centroid_by_connection_type({
'part_of_physical',
'at',
'on'
})
except ValueError:
pass
else:
return centroid[0], centroid[1], "related"
try:
centroid = self.centroid_by_connection_type({
'part_of_regional',
'part_of_analytical',
'part_of_admin',
})
except ValueError:
pass
else:
return centroid[0], centroid[1], "related"
try:
centroid = self.centroid_by_connection_type({
'near',
'intersects',
'bounds',
'flows_into',
'route_next',
'abuts',
'communicates',
}, bidirectional=True)
except ValueError:
pass
else:
return centroid[0], centroid[1], "related"
try:
centroid = self.centroid_by_connection_type({
'member',
'dependent',
})
except ValueError:
pass
else:
return centroid[0], centroid[1], "related"
connected = PlaceConnected(self.context)
extents = connected.preciseExtents()
if extents:
centroid = self.average_centroid_of_geometries(extents)
return centroid[0], centroid[1], "related"
gridGeoms = located.gridGeoms()
if gridGeoms:
centroid = self.average_centroid_of_geometries(gridGeoms)
return centroid[0], centroid[1], "rough"
extents = connected.relatedExtents()
if extents:
centroid = self.average_centroid_of_geometries(extents)
return centroid[0], centroid[1], "rough"
# The end.
return None, None, "unlocated"
@property
def coords(self):
x, y, precision = self.reprPoint()
if precision == 'unlocated':
return None
return x, y
@property
def x(self):
return self.reprPoint()[0]
@property
def y(self):
return self.reprPoint()[1]
@property
def precision(self):
return self.reprPoint()[2]