-
Notifications
You must be signed in to change notification settings - Fork 25
/
generateParkingAreasFromOSM.py
executable file
·296 lines (250 loc) · 10.2 KB
/
generateParkingAreasFromOSM.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
#!/usr/bin/env python3
""" Extract Parking Areas from OSM.
Author: Lara CODECA
This program and the accompanying materials are made available under the
terms of the Eclipse Public License 2.0 which is available at
http://www.eclipse.org/legal/epl-2.0.
"""
import argparse
import os
import sys
import xml.etree.ElementTree
from tqdm import tqdm
if "SUMO_HOME" in os.environ:
sys.path.append(os.path.join(os.environ["SUMO_HOME"], "tools"))
import sumolib
else:
sys.exit("please declare environment variable 'SUMO_HOME'")
def get_options(cmd_args=None):
"""Argument Parser"""
parser = argparse.ArgumentParser(
prog="generateParkingAreasFromOSM.py",
usage="%(prog)s [options]",
description="Extract Parking Areas from OSM.",
)
parser.add_argument(
"--osm", type=str, dest="osm_file", required=True, help="OSM file."
)
parser.add_argument(
"--net", type=str, dest="net_file", required=True, help="SUMO network file."
)
parser.add_argument(
"--out", type=str, dest="output", required=True, help="SUMO parking areas file."
)
parser.add_argument(
"--default-capacity",
type=int,
dest="default_capacity",
default=100,
help="Default parking areas capacity if the OSM tag is missing.",
)
parser.add_argument(
"--parking-len",
type=float,
dest="parking_len",
default=10.0,
help="Parking areas length.",
)
parser.add_argument(
"--parking-angle",
type=float,
dest="parking_angle",
default=45.0,
help="Parking areas angle.",
)
parser.add_argument(
"--distance-from-intersection",
type=float,
dest="intersection_buffer",
default=10.0,
help="Buffer area used to avoid having the parking entrance "
"too close to an intersection.",
)
return parser.parse_args(cmd_args)
class ParkingAreasFromOSMGenerator:
"""Generate the SUMO additional file for parkings based on OSM."""
def __init__(self, options):
self._options = options
self._osm = self._parse_xml_file(options.osm_file)
self._net = sumolib.net.readNet(options.net_file)
self._parkings_edges_dict = dict()
self._osm_parkings = dict()
self._sumo_parkings = dict()
def parkings_generation(self):
"""Main finction to generate all the parking areas."""
print("Filtering OSM for parking lot..")
self._filter_parkings()
print("Create parkings for SUMO..")
self._parkings_to_edges()
self._parkings_sumo()
def save_parkings_to_file(self, filename):
"""Save the generated parkings to file."""
self._save_parkings_to_file(filename)
@staticmethod
def _parse_xml_file(xml_file):
"""Extract all info from an OSM file."""
xml_tree = xml.etree.ElementTree.parse(xml_file).getroot()
dict_xml = {}
for child in xml_tree:
parsed = {}
for key, value in child.attrib.items():
parsed[key] = value
for attribute in child:
if attribute.tag in list(parsed.keys()):
parsed[attribute.tag].append(attribute.attrib)
else:
parsed[attribute.tag] = [attribute.attrib]
if child.tag in list(dict_xml.keys()):
dict_xml[child.tag].append(parsed)
else:
dict_xml[child.tag] = [parsed]
return dict_xml
def _filter_parkings(self):
"""Retrieve all the parking lots from a OSM structure."""
for node in tqdm(self._osm["node"]):
parking = False
if "tag" not in list(node.keys()):
continue
for tag in node["tag"]:
if self._is_parkings(tag):
parking = True
if parking:
x_coord, y_coord = self._net.convertLonLat2XY(node["lon"], node["lat"])
node["x"] = x_coord
node["y"] = y_coord
self._osm_parkings[node["id"]] = node
print("Gathered {} parking lots.".format(len(list(self._osm_parkings.keys()))))
_PARKING_DICT = {
"amenity": ["parking", "motorcycle_parking", "parking_entrance"],
"name": ["underground parking"],
"parking": ["surface", "underground", "multi-storey"],
"service": ["parking_aisle"],
}
def _is_parkings(self, tag):
"""Check if the tag matches to one of the possible parking lots."""
for key, value in self._PARKING_DICT.items():
if tag["k"] == key and tag["v"] in value:
return True
return False
def _parkings_to_edges(self):
"""Associate the parking-id to and edge-id in a dictionary."""
for parking in tqdm(self._osm_parkings.values()):
self._parkings_edges_dict[parking["id"]] = self._parking_to_edge(parking)
def _parking_to_edge(self, parking):
"""Given a parking lot, return the closest edge (lane_0) and all the other info
required by SUMO for the parking areas:
(edge_info, lane_info, location, parking.coords, parking.capacity)
"""
edge_info = None
lane_info = None
dist_lane = sys.float_info.max
location = None
radius = 50.0
while not edge_info:
nearest_edges = self._net.getNeighboringEdges(
parking["x"], parking["y"], r=radius
)
for edge, _ in nearest_edges:
if not (edge.allows("passenger") and edge.allows("pedestrian")):
continue
if self._is_too_short(edge.getLength()):
continue
# select the lane closer to the curb
selected_lane = None
for lane in edge.getLanes():
if not lane.allows("passenger"):
continue
selected_lane = lane
break
if selected_lane is not None:
pos, dist = selected_lane.getClosestLanePosAndDist(
(float(parking["x"]), float(parking["y"]))
)
if dist < dist_lane:
edge_info = edge
lane_info = selected_lane
dist_lane = dist
location = pos
radius += 50.0
if dist_lane > 50.0:
print(
"Alert: parking lots {} is {} meters from lane {}.".format(
parking["id"], dist_lane, lane_info.getID()
)
)
return (edge_info, lane_info, location)
def _is_too_short(self, edge_len):
"""Check if the edge type is appropriate for a parking lot."""
if edge_len < (
self._options.parking_len + 2 * self._options.intersection_buffer
):
return True
return False
def _get_capacity(self, parking_id):
"""Retrieve parking lot capacity from OSM."""
for tag in self._osm_parkings[parking_id]["tag"]:
if tag["k"] == "capacity":
try:
return int(tag["v"])
except ValueError:
print(
"Parking {} capacity is not an integer [{}].".format(
parking_id, tag["v"]
)
)
return self._options.default_capacity
print("Parking {} has no capacity tag.".format(parking_id))
return self._options.default_capacity
def _parkings_sumo(self):
"""Compute the parking lots stops location for SUMO."""
for plid, (_, lane, location) in self._parkings_edges_dict.items():
new_pl = {
"id": plid,
"lane": lane.getID(),
"start": location - self._options.parking_len / 2,
"end": location + self._options.parking_len / 2,
"capacity": self._get_capacity(plid),
"coords": (
float(self._osm_parkings[plid]["x"]),
float(self._osm_parkings[plid]["y"]),
),
}
if new_pl["start"] < self._options.intersection_buffer:
new_pl["start"] = self._options.intersection_buffer
new_pl["end"] = new_pl["start"] + self._options.parking_len
if new_pl["end"] > lane.getLength() - self._options.intersection_buffer:
new_pl["end"] = lane.getLength() - self._options.intersection_buffer
new_pl["start"] = new_pl["end"] - self._options.parking_len
self._sumo_parkings[plid] = new_pl
_ADDITIONALS_TPL = """<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with generateParkingAreasFromOSM.py [https://github.com/lcodeca/SUMOActivityGen] -->
<additional xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://sumo.dlr.de/xsd/additional_file.xsd"> {content}
</additional>
"""
_PARKINGS_TPL = """
<parkingArea id="{id}" lane="{lane}" startPos="{start}" endPos="{end}" roadsideCapacity="{capacity}" friendlyPos="true"/>""" # pylint: disable=C0301
def _save_parkings_to_file(self, filename):
"""Save the parking lots into a SUMO XML additional file."""
print("Creation of {}".format(filename))
with open(filename, "w") as outfile:
list_of_parkings = ""
for parking in self._sumo_parkings.values():
list_of_parkings += self._PARKINGS_TPL.format(
id=parking["id"],
lane=parking["lane"],
start=parking["start"],
end=parking["end"],
capacity=parking["capacity"],
)
content = list_of_parkings
outfile.write(self._ADDITIONALS_TPL.format(content=content))
print("{} created.".format(filename))
def main(cmd_args):
"""Extract Parking Areas from OSM."""
options = get_options(cmd_args)
parkings = ParkingAreasFromOSMGenerator(options)
parkings.parkings_generation()
parkings.save_parkings_to_file(options.output)
print("Done.")
if __name__ == "__main__":
main(sys.argv[1:])