forked from mapbox/robosat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroad.py
151 lines (131 loc) · 5.06 KB
/
road.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
import sys
import math
import osmium
import geojson
import shapely.geometry
from robosat.osm.core import FeatureStorage
class RoadHandler(osmium.SimpleHandler):
"""Extracts road polygon features (visible in satellite imagery) from the map.
"""
highway_attributes = {
"motorway": {
"lanes": 4,
"lane_width": 3.75,
"left_hard_shoulder_width": 0.75,
"right_hard_shoulder_width": 3.0,
},
"trunk": {"lanes": 3, "lane_width": 3.75, "left_hard_shoulder_width": 0.75, "right_hard_shoulder_width": 3.0},
"primary": {
"lanes": 2,
"lane_width": 3.75,
"left_hard_shoulder_width": 0.50,
"right_hard_shoulder_width": 1.50,
},
"secondary": {
"lanes": 1,
"lane_width": 3.50,
"left_hard_shoulder_width": 0.00,
"right_hard_shoulder_width": 0.75,
},
"tertiary": {
"lanes": 1,
"lane_width": 3.50,
"left_hard_shoulder_width": 0.00,
"right_hard_shoulder_width": 0.75,
},
"unclassified": {
"lanes": 1,
"lane_width": 3.50,
"left_hard_shoulder_width": 0.00,
"right_hard_shoulder_width": 0.00,
},
"residential": {
"lanes": 1,
"lane_width": 3.50,
"left_hard_shoulder_width": 0.00,
"right_hard_shoulder_width": 0.75,
},
"service": {
"lanes": 1,
"lane_width": 3.00,
"left_hard_shoulder_width": 0.00,
"right_hard_shoulder_width": 0.00,
},
"motorway_link": {
"lanes": 2,
"lane_width": 3.75,
"left_hard_shoulder_width": 0.75,
"right_hard_shoulder_width": 3.00,
},
"trunk_link": {
"lanes": 2,
"lane_width": 3.75,
"left_hard_shoulder_width": 0.50,
"right_hard_shoulder_width": 1.50,
},
"primary_link": {
"lanes": 1,
"lane_width": 3.50,
"left_hard_shoulder_width": 0.00,
"right_hard_shoulder_width": 0.75,
},
"secondary_link": {
"lanes": 1,
"lane_width": 3.50,
"left_hard_shoulder_width": 0.00,
"right_hard_shoulder_width": 0.75,
},
"tertiary_link": {
"lanes": 1,
"lane_width": 3.50,
"left_hard_shoulder_width": 0.00,
"right_hard_shoulder_width": 0.00,
},
}
road_filter = set(highway_attributes.keys())
EARTH_MEAN_RADIUS = 6371004.0
def __init__(self, out, batch):
super().__init__()
self.storage = FeatureStorage(out, batch)
def way(self, w):
if "highway" not in w.tags:
return
if w.tags["highway"] not in self.road_filter:
return
left_hard_shoulder_width = self.highway_attributes[w.tags["highway"]]["left_hard_shoulder_width"]
lane_width = self.highway_attributes[w.tags["highway"]]["lane_width"]
lanes = self.highway_attributes[w.tags["highway"]]["lanes"]
right_hard_shoulder_width = self.highway_attributes[w.tags["highway"]]["right_hard_shoulder_width"]
if "oneway" not in w.tags:
lanes = lanes * 2
elif w.tags["oneway"] == "no":
lanes = lanes * 2
if "lanes" in w.tags:
try:
# Roads have at least one lane; guard against data issues.
lanes = max(int(w.tags["lanes"]), 1)
# Todo: take into account related lane tags
# https://wiki.openstreetmap.org/wiki/Tag:busway%3Dlane
# https://wiki.openstreetmap.org/wiki/Tag:cycleway%3Dlane
# https://wiki.openstreetmap.org/wiki/Key:parking:lane
except ValueError:
print("Warning: invalid feature: https://www.openstreetmap.org/way/{}".format(w.id), file=sys.stderr)
road_width = left_hard_shoulder_width + lane_width * lanes + right_hard_shoulder_width
if "width" in w.tags:
try:
# At least one meter wide, for road classes specified above
road_width = max(float(w.tags["width"]), 1.0)
# Todo: handle optional units such as "2 m"
# https://wiki.openstreetmap.org/wiki/Key:width
except ValueError:
print("Warning: invalid feature: https://www.openstreetmap.org/way/{}".format(w.id), file=sys.stderr)
geometry = geojson.LineString([(n.lon, n.lat) for n in w.nodes])
shape = shapely.geometry.shape(geometry)
geometry_buffer = shape.buffer(math.degrees(road_width / 2.0 / self.EARTH_MEAN_RADIUS))
if shape.is_valid:
feature = geojson.Feature(geometry=shapely.geometry.mapping(geometry_buffer))
self.storage.add(feature)
else:
print("Warning: invalid feature: https://www.openstreetmap.org/way/{}".format(w.id), file=sys.stderr)
def flush(self):
self.storage.flush()