-
Notifications
You must be signed in to change notification settings - Fork 1
/
osm_download_divide_terrain.py
182 lines (153 loc) · 5.61 KB
/
osm_download_divide_terrain.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
import bpy
import os
import shutil
import sys
import argparse
import numpy as np
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):
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 = 'terrain'
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()
for obj in bpy.context.selected_objects:
if 'profile' not in obj.name:
if 'buildings' in obj.name:
obj.scale.y *= 3
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
bpy.ops.uv.smart_project(override)
bpy.ops.object.mode_set(mode='OBJECT')
if 'Terrain' 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)
bpy.context.view_layer.update()
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",
)
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)