-
Notifications
You must be signed in to change notification settings - Fork 3
/
nseg_tools.py
69 lines (62 loc) · 2.16 KB
/
nseg_tools.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
# keep_end_stub()
#
# For short end stubs, return True if we want to keep them anyway depending on type
#
def keep_end_stub(way):
# in Stockholm dataset a number of steps in stub ends have been observed
return way.tags.get("highway", None) == "steps"
# way_may_be_reversed()
#
# Return True if the tags allow that the way is reversed, otherwise False
#
def way_may_be_reversed(way):
return "oneway" not in way.tags and way.tags.get("junction", None) != "roundabout" and way.tags.get("highway", None) != "steps"
# get_directional_nodes()
#
# get a dictionary of all nodes that depend on the direction of the way it is tied to.
#
def get_directional_nodes(point_db):
directional_nodes = {}
for nodes in point_db.values():
for node in nodes:
if "direction" in node.tags:
directional_nodes[node.way] = node
return directional_nodes
# reverse_way()
#
# Reverse way, changing tags if necessary
#
def reverse_way(way, directional_nodes):
way.way = list(reversed(way.way))
for p in way.way:
if p in directional_nodes:
node = directional_nodes[p]
if node.tags["direction"] == "backward":
node.tags["direction"] = "forward"
elif node.tags["direction"] == "forward":
node.tags["direction"] = "backward"
new_tags = {}
for k, v in way.tags.items():
if k == "oneway":
if v == "yes":
v = -1
elif v == -1:
v = "yes"
else:
raise RuntimeError("Cannot reverse oneway")
elif k == "direction":
# direction normally used on points only, but here also check for ways to be sure
if v == "forward":
v = "backward"
elif v == "backward":
v = "forward"
elif ":forward" in k:
k = k.replace(":forward", ":backward")
elif ":backward" in k:
k = k.replace(":backward", ":forward")
elif ":left" in k:
k = k.replace(":left", ":right")
elif ":right" in k:
k = k.replace(":right", ":left")
new_tags[k] = v
way.tags = new_tags