OpenDroneMap-ODM/scripts/odm_dem.py

154 wiersze
6.8 KiB
Python
Czysty Zwykły widok Historia

2017-06-27 16:43:36 +00:00
import ecto, os, json
from shutil import copyfile
2017-06-23 15:20:46 +00:00
from opendm import io
from opendm import log
from opendm import system
from opendm import context
from opendm import types
class ODMDEMCell(ecto.Cell):
2017-06-23 15:20:46 +00:00
def declare_params(self, params):
params.declare("verbose", 'print additional messages to console', False)
def declare_io(self, params, inputs, outputs):
inputs.declare("tree", "Struct with paths", [])
2017-06-23 15:20:46 +00:00
inputs.declare("args", "The application arguments.", {})
2017-06-23 19:28:46 +00:00
inputs.declare("reconstruction", "list of ODMReconstructions", [])
2017-06-23 15:20:46 +00:00
def process(self, inputs, outputs):
# Benchmarking
start_time = system.now_raw()
log.ODM_INFO('Running ODM DEM Cell')
# get inputs
args = self.inputs.args
tree = self.inputs.tree
las_model_found = io.file_exists(tree.odm_georeferencing_model_las)
2017-06-29 15:00:52 +00:00
env_paths = [context.superbuild_bin_path]
2017-06-23 19:28:46 +00:00
# Just to make sure
l2d_module_installed = True
try:
system.run('l2d_classify --help > /dev/null', env_paths)
2017-06-23 19:28:46 +00:00
except:
log.ODM_WARNING('lidar2dems is not installed properly')
l2d_module_installed = False
log.ODM_INFO('Create DSM: ' + str(args.dsm))
log.ODM_INFO('Create DTM: ' + str(args.dtm))
log.ODM_INFO('DEM input file {0} found: {1}'.format(tree.odm_georeferencing_model_las, str(las_model_found)))
2017-06-23 19:28:46 +00:00
# Do we need to process anything here?
if (args.dsm or args.dtm) and las_model_found and l2d_module_installed:
2017-06-23 19:28:46 +00:00
# define paths and create working directories
odm_dem_root = tree.path('odm_dem')
system.mkdir_p(odm_dem_root)
2017-06-27 16:43:36 +00:00
dsm_output_filename = os.path.join(odm_dem_root, 'dsm.tif')
dtm_output_filename = os.path.join(odm_dem_root, 'dtm.tif')
2017-06-23 19:28:46 +00:00
# check if we rerun cell or not
rerun_cell = (args.rerun is not None and
args.rerun == 'odm_dem') or \
(args.rerun_all) or \
(args.rerun_from is not None and
'odm_dem' in args.rerun_from)
if (args.dtm and not io.file_exists(dtm_output_filename)) or \
(args.dsm and not io.file_exists(dsm_output_filename)) or \
rerun_cell:
2018-01-04 17:08:59 +00:00
2017-06-27 16:43:36 +00:00
# Process with lidar2dems
2017-06-23 19:28:46 +00:00
terrain_params_map = {
'flatnonforest': (1, 3),
'flatforest': (1, 2),
'complexnonforest': (5, 2),
'complexforest': (10, 2)
}
terrain_params = terrain_params_map[args.dem_terrain_type.lower()]
kwargs = {
'verbose': '-v' if self.params.verbose else '',
'slope': terrain_params[0],
'cellsize': terrain_params[1],
2017-06-27 16:43:36 +00:00
'outdir': odm_dem_root,
'site': ''
2017-06-23 19:28:46 +00:00
}
if args.crop > 0:
2018-01-04 17:08:59 +00:00
bounds_shapefile_path = os.path.join(tree.odm_georeferencing, 'odm_georeferenced_model.bounds.shp')
if os.path.exists(bounds_shapefile_path):
kwargs['site'] = '-s {}'.format(bounds_shapefile_path)
2017-06-23 19:28:46 +00:00
l2d_params = '--slope {slope} --cellsize {cellsize} ' \
'{verbose} ' \
'-o {site} ' \
2017-06-23 19:28:46 +00:00
'--outdir {outdir}'.format(**kwargs)
approximate = '--approximate' if args.dem_approximate else ''
# Classify only if we need a DTM
run_classification = args.dtm
if run_classification:
system.run('l2d_classify {0} --decimation {1} '
2017-07-06 23:40:03 +00:00
'{2} --initialDistance {3} {4}'.format(
2017-07-07 00:38:21 +00:00
l2d_params, args.dem_decimation, approximate,
args.dem_initial_distance, tree.odm_georeferencing), env_paths)
else:
log.ODM_INFO("Will skip classification, only DSM is needed")
2018-01-04 17:08:59 +00:00
l2d_classified_pattern = 'odm_georeferenced_model.bounds-0_l2d_s{slope}c{cellsize}.las' if args.crop > 0 else 'l2d_s{slope}c{cellsize}.las'
copyfile(tree.odm_georeferencing_model_las, os.path.join(odm_dem_root, l2d_classified_pattern.format(**kwargs)))
2017-06-23 19:28:46 +00:00
products = []
if args.dsm: products.append('dsm')
if args.dtm: products.append('dtm')
radius_steps = [args.dem_resolution]
for _ in range(args.dem_gapfill_steps - 1):
radius_steps.append(radius_steps[-1] * 3) # 3 is arbitrary, maybe there's a better value?
2017-06-23 19:28:46 +00:00
for product in products:
demargs = {
'product': product,
'indir': odm_dem_root,
'l2d_params': l2d_params,
'maxsd': args.dem_maxsd,
'maxangle': args.dem_maxangle,
'resolution': args.dem_resolution,
'radius_steps': ' '.join(map(str, radius_steps)),
'gapfill': '--gapfill' if args.dem_gapfill_steps > 0 else '',
# If we didn't run a classification, we should pass the decimate parameter here
'decimation': '--decimation {0}'.format(args.dem_decimation) if not run_classification else ''
2017-06-23 15:20:46 +00:00
}
2017-06-23 19:28:46 +00:00
system.run('l2d_dems {product} {indir} {l2d_params} '
'--maxsd {maxsd} --maxangle {maxangle} '
'--resolution {resolution} --radius {radius_steps} '
'{decimation} '
'{gapfill} '.format(**demargs), env_paths)
2017-06-23 15:20:46 +00:00
2017-06-27 16:43:36 +00:00
# Rename final output
if product == 'dsm':
2018-01-04 17:08:59 +00:00
dsm_pattern = 'odm_georeferenced_model.bounds-0_dsm.idw.tif' if args.crop > 0 else 'dsm.idw.tif'
os.rename(os.path.join(odm_dem_root, dsm_pattern), dsm_output_filename)
2017-06-27 16:43:36 +00:00
elif product == 'dtm':
2018-01-04 17:08:59 +00:00
dtm_pattern = 'odm_georeferenced_model.bounds-0_dsm.idw.tif' if args.crop > 0 else 'dtm.idw.tif'
os.rename(os.path.join(odm_dem_root, dtm_pattern), dtm_output_filename)
2017-06-23 19:28:46 +00:00
else:
log.ODM_WARNING('Found existing outputs in: %s' % odm_dem_root)
2017-06-23 15:20:46 +00:00
else:
2017-06-23 19:28:46 +00:00
log.ODM_WARNING('DEM will not be generated')
2017-06-23 15:20:46 +00:00
if args.time:
2017-06-23 19:28:46 +00:00
system.benchmark(start_time, tree.benchmarking, 'Dem')
2017-06-23 15:20:46 +00:00
2017-06-23 19:28:46 +00:00
log.ODM_INFO('Running ODM DEM Cell - Finished')
return ecto.OK if args.end_with != 'odm_dem' else ecto.QUIT