-
Notifications
You must be signed in to change notification settings - Fork 0
/
svg_path_d.py
62 lines (47 loc) · 2.11 KB
/
svg_path_d.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
import svg
import mercator
class PathD:
def __init__(self, element):
match element["type"]:
case "node" | "way":
self.d = WayD(element["geometry"]).d
case "relation":
self.d = MultipolygonD(element["members"]).d
class WayD:
def __init__(self, geometry):
self.d = []
self.d.append(svg.M(**mercator.Mercator.node(geometry[0])))
for node in geometry[1:]:
self.d.append(svg.L(**mercator.Mercator.node(node)))
class MultipolygonD:
def __init__(self, members):
self.d = []
way = []
edge_nodes = {
"first": None,
"last": None,
}
while len(members) > 0:
for relation_member in members:
if relation_member["role"] in ("outer", "inner"):
if relation_member["type"] == "way":
relation_member_geometry = relation_member["geometry"]
# Check whether way has a common node with the previous one
if edge_nodes["last"] is not None:
if relation_member_geometry[-1] == edge_nodes["last"]:
relation_member_geometry.reverse()
elif relation_member_geometry[0] != edge_nodes["last"]:
continue
if edge_nodes["first"] is None:
edge_nodes["first"] = relation_member_geometry[0]
edge_nodes["last"] = relation_member_geometry[-1]
way.extend(relation_member_geometry)
if edge_nodes["first"] == edge_nodes["last"]:
self.d.extend(WayD(way).d)
self.d.append(svg.Z())
way = []
edge_nodes = {
"first": None,
"last": None,
}
members.remove(relation_member)