-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpolygonTools.py
368 lines (269 loc) · 9 KB
/
polygonTools.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
# https://github.com/hpcc-systems/HPCC-Platform/blob/master/initfiles/examples/embed/python-stream.ecl
# https://hpccsystems.com/bb/viewtopic.php?f=10&t=3613
# https://hpccsystems.com/bb/viewtopic.php?f=23&t=5243
# https://hpccsystems.com/blog/embedding-tensorflow-operations-ecl
# https://hpccsystems.com/bb/viewtopic.php?f=41&t=1509
import pyproj
from shapely import wkt, ops
from shapely.errors import TopologicalError
from shapely.geometry import Polygon
import warnings
import itertools
from functools import wraps
import traceback
# Internal Functions #
def _fail_as(fail_as):
"""
Decorator to use _fail_nicely in a function.
When used as a decorator, this tries to run the function but
returns `fail_as` if an exception is raised. This is needed as
HPCC does not communicate python errors back to HPCC in a
sensible manner.
"""
def wrapper(f):
@wraps(f)
def func_wrapper(*args, **kwargs):
return _fail_nicely(f, fail_as, args, kwargs)
return func_wrapper
return wrapper
# TODO test
def _convert_wkt(poly):
if isinstance(poly, str):
return wkt.loads(poly)
else:
return poly
def _fail_nicely(f, to_return, args, kwargs):
"""
Run a function and return `to_return` in the case of an exception.
:param f: function
:param to_return: Object
:param args: args for `f`
:param kwargs: keyword args for `f`
:return:
"""
try:
return f(*args, **kwargs)
except Exception:
tb = traceback.format_exc()
warnings.warn(tb)
return to_return
#########################
# Single Line Functions #
@_fail_as(0.0)
def poly_area(poly):
poly = _convert_wkt(poly)
return float(poly.area)
@_fail_as(False)
def wkt_isvalid(poly):
poly = _convert_wkt(poly)
return poly.is_valid
@_fail_as(False)
def poly_isin(inner, outer):
inner = _convert_wkt(inner)
outer = _convert_wkt(outer)
return outer.contains(inner)
@_fail_as(False)
def poly_intersect(poly1, poly2):
poly1 = _convert_wkt(poly1)
poly2 = _convert_wkt(poly2)
return poly1.intersects(poly2)
@_fail_as([])
def poly_corners(poly):
poly = _convert_wkt(poly)
return list(poly.bounds)
@_fail_as('')
def poly_centroid(poly):
poly = _convert_wkt(poly)
return poly.centroid.wkt
@_fail_as("")
def project_polygon(poly, to_proj, from_proj="epsg:4326"):
"""
Project from one CRS to another. Usually used to make area calcs sensible
"""
poly = _convert_wkt(poly)
p1 = pyproj.Proj(init=from_proj)
p2 = pyproj.Proj(init=to_proj)
def trans(x, y):
return pyproj.transform(p1, p2, x, y)
poly = ops.transform(trans, poly)
return poly.wkt
@_fail_as(0.0)
def overlap_area(polys):
"""
Remember to project!!!!
"""
union_poly = overlap_polygon(polys)
return poly_area(union_poly)
@_fail_as("")
def overlap_polygon(in_polys):
polys = (_convert_wkt(poly) for poly in in_polys)
combinations = itertools.combinations(polys, 2)
overlaps = [a.intersection(b) for a, b in combinations]
unioned_overlaps = poly_union(overlaps)
return unioned_overlaps
@_fail_as("")
def poly_simplify(in_poly, tol=0.000001):
poly = _convert_wkt(in_poly)
poly = poly.simplify(tol)
poly = poly if poly.is_valid else ""
return poly
@_fail_as("")
def _combine_poly(poly_1, poly_2, tol=0.000001):
p1 = _convert_wkt(poly_1)
if not p1.is_valid:
p1 = p1.simplify(tol)
p2 = _convert_wkt(poly_2)
if not p2.is_valid:
p2 = p2.simplify(tol)
assert p1.is_valid
assert p2.is_valid
try:
p = p1.union(p2)
except TopologicalError:
p1 = p1.simplify(tol)
p2 = p2.simplify(tol)
p = _combine_poly(p1, p2, tol)
if not p.is_valid:
p = p.simplify(tol)
assert p.is_valid
return p.wkt
@_fail_as("")
def poly_union(in_polys, tol=0.000001):
"""
Union a list of polygons. Drops invalid polygons at read in
so CHECK THIS FIRST! `poly_isvalid` will help you here.
Parameters
----------
in_polys: list
polygons to merge in WKT format.
tol: float
tolerance to simplify polygons by in the case of an overlapping
merge.
Returns
-------
type: string
String of the resulting WKT.
"""
# if len(in_polys) == 1:
# return in_polys[0]
combined = Polygon().wkt
for new in in_polys:
combined = _combine_poly(combined, new, tol)
return combined
###########################################################
# Dataset Wide Functions #
def wkts_are_valid(recs):
"""
Ensures your WKTs are valid
Takes an ECL dataset {STRING uid; STRING polygon;}
Returns an ECL dataset {STRING uid; BOOLEAN is_valid;}
"""
for rec in recs:
yield (rec.uid, wkt_isvalid(rec.polygon))
def polys_area(recs):
"""
Failures will not be returned. Test with polys_is_valid
first!
Takes an ECL dataset {STRING uid; STRING polygon;}
Returns an ECL dataset {STRING uid; REAL area;}
"""
for rec in recs:
yield (rec.uid, poly_area(rec.polygon))
def polys_arein(recs):
"""
Failures will not be returned. Test with polys_are_valid
first!
Takes an ECL dataset {STRING uid; STRING inner; STRING outer;}
Returns an ECL dataset {STRING uid; BOOLEAN is_in;}
"""
for rec in recs:
yield (rec.uid, poly_isin(rec.inner, rec.outer))
def polys_union(recs, tol=0.000001):
"""
Failures will be silently dropped from the merge. Test
with polys_is_valid first!
Takes an ECL dataset {STRING uid; SET OF STRING polygons;}
Returns an ECL dataset {STRING uid; STRING polygon;}
"""
for rec in recs:
yield (rec.uid, poly_union(rec.polygons, tol))
def overlap_areas(recs):
"""
Failures will be silently dropped from the merge. Test
with polys_is_valid first!
Takes an ECL dataset {STRING uid; SET OF STRING polygons;}
Returns an ECL dataset {STRING uid; REAL overlap;}
"""
for rec in recs:
yield (rec.uid, overlap_area(rec.polygons))
def overlap_polygons(recs):
"""
Failures will be silently dropped from the merge. Test
with polys_is_valid first!
Takes an ECL dataset {STRING uid; SET OF STRING polygons;}
Returns an ECL dataset {STRING uid; STRING polygon;}
"""
for rec in recs:
yield (rec.uid, overlap_polygon(rec.polygons))
def polys_intersect(recs):
"""
Failures will be silently dropped from the merge. Test
with polys_is_valid first!
Takes an ECL dataset {STRING uid; STRING polygon; STRING polygon2;}
Returns an ECL dataset {STRING uid; BOOLEAN intersects;}
"""
for rec in recs:
yield(rec.uid, poly_intersect(rec.polygon, rec.polygon2))
def project_polygons(recs, to_proj, from_proj="epsg:4326"):
"""
Failures will be silently dropped from the merge. Test
with polys_is_valid first!
Takes an ECL dataset {STRING uid; STRING polygon;}
Returns an ECL dataset {STRING uid; STRING polygon;}
"""
for rec in recs:
yield (rec.uid, project_polygon(rec.polygon, to_proj, from_proj))
def polys_corners(recs):
"""
Failures will be silently dropped from the merge. Test
with polys_is_valid first! Returns Corners, not whole box
but that is sufficient.
Takes an ECL dataset {STRING uid; STRING polygon;}
Returns an ECL dataset {STRING uid; REAL lon_min; REAL lat_max; REAL lon_max; REAL lat_min;}
"""
for rec in recs:
boundbox = poly_corners(rec.polygon)
try:
yield (rec.uid, boundbox[0], boundbox[1], boundbox[2], boundbox[3])
except IndexError:
yield (rec.uid, 0.0, 0.0, 0.0, 0.0)
def polys_centroids(recs):
"""
Failures will be silently dropped from the merge. Test
with polys_is_valid first!
Takes an ECL dataset {STRING uid; STRING polygon;}
Returns an ECL dataset {STRING uid; STRING centroid;}
"""
for rec in recs:
yield (rec.uid, poly_centroid(rec.polygon))
def polys_simplify(recs, tol=0.000001):
"""
Failures will be returned as ''!
Takes an ECL dataset {STRING uid; STRING polygon;}
Returns an ECL dataset {STRING uid; STRING polygon;}
"""
for rec in recs:
yield (rec.uid, poly_simplify(rec.polygon, tol))
#################################################################
# if __name__ == "__main__":
# outer = 'POLYGON((40 40, 20 40, 20 20, 40 20, 40 40))'
# inner = 'POINT(30 30)'
# poly_isin(inner, outer)
# answer = poly_isin(inner, outer)
# print(answer)
# import collections
# Row = collections.namedtuple("rec", ["uid", "inner", "outer"])
# recs = Row(uid='a', inner='POINT(30 30)', outer='POLYGON((40 40, 20 40, 20 20, 40 20, 40 40))') #Make a namedtuple from the Row class we created
# results = polys_arein([recs])
# for result in results:
# print(result)