-
Notifications
You must be signed in to change notification settings - Fork 1
/
osm_download_divide_building.py
400 lines (333 loc) · 13 KB
/
osm_download_divide_building.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
import bpy
import os
import shutil
import sys
import argparse
import numpy as np
import xml.etree.ElementTree as ET
def filter_and_merge_path_road(object_filepath):
bpy.ops.object.select_all(action='DESELECT')
count = 0
for obj in bpy.context.scene.objects:
if ('path' in obj.name or 'road' in obj.name) and 'building' not in obj.name:
obj.select_set(True)
count += 1
if bpy.context.selected_objects:
bpy.context.view_layer.objects.active = bpy.context.selected_objects[0]
bpy.ops.object.join()
merged_obj = bpy.context.object
merged_obj.name = 'map.osm_path_road'
merged_obj.data.name = 'map.osm_path_road'
if count>0:
bpy.ops.export_scene.obj(filepath=object_filepath, use_selection=True)
def filter_and_merge_vegetation(object_filepath):
bpy.ops.object.select_all(action='DESELECT')
count = 0
for obj in bpy.context.scene.objects:
if 'vegetation' in obj.name and 'building' not in obj.name:
obj.select_set(True)
count += 1
print('veg+1')
if bpy.context.selected_objects:
bpy.context.view_layer.objects.active = bpy.context.selected_objects[0]
bpy.ops.object.join()
merged_obj = bpy.context.object
merged_obj.name = 'map.osm_vegetation'
merged_obj.data.name = 'map.osm_vegetation'
print('veg num:',count)
if count>0:
bpy.ops.export_scene.obj(filepath=object_filepath, use_selection=True)
def filter_and_merge_forest(object_filepath):
bpy.ops.object.select_all(action='DESELECT')
count = 0
for obj in bpy.context.scene.objects:
if 'forest' in obj.name and 'building' not in obj.name:
obj.select_set(True)
count += 1
print('forest+1')
if bpy.context.selected_objects:
bpy.context.view_layer.objects.active = bpy.context.selected_objects[0]
bpy.ops.object.join()
merged_obj = bpy.context.object
merged_obj.name = 'map.osm_forest'
merged_obj.data.name = 'map.osm_forest'
print('forest num:',count)
if count>0:
bpy.ops.export_scene.obj(filepath=object_filepath, use_selection=True)
def filter_and_merge_water(object_filepath):
bpy.ops.object.select_all(action='DESELECT')
count = 0
for obj in bpy.context.scene.objects:
if 'water' in obj.name and 'building' not in obj.name:
obj.select_set(True)
count += 1
print('water+1')
if bpy.context.selected_objects:
bpy.context.view_layer.objects.active = bpy.context.selected_objects[0]
bpy.ops.object.join()
merged_obj = bpy.context.object
merged_obj.name = 'map.osm_water'
merged_obj.data.name = 'map.osm_water'
print('water num:',count)
if count>0:
bpy.ops.export_scene.obj(filepath=object_filepath, use_selection=True)
def filter_and_merge_area(object_filepath):
bpy.ops.object.select_all(action='DESELECT')
count = 0
for obj in bpy.context.scene.objects:
if 'area' in obj.name and 'building' not in obj.name:
obj.select_set(True)
count += 1
if bpy.context.selected_objects:
bpy.context.view_layer.objects.active = bpy.context.selected_objects[0]
bpy.ops.object.join()
merged_obj = bpy.context.object
merged_obj.name = 'map.osm_area'
merged_obj.data.name = 'map.osm_area'
if count>0:
bpy.ops.export_scene.obj(filepath=object_filepath, use_selection=True)
def merge_objects():
bpy.ops.object.select_all(action='DESELECT')
for obj in bpy.context.scene.objects:
if obj.type == 'MESH':
obj.select_set(True)
bpy.context.view_layer.objects.active = bpy.context.selected_objects[0]
bpy.ops.object.join()
def extract_building_heights(osm_file, default_building_height, default_building_level):
tree = ET.parse(osm_file)
root = tree.getroot()
building_dict = {}
building_counter = 1
for way in root.findall('way'):
building = False
height = None
levels = None
# Check for "building" tags
for tag in way.findall('tag'):
if tag.attrib['k'] == 'building':
building = True
if tag.attrib['k'] == 'height':
height = float(tag.attrib['v'])
if tag.attrib['k'] == 'building:levels':
if tag.attrib['v'].isdigit():
levels = int(tag.attrib['v'])
if building:
building_name = f"buildings.{building_counter:03d}"
if height is not None:
# If height is explicitly provided
building_dict[building_name] = height
elif levels is not None:
# If building:levels exists, calculate height
building_dict[building_name] = levels * default_building_height
else:
# Use default levels and default height
building_dict[building_name] = default_building_level * default_building_height
building_counter += 1
return building_dict
def download_area(min_lat, max_lat, min_long, max_long, fp):
bpy.data.scenes["Scene"].blosm.maxLat = max_lat
bpy.data.scenes["Scene"].blosm.minLat = min_lat
bpy.data.scenes["Scene"].blosm.maxLon = max_long
bpy.data.scenes["Scene"].blosm.minLon = min_long
bpy.ops.blosm.import_data()
bpy.ops.export_scene.obj(filepath=fp)
def calculate_scene_center():
obj_locations = []
for obj in bpy.context.scene.objects:
if obj.type == 'MESH':
obj_locations.append(np.array(obj.location))
if obj_locations:
center = np.mean(obj_locations, axis=0)
return center
return np.array([0.0, 0.0, 0.0])
def download_divide(max_lat, min_lat, max_long, min_long, obj_save_path, osm_save_path, mtl_file, png_file, addonpath, asset_save_path,export_path, default_level_height, default_building_level):
fp = obj_save_path + 'map.obj'
if not os.path.exists(obj_save_path):
os.makedirs(obj_save_path)
if not os.path.exists(osm_save_path):
os.makedirs(osm_save_path)
bpy.ops.preferences.addon_install(filepath=addonpath)
bpy.ops.preferences.addon_enable(module="blosm")
bpy.context.preferences.addons['blosm'].preferences.dataDir = osm_save_path
bpy.context.preferences.addons['blosm'].preferences.assetsDir = asset_save_path
bpy.context.preferences.addons['blosm'].preferences.googleMapsApiKey = ""
bpy.data.scenes["Scene"].blosm.dataType = 'osm'
bpy.data.scenes["Scene"].blosm.relativeToInitialImport = True
download_area(min_lat, max_lat, min_long, max_long, fp)
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete(use_global=False)
bpy.ops.import_scene.obj(filepath=fp)
imported_objects = bpy.context.selected_objects
print(imported_objects)
if not imported_objects:
print("Error: No objects were imported.")
else:
transform_data = {}
for obj in imported_objects:
print(obj.name)
bpy.context.view_layer.objects.active = obj
obj.select_set(True)
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.separate(type='LOOSE')
# print('SEP')
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.mesh.quads_convert_to_tris()
bpy.ops.object.mode_set(mode='OBJECT')
bpy.context.view_layer.update()
obj_loc = {}
osm_file = osm_save_path+'osm/map.osm'
building_heights = extract_building_heights(osm_file, default_level_height, default_building_level)
for obj in bpy.context.selected_objects:
if 'profile' not in obj.name:
obj_loc[obj.name] = obj.location
if 'buildings' in obj.name:
for b in building_heights.keys():
if b in obj.name:
break
obj.dimensions.y = building_heights[b]
bpy.context.view_layer.objects.active = obj
bpy.ops.object.mode_set(mode='OBJECT')
obj.select_set(True)
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='SELECT')
override = bpy.context.copy()
for area in bpy.context.screen.areas:
if area.type == 'IMAGE_EDITOR':
override['area'] = area
override['region'] = area.regions[-1]
override['space_data'] = area.spaces.active
break
if 'building' in obj.name:
bpy.ops.uv.smart_project(override)
bpy.ops.object.mode_set(mode='OBJECT')
if 'building' in obj.name:
object_dir = os.path.join(obj_save_path, obj.name)
if not os.path.exists(object_dir):
os.makedirs(object_dir)
object_filepath = os.path.join(object_dir, "mesh.obj")
bpy.context.view_layer.objects.active = obj
bpy.ops.object.select_all(action='DESELECT')
obj.select_set(True)
bpy.ops.export_scene.obj(filepath=object_filepath, use_selection=True)
shutil.copy(mtl_file, object_dir)
shutil.copy(png_file, object_dir)
print(obj_loc)
print(obj_loc)
bpy.context.view_layer.update()
#merge path and road
object_dir = os.path.join(obj_save_path, 'map.osm_path_road')
if not os.path.exists(object_dir):
os.makedirs(object_dir)
object_filepath = os.path.join(object_dir, "mesh.obj")
filter_and_merge_path_road(object_filepath)
#merge vegetation
object_dir = os.path.join(obj_save_path, 'map.osm_vegetation')
if not os.path.exists(object_dir):
os.makedirs(object_dir)
object_filepath = os.path.join(object_dir, "mesh.obj")
filter_and_merge_vegetation(object_filepath)
#merge forest
object_dir = os.path.join(obj_save_path, 'map.osm_forest')
if not os.path.exists(object_dir):
os.makedirs(object_dir)
object_filepath = os.path.join(object_dir, "mesh.obj")
filter_and_merge_forest(object_filepath)
#merge water
object_dir = os.path.join(obj_save_path, 'map.osm_water')
if not os.path.exists(object_dir):
os.makedirs(object_dir)
object_filepath = os.path.join(object_dir, "mesh.obj")
filter_and_merge_water(object_filepath)
#merge area
object_dir = os.path.join(obj_save_path, 'map.osm_area')
if not os.path.exists(object_dir):
os.makedirs(object_dir)
object_filepath = os.path.join(object_dir, "mesh.obj")
filter_and_merge_area(object_filepath)
bpy.context.view_layer.update()
if not os.path.exists(export_path):
os.makedirs(export_path)
bpy.ops.export_scene.obj(filepath=export_path+'mesh.obj')
if __name__ == '__main__':
opt = argparse.ArgumentParser()
#Blender params
opt.add_argument(
"--max_lat",
type=float,
default=40.001,
help="Max lat of OSM area",
)
opt.add_argument(
"--min_lat",
type=float,
default=39.998,
help="Min lat of OSM area",
)
opt.add_argument(
"--max_lon",
type=float,
default=116.327,
help="Max lon of OSM area",
)
opt.add_argument(
"--min_lon",
type=float,
default=116.326,
help="Min lon of OSM area",
)
opt.add_argument(
"--obj_save_path",
type=str,
default='',
help="Saving path of the obj file",
)
opt.add_argument(
"--osm_save_path",
type=str,
default='',
help="Saving path of the osm file",
)
opt.add_argument(
"--mtl_file",
type=str,
default='',
help="Mtl file path",
)
opt.add_argument(
"--png_file",
type=str,
default='',
help="Initial material file path",
)
opt.add_argument(
"--addonpath",
type=str,
default='',
help="Addon Blosm file path",
)
opt.add_argument(
"--asset_save_path",
type=str,
default="",
help="Asset save path",
)
opt.add_argument(
"--export_path",
type=str,
default="",
help="Export path",
)
opt.add_argument(
"--default_level_height",
type=float,
default=3.0
)
opt.add_argument(
"--default_building_level",
type=int,
default=3
)
argv = sys.argv[sys.argv.index("--") + 1 :]
opt = opt.parse_args(argv)
#=======Start downloading OSM files=======
download_divide(opt.max_lat, opt.min_lat, opt.max_lon, opt.min_lon, opt.obj_save_path, opt.osm_save_path, opt.mtl_file, opt.png_file, opt.addonpath, opt.asset_save_path,opt.export_path, opt.default_level_height, opt.default_building_level)