-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-batch.py
348 lines (276 loc) · 15.1 KB
/
generate-batch.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
import os
# Set imports based on environment and packages
# import gdal
# from gdalconst import GA_ReadOnly
from osgeo import gdal
from osgeo import gdalconst
from osgeo.gdalconst import GA_ReadOnly
import json
import sys
import argparse
# File directory structure
#
# >templates/
# >TEMPLATESHORTNAME/
# -TEMPLATESHORTNAME.json
# -TEMPLATESHORTNAME.sh
# -rasterPixelFunctions.py
# >template-workspace/ <-working directory for each template
# >TEMPLATESHORTNAME/DEMRES/
# >CALCRASTERTYPES[:]/
# -XX.tif
# -clipRaster-batch.sh <-generated script to run gdalwarp
# >touch_terrain_configs/
# -XX.json <-generated config JSON file for each state
# -meshGeneration-batch.sh <-generated script to run Touch Terrain
# -meshBoolean-batch.sh <-generated script to run libigl# >state-stls/
# >TEMPLATESHORTNAME/DEMRES/
# >XX-RASTERTYPESUFFIXES[:]/
# -XX_tile_1_1.tif
# Run this python script in top level dir directory
# e.g. python ./generate-batch.py ./templates/TEMPLATESHORTNAME/TEMPLATESHORTNAME.json -gc -gt
# python ./generate-batch.py ./templates/
parser = argparse.ArgumentParser(description='touch terrain config generation')
parser.add_argument(
'template', help='Path to template configuration that will be used.')
parser.add_argument('-gc', action='store_true')
parser.add_argument('-gt', action='store_true')
args = parser.parse_args()
templateWorkspaceAll = './template-workspace/'
#calcRasterBatchFilename = 'calcRaster-batch.sh'
clipRasterBatchFilename = 'clipRaster-batch.sh'
meshGenerationBatchFilename = 'meshGeneration-batch.sh'
meshBooleanBatchFilename = 'meshBoolean-batch.sh'
meshBooleanBin = './tools/gp-cli/precompiled/pc/bin/meshboolean.exe'
templateConfigurationPath = args.template
# use tt template config
# templateConfigurationPath = './touch_terrain_configuration_templates/individual-states.json'
templateRequiredKeys = [
'templateName',
'templateShortName',
'parentTemplates',
'demRes',
'calcRasterTypes',
'clipBoundariesDir',
'rasterTypeSuffixes',
'excludeFileList',
'targetSRS',
'1mmToRealScale'
]
templateConfig = {}
templateName = ''
templateDEMRes = -1
if templateConfigurationPath:
templateFp = open(templateConfigurationPath, 'r')
templateConfig = json.load(templateFp)
if isinstance(templateConfig, dict):
for rK in templateRequiredKeys:
if rK not in templateConfig:
print(
f'Template - {templateConfigurationPath} is missing {rK}\n')
sys.exit()
def inheritFromParent(parentPath):
pConfig = json.load(open(parentPath, 'r'))
if isinstance(pConfig, dict):
for pT in pConfig['parentTemplates']:
inheritFromParent(pT)
for pK in pConfig.keys():
if pK not in templateConfig.keys():
templateConfig[pK] = pConfig[pK]
for pT in templateConfig['parentTemplates']:
inheritFromParent(pT)
templateName = templateConfig['templateName']
templateDEMRes = templateConfig['demRes']
print(f'Loaded config template {templateName}')
print(f'Using {len(templateConfig["calcRasterTypes"])} input paths')
else:
print(f'\nTemplate - {templateConfigurationPath} is not dict\n')
sys.exit()
#Create full output paths to specific template workspace
templateWorkspacePath = templateWorkspaceAll + templateConfig['templateShortName'] + '/' + str(templateDEMRes) + '/'
clipRasterBatchPath = templateWorkspacePath + clipRasterBatchFilename
meshGenerationBatchPath = templateWorkspacePath + meshGenerationBatchFilename
meshBooleanBatchPath = templateWorkspacePath + meshBooleanBatchFilename
def clippedRastersPath(i):
p = templateWorkspacePath + '/' + \
templateConfig['calcRasterTypes'][i]
p += '/'
return p
print(f'Getting all file name listing from directory {clippedRastersPath(0)}')
configTemplatePath = f'{templateWorkspacePath}/touch_terrain_configs/'
def delete_files_in_directory(directory_path):
try:
files = os.listdir(directory_path)
for file in files:
file_path = os.path.join(directory_path, file)
if os.path.isfile(file_path):
os.remove(file_path)
except OSError:
print("Error occurred while deleting files.")
# clip raster to boundaries
if args.gc:
for i in range(0,len(templateConfig['calcRasterTypes'])):
outputPath = clippedRastersPath(i)
os.makedirs(os.path.dirname(outputPath), exist_ok=True)
print(f'Created directory {outputPath}')
with open(clipRasterBatchPath, 'w+') as cmdfp:
cmdCount = 0
for entry in os.scandir(templateConfig['clipBoundariesDir']):
if entry.name.endswith('.gpkg') and entry.is_file():
# print(entry.path)
forwardSlashPath = entry.path.replace(os.sep, '/')
entryName = entry.name.replace(".gpkg", "")
if entryName in templateConfig['excludeFileList']:
continue
print(entry)
for i in range(0, len(templateConfig['calcRasterTypes'])):
clipRasterCmd = f'gdalwarp \
-overwrite \
-t_srs {templateConfig["targetSRS"]} \
-of GTiff \
-tr {str(templateDEMRes)} {str(templateDEMRes)} \
-cutline {forwardSlashPath} \
-crop_to_cutline \
{templateWorkspacePath}/{templateConfig["calcRasterTypes"][i]}.tif \
{clippedRastersPath(i)}{entryName}.tif \
-r near \
-multi \
-dstnodata -9999 \
-co "TILED=YES" -co "COMPRESS=ZSTD" -co "PREDICTOR=2" -co "BIGTIFF=IF_SAFER" -co "NUM_THREADS=ALL_CPUS" -wo "NUM_THREADS=ALL_CPUS"'
cmdfp.write(clipRasterCmd + '\n')
cmdCount += 1
print(f'{cmdCount} clip command generated in {clipRasterBatchPath}')
if args.gt:
delete_files_in_directory(configTemplatePath)
os.makedirs(os.path.dirname(configTemplatePath), exist_ok=True)
print(f'Created directory {configTemplatePath}')
print(os.path.basename(templateConfigurationPath).split(".")[0])
outputSTLTopDir = './state_stls/'
outputSTLTemplateDir = f'{outputSTLTopDir}{templateConfig["templateShortName"]}/{templateDEMRes}/'
os.makedirs(os.path.dirname('./tmp/' + outputSTLTemplateDir), exist_ok=True)
print(f'Created directory {"./tmp/" + outputSTLTemplateDir}')
with open(meshGenerationBatchPath, 'w+') as cmdGenfp, open(meshBooleanBatchPath, 'w+') as cmdBoolfp:
configFileCount = 0
# use first path for master all file list
# Process states by largest TIF first to optimize 3d model generation time
# Small states should finish first and the larger states which need the most memoery will trickle in at the end.
allFiles = os.listdir(clippedRastersPath(0))
allFiles.sort(key=lambda f: os.stat(
clippedRastersPath(0)+f).st_size, reverse=False)
for entry in allFiles:
if entry.endswith('.tif'):
entryName = entry.replace(".tif", "")
if entryName in templateConfig['excludeFileList']:
continue
print(entry)
# Get TIF extents to 3d print
data = gdal.Open(
clippedRastersPath(0)+entry, GA_ReadOnly)
geoTransform = data.GetGeoTransform()
minx = geoTransform[0]
maxy = geoTransform[3]
maxx = minx + geoTransform[1] * data.RasterXSize
miny = maxy + geoTransform[5] * data.RasterYSize
# print([minx, miny, maxx, maxy])
data = None
# Create ttArgs dictionary to save to configuration json file
# TouchTerrain_standalone.py should be run from the "geographic-data/state_stl" directory
ttArgs = {
"importedDEM": '',
"DEM_name": 'USGS/NED',
"trlat": maxy, # lat/lon of top right corner
"trlon": maxx,
"bllat": miny, # lat/lon of bottom left corner
"bllon": minx,
# individual states
# width of each tile in mm (total width of TIF extent in meters divided by 500km = number of "200mm wide buildplates" needed at our 0.4mm = 1km scale)
# "tilewidth": 200 * ( maxx - minx ) / 500000,
# 5x size
# "tilewidth": 5*200 * ( maxx - minx ) / 500000,
# resolution (horizontal) of 3D printer (= size of one pixel) in mm
"printres": -1,
# oahu
# "basethick": 0, # thickness (in mm) of printed base
# individual states
# "basethick": 0.7, # thickness (in mm) of printed base
# "fill_holes": [-1, 7],
# "zscale": 5, # elevation (vertical) scaling
# usa 48 state combined width 200mm buildplate is 5000km, 0.4mm = 10km
# "tilewidth": 200 * ( maxx - minx ) / 5000000,
# "printres": -1,
# "basethick": 5, # thickness (in mm) of printed base
# "zscale": 50, # elevation (vertical) scaling
# "fill_holes": [-1, 8],
# USA 48 combined low poly
# 0.4mm = 10km
# "tilewidth": 200 * (maxx - minx) / 5000000,
# "printres": -1,
# thickness (in mm) of printed base
# "basethick": 2,
# "zscale": 100, # elevation (vertical) scaling
# "fill_holes": [-1, 8],
# "ignore_leq": -100,
# "min_elev": -100, # lowest point in NA is greater than -100m
# "smooth_borders": True,
# number of tiles in x and y. We are creating 1 big 3D model at our desired scale that we will custom divide to fit on the printer.
"ntilesx": 1,
"ntilesy": 1,
# indidivudal states
# "ignore_leq": -100,
# "min_elev": -100, #lowest point in NA is greater than -100m
# "smooth_borders": False,
# oahu
# "ignore_leq": -125,
# "min_elev": -125,
# "fill_holes": [-1, 7],
# format of 3D model files: "obj" wavefront obj (ascii),"STLa" ascii STL or "STLb" binary STL
"fileformat": "STLb",
# True-> all tiles are centered around 0/0, False, all tiles "fit together"
"tile_centered": False,
"zip_file_name": '', # base name of zipfile, .zip will be added
# 0 means all cores, None (null in JSON!) => don't use multiprocessing
"CPU_cores_to_use": 0,
# if raster is bigger, use temp_files instead of memory
"max_cells_for_memory_only": 5000**2,
# "lower_leq": [1,0.3],
# "offset_masks_lower": [["./dems/stream-lake-mask-clipped-500m/" + entry, 1.7]],
"clean_diags": True
}
# overwrite args with template values
for tK, tV in templateConfig.items():
if tK == '1mmToRealScale':
# (meters of real world)/((1mmtoRealScalemm)/(1000mmin1m))
ttArgs["tilewidth"] = (maxx - minx) / (tV / 1000)
elif tK in templateRequiredKeys: # skip putting template metadata keys in the args passed to touchterrain
continue
else:
ttArgs[tK] = tV
def generateZipFilenameForPathIndex(i):
return outputSTLTemplateDir + entryName + templateConfig['rasterTypeSuffixes'][i]
for i in range(0, len(templateConfig['calcRasterTypes'])):
ttArgs["importedDEM"] = clippedRastersPath(i) + entry
ttArgs["zip_file_name"] = generateZipFilenameForPathIndex(
i)
configFilename = entryName + \
templateConfig['rasterTypeSuffixes'][i] + '.json'
with open(configTemplatePath + configFilename, 'w+') as fp:
json.dump(ttArgs, fp, indent=0, sort_keys=True)
configFileCount += 1
cmdGenfp.write(
f'python ./TouchTerrain_standalone.py {configTemplatePath + configFilename}' + '\n')
if len(templateConfig['calcRasterTypes']) >= 2:
# Write libigl gp-CLI command for boolean subtract between second and first STL
cmdBoolfp.write(f'echo {configFileCount} Mesh boolean subtracting {entryName}' + '\n' +
f'time {meshBooleanBin} {generateZipFilenameForPathIndex(1)}/{entryName}_tile_1_1.STL {generateZipFilenameForPathIndex(0)}/{entryName}_tile_1_1.STL minus {generateZipFilenameForPathIndex(0)}/{entryName}_rivers.STL' + '\n' + f'echo {entryName} result $?' + '\n')
if len(templateConfig['calcRasterTypes']) > 2:
# Write libigl gp-CLI command for boolean subtract between second and third STL
cmdBoolfp.write(f'echo {configFileCount} Mesh boolean subtracting {entryName}' + '\n' +
f'time {meshBooleanBin} {generateZipFilenameForPathIndex(1)}/{entryName}_tile_1_1.STL {generateZipFilenameForPathIndex(2)}/{entryName}_tile_1_1.STL minus {generateZipFilenameForPathIndex(2)}/{entryName}_thru_rivers.STL' + '\n' + f'echo {entryName} result $?' + '\n')
print(
f'Wrote {str(configFileCount)} config files to {configTemplatePath}. Batch mesh generation script in {meshGenerationBatchPath}')
print(f'{meshGenerationBatchPath} should be run from {os.getcwd()}')
print(
f'Each state will be unzipped into its own folder in {outputSTLTopDir} relative to where batch file is run from.')
print(f'Run {meshBooleanBatchPath} after generating 3d state stls to perform boolean subtract to create rivers only 3d model')
print(f'Ex: \nchmod 700 touch-terrain-batch.sh\n./touch-terrain-batch.sh\nnice -n 5 ./{meshBooleanBatchPath}')
print('\nClean up nonmanifold meshes afterward with blender 3d print toolbox.')