-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathgeoutils.py
More file actions
318 lines (270 loc) · 10.3 KB
/
geoutils.py
File metadata and controls
318 lines (270 loc) · 10.3 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
import logging
from functools import partial
import mercantile
import pyproj
from rtree import index
from shapely.geometry import GeometryCollection
from shapely.geometry import LineString
from shapely.geometry import mapping
from shapely.geometry import MultiPolygon
from shapely.geometry import Polygon
from shapely.geometry import shape
from shapely.ops import cascaded_union
from shapely.ops import transform
import utils
logger = logging.getLogger("processing")
def get_union(geojson):
""" Returns a geojson geometry that is the union of all features in a geojson feature collection """
shapes = []
for feature in geojson["features"]:
if feature["geometry"]["type"] not in ["Polygon", "MultiPolygon"]:
continue
s = shape(feature["geometry"])
if s and not s.is_valid:
s = s.buffer(0.0)
if not s.is_valid:
logger.error("Invalid geometry in get_union, failed to fix")
else:
pass
# logger.warning("Invalid geometry in get_union. Fixed.")
if s and s.is_valid:
# get rid of holes
if type(s) in (MultiPolygon, GeometryCollection):
hulls = [Polygon(r.exterior) for r in s.geoms]
hull = MultiPolygon(hulls)
else:
hull = Polygon(s.exterior)
# simplify so calculating union doesnt take forever
simplified = hull.simplify(0.01, preserve_topology=True)
if simplified.is_valid:
shapes.append(simplified)
else:
shapes.append(hull)
try:
result = cascaded_union(shapes)
except Exception as e:
# workaround for geos bug with cacscaded_union sometimes failing
logger.error("cascaded_union failed, falling back to union")
result = shapes.pop()
for s in shapes:
result = result.union(s)
# get rid of holes
if type(result) in (MultiPolygon, GeometryCollection):
hulls = [Polygon(r.exterior) for r in result.geoms]
hull = MultiPolygon(hulls)
else:
hull = Polygon(result.exterior)
return mapping(hull)
def polygon_from_bbox(bbox):
""" Generate a polygon geometry from a ESWN bouding box
:param bbox: a 4 float bounding box
:returns: a polygon geometry
"""
return [
[
[bbox[0], bbox[1]],
[bbox[2], bbox[1]],
[bbox[2], bbox[3]],
[bbox[0], bbox[3]],
[bbox[0], bbox[1]],
]
]
def get_label_points(geojson, use_polylabel=True):
""" Generate label points for polygon features
:param geojson: A GeoJSON feature collection containing Polygons or MultiPolygons
:returns: A new GeoJSON Feature collection containing Point features
"""
if use_polylabel:
try:
from shapely.algorithms.polylabel import polylabel
except:
logging.error("Polylabel not available, using centroid for label points")
polylabel = None
else:
logging.error("using centroid for label points, Polylabel disabled")
polylabel = None
label_features = []
for feature in geojson["features"]:
if feature["geometry"]["type"] not in ["Polygon", "MultiPolygon"]:
continue
feature_geometry = shape(feature["geometry"])
if type(feature_geometry) == MultiPolygon:
geometries = feature_geometry.geoms
else:
geometries = [feature_geometry]
for geometry in geometries:
if (
polylabel and geometry.is_valid
): # polylabel doesnt work on invalid geometries, centroid does
try:
project = partial(
pyproj.transform,
pyproj.Proj(init="epsg:4326"),
pyproj.Proj(init="epsg:3857"),
)
geometry_3857 = transform(project, geometry)
label_geometry_3857 = polylabel(geometry_3857)
project = partial(
pyproj.transform,
pyproj.Proj(init="epsg:3857"),
pyproj.Proj(init="epsg:4326"),
)
label_geometry = transform(
project, label_geometry_3857
) # apply projection
except Exception as e:
logger.error(
"Error getting polylabel point for feature: "
+ str(feature["properties"]),
exc_info=e,
)
label_geometry = geometry.centroid
else:
label_geometry = geometry.centroid
if label_geometry:
f = {
"type": "Feature",
"geometry": mapping(label_geometry),
"properties": feature["properties"],
}
label_features.append(f)
return {"type": "FeatureCollection", "features": label_features}
def get_demo_point(geojson):
logging.debug("Generating demo point")
logger.debug("extracting geometry rings")
geometries = []
for feature in geojson["features"]:
if feature["geometry"]["type"] == "Polygon":
rings = feature["geometry"]["coordinates"]
elif feature["geometry"]["type"] == "MultiPolygon":
rings = []
for p in feature["geometry"]["coordinates"]:
rings.extend(p)
for ring in rings:
s = LineString(ring)
geometries.append(s)
logger.debug("Inserting into index")
def generator_function():
for i, obj in enumerate(geometries):
yield (
i,
obj.bounds,
i,
) # Buffer geometry so it comes up in intersection queries
spatial_index = index.Index(generator_function())
best_tile = None
best_tile_feature_count = 0
envelope = shape(get_union(geojson)).bounds
logger.debug("Iterating tiles to find best tile")
for zoom in range(8, 17):
best_tile_feature_count = 0
if best_tile:
envelope = mercantile.bounds(best_tile.x, best_tile.y, best_tile.z)
for tile in mercantile.tiles(
envelope[0], envelope[1], envelope[2], envelope[3], [zoom]
):
tile_bounds = mercantile.bounds(tile.x, tile.y, tile.z)
tile_features = [i for i in spatial_index.intersection(tile_bounds)]
if len(tile_features) > best_tile_feature_count:
tile_bounds_geometry = Polygon(polygon_from_bbox(tile_bounds)[0])
tile_feature_count = 0
for i in tile_features:
if tile_bounds_geometry.intersects(geometries[i]):
tile_feature_count += 1
if tile_feature_count > best_tile_feature_count:
best_tile_feature_count = tile_feature_count
best_tile = tile
if best_tile:
logger.debug(
"best tile: "
+ str(best_tile)
+ " had "
+ str(best_tile_feature_count)
+ " features"
)
tile_bounds = mercantile.bounds(best_tile.x, best_tile.y, best_tile.z)
return (
(tile_bounds[0] + tile_bounds[2]) / 2.0,
(tile_bounds[1] + tile_bounds[3]) / 2.0,
)
else:
logger.error("Found 0 tiles with features")
def _explode(coords):
"""Explode a GeoJSON geometry's coordinates object and
yield coordinate tuples. As long as the input is conforming,
the type of the geometry doesn't matter.
From @sgillies answer: http://gis.stackexchange.com/a/90554/27367
"""
for e in coords:
if isinstance(e, (float, int)):
yield coords
break
else:
for f in _explode(e):
yield f
def get_bbox_from_geojson_feature(feature):
""" Generate a bounding box for GeoJson Feature
:param geojson: GeoJSON Feature
:returns: a 4 float bounding box, ESWN
"""
return get_bbox_from_geojson_geometry(feature["geometry"])
def get_bbox_from_geojson_geometry(geometry):
""" Generate a bounding box for GeoJson Geometry
:param geojson: GeoJSON Geometry
:returns: a 4 float bounding box, ESWN
"""
x, y = list(zip(*list(_explode(geometry["coordinates"]))))
return min(x), min(y), max(x), max(y)
def get_bbox_from_geojson(geojson):
""" Generate a bounding box for GeoJson
:param geojson: Either a GeoJson Feature or FeatureCollection
:returns: a 4 float bounding box, ESWN
"""
if geojson["type"] == "Feature":
return get_bbox_from_geojson_feature(geojson)
elif geojson["type"] == "FeatureCollection":
return get_bbox_from_geojson_feature_collection(geojson)
else:
raise Exception("GeoJson type was not Feature or FeatureCollection")
def get_bbox_from_geojson_feature_collection(geojson):
""" Generate a bounding box for GeoJson FeatureCollection
:param geojson: a GeoJson FeatureCollection
:returns: a 4 float bounding box, ESWN
"""
features = geojson["features"]
if len(features) == 0:
return None
elif len(features) == 1:
return get_bbox_from_geojson_feature(features[0])
feature_bboxes = []
for feature in features:
if "bbox" in feature:
feature_bbox = feature["bbox"]
else:
feature_bbox = get_bbox_from_geojson_feature(feature)
feature_bboxes.append(feature_bbox)
return (
min([b[0] for b in feature_bboxes]),
min([b[1] for b in feature_bboxes]),
max([b[2] for b in feature_bboxes]),
max([b[3] for b in feature_bboxes]),
)
def get_area_acres(geometry):
""" Calculate area in acres for a GeoJSON geometry
:param geometry: A GeoJSON Polygon geometry
:returns: Area in acres
"""
shapely_geometry = shape(geometry)
geom_aea = transform(
partial(
pyproj.transform,
pyproj.Proj(init="EPSG:4326"),
pyproj.Proj(
proj="aea",
lat1=shapely_geometry.bounds[1],
lat2=shapely_geometry.bounds[3],
),
),
shapely_geometry,
)
return round(geom_aea.area / 4046.8564224)