forked from mapbox/robosat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.py
62 lines (39 loc) · 1.21 KB
/
core.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
import os
import uuid
import geojson
class FeatureStorage:
"""Stores features on disk and handles batching.
Note: you have to call flush at the end to flush the last partial batch.
"""
def __init__(self, out, batch):
assert batch > 0
self.out = out
self.batch = batch
self.features = []
def add(self, feature):
if len(self.features) >= self.batch:
self.flush()
self.features.append(feature)
def flush(self):
if not self.features:
return
collection = geojson.FeatureCollection(self.features)
base, ext = os.path.splitext(self.out)
suffix = uuid.uuid4().hex
out = "{}-{}{}".format(base, suffix, ext)
with open(out, "w") as fp:
geojson.dump(collection, fp)
self.features.clear()
def is_polygon(way):
"""Checks if the way is a polygon.
Args
way: the osmium.osm.Way to check.
Returns:
True if the way is a polygon, False otherwise.
Note: The geometry shape can still be invalid (e.g. self-intersecting).
"""
if not way.is_closed():
return False
if len(way.nodes) < 4:
return False
return True