-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmapdata.py
More file actions
60 lines (52 loc) · 2.62 KB
/
mapdata.py
File metadata and controls
60 lines (52 loc) · 2.62 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
import overpass
class MapData():
def __init__(self) -> None:
self.api = overpass.API(debug=False)
self._format = "json"
def get_data(self, n: float, s: float, e: float, w: float) -> bool:
MapQuery = overpass.MapQuery(s, w, n, e)
# needed format to get both nodes and ways
self.data = self.api.get(MapQuery, responseformat = self._format)
def store_data(self, node_file_name: str, way_file_name: str) -> bool:
# TODO: Parsing done with C
nodes_to_remove = []
with open(node_file_name, 'w') as node_file, open(way_file_name, 'w') as way_file:
for element in self.data["elements"]:
# Firstly iterate ways to remove not wanted nodes
if element["type"] == "way":
# Must parse 'building' ways, since represent the boundaries of a building and don't connect
# to main road
#print(element)
if 'tags' in element.keys() and 'building' not in element['tags'].keys():
# If it's not a building, we can write the way down
line = "id=" + str(element["id"]) + ";" + \
";".join([str(node) for node in element["nodes"]]) + "\n"
way_file.write(line)
else:
# If it's a building, we will add its nodes to "nowrite" list
nodes_to_remove.extend(element["nodes"])
for element in self.data["elements"]:
# Parse building nodes
if element["type"] == "node" and element["id"] not in nodes_to_remove:
line = str(element["id"]) + ";" + \
str(element["lat"]) + ";" + \
str(element["lon"]) + "\n"
node_file.write(line)
#TODO: delete test method
def test(self):
for element in self.data["elements"]:
if element['id'] == 411117242:
if 'building' in element['tags'].keys():
print('building')
print(element['nodes'])
else:
print("nobuilding")
print(element, '\n')
#print(element['tags'].keys(), '\n')
if element['id'] == 4128489669:
print(element)
if __name__ == '__main__':
map_handler = MapData()
map_handler.get_data(34.0156618075, 33.7568941425, -118.2379236925, -118.30273380749999)
#map_handler.test()
map_handler.store_data("nodes.csv", "roads.csv")