-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathosm2maya.py
142 lines (89 loc) · 2.79 KB
/
osm2maya.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
import maya.cmds as cmds
import random
import xml.dom.minidom
# replace filepath with the absolute path of the OSM file you have downloaded
doc = xml.dom.minidom.parse(filepath)
# building:levels
all_ways = doc.getElementsByTagName("way")
buildings = []
for el in all_ways:
for ch in el.childNodes:
if ch.attributes:
if 'k' in ch.attributes.keys() and 'building' == ch.attributes['k'].value:
buildings.append(el)
break
nodes = doc.getElementsByTagName("node")
id_to_tuple = {}
for node in nodes:
id_val = node.attributes['id'].value
if 'lon' in node.attributes.keys():
(lon, lat) = (node.attributes['lon'].value, node.attributes['lat'].value)
id_to_tuple[id_val] = (lon, lat)
all_buildings = []
for b in buildings:
lst = []
nds = b.getElementsByTagName('nd')
for ch in nds:
if ch.tagName == 'nd':
node_id = ch.attributes['ref'].value
lst.append(id_to_tuple[node_id])
tags = b.getElementsByTagName('tag')
level = 1
for tag in tags:
if tag.tagName == 'tag':
if tag.attributes['k'].value == 'building:levels':
try:
level = int(tag.attributes['v'].value)
except:
level = 1
all_buildings.append((lst, level))
print(all_buildings[0])
all_buildings = sorted(all_buildings)
sz = len(all_buildings)
start_lon = float(all_buildings[int(sz/2)][0][0][0])
start_lat = float(all_buildings[int(sz/2)][0][0][1])
print(all_buildings[0])
def get_xy(lon, lat):
lon = float(lon)
lat = float(lat)
mul = 111.321 * 1000 # meters
diff_lon = lon - start_lon
diff_lat = lat - start_lat
return (diff_lon * mul, diff_lat*mul)
buildings_xy = []
for lst in all_buildings:
tmp = []
for i in lst[0]:
tmp.append(get_xy(i[0], i[1]))
# height froom levels
h = lst[1]
buildings_xy.append((tmp, h))
print(buildings_xy[0])
# Polygons ready, now use it below
cubeList = cmds.ls('myCube*')
if len(cubeList) > 0:
cmds.delete(cubeList)
all_poly = []
for lst in buildings_xy:
tmp = []
for i in lst[0]:
(x,z) = i
x/=-100
z/=100
y = 0
tmp.append((x,y,z))
h = lst[1]
res = cmds.polyCreateFacet( p=tmp, name='buildingpoly#')
all_poly.append(res)
thickness = random.uniform(0.1, 0.2)
assert h >= 0
normals = cmds.polyInfo(res[0], fn=1)
normal = float(normals[0].split(":")[1].split()[1])
# For reversed normals, Just redraw in anticlockwise
if normal < 0:
cmds.delete(res[0])
tmp.reverse()
res = cmds.polyCreateFacet( p=tmp, name='buildingpoly#')
normals = cmds.polyInfo(res[0], fn=1)
normal = float(normals[0].split(":")[1].split()[1])
cmds.polyExtrudeFacet(res[0], kft=1, thickness=(h / 10.0))