forked from mapbox/robosat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parking.py
45 lines (32 loc) · 1.28 KB
/
parking.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
import sys
import osmium
import geojson
import shapely.geometry
from robosat.osm.core import is_polygon
class ParkingHandler(osmium.SimpleHandler):
"""Extracts parking lot polygon features (visible in satellite imagery) from the map.
"""
# parking=* to discard because these features are not vislible in satellite imagery
parking_filter = set(["underground", "sheds", "carports", "garage_boxes"])
def __init__(self):
super().__init__()
self.features = []
def way(self, w):
if not is_polygon(w):
return
if "amenity" not in w.tags or w.tags["amenity"] != "parking":
return
if "parking" in w.tags:
if w.tags["parking"] in self.parking_filter:
return
geometry = geojson.Polygon([[(n.lon, n.lat) for n in w.nodes]])
shape = shapely.geometry.shape(geometry)
if shape.is_valid:
feature = geojson.Feature(geometry=geometry)
self.features.append(feature)
else:
print("Warning: invalid feature: https://www.openstreetmap.org/way/{}".format(w.id), file=sys.stderr)
def save(self, out):
collection = geojson.FeatureCollection(self.features)
with open(out, "w") as fp:
geojson.dump(collection, fp)