OpenDroneMap-ODM/scripts/odm_georeferencing.py

196 wiersze
9.3 KiB
Python
Czysty Zwykły widok Historia

2015-12-01 16:25:14 +00:00
import ecto
2016-02-25 19:51:03 +00:00
import csv
2016-12-14 18:36:23 +00:00
import os
2015-12-01 16:25:14 +00:00
from opendm import io
from opendm import log
2015-12-10 17:17:39 +00:00
from opendm import types
2015-12-01 16:25:14 +00:00
from opendm import system
from opendm import context
2016-02-25 20:02:48 +00:00
2015-12-01 16:25:14 +00:00
class ODMGeoreferencingCell(ecto.Cell):
def declare_params(self, params):
params.declare("gcp_file", 'path to the file containing the ground control '
'points used for georeferencing.The file needs to '
'be on the following line format: \neasting '
'northing height pixelrow pixelcol imagename', 'gcp_list.txt')
params.declare("img_size", 'image size used in calibration', 2400)
params.declare("use_exif", 'use exif', False)
2017-04-06 13:06:09 +00:00
params.declare("dem", 'Generate a dem', False)
2017-04-06 19:37:13 +00:00
params.declare("sample_radius", "Minimum distance between samples for DEM gen", 3)
params.declare("gdal_res", "Length of raster cell edges in X/Y units ", 2)
params.declare("gdal_radius", "Radius about cell center bounding points to use to calculate a cell value", 0.5)
2016-12-11 22:16:11 +00:00
params.declare("verbose", 'print additional messages to console', False)
2015-12-01 16:25:14 +00:00
def declare_io(self, params, inputs, outputs):
inputs.declare("tree", "Struct with paths", [])
2015-12-01 16:25:14 +00:00
inputs.declare("args", "The application arguments.", {})
inputs.declare("photos", "list of ODMPhoto's", [])
inputs.declare("reconstruction", "list of ODMReconstructions", [])
outputs.declare("reconstruction", "list of ODMReconstructions", [])
2015-12-01 16:25:14 +00:00
def process(self, inputs, outputs):
2016-12-14 18:36:23 +00:00
# find a file in the root directory
def find(file, dir):
for root, dirs, files in os.walk(dir):
return '/'.join((root, file)) if file in files else None
2016-02-29 14:45:00 +00:00
# Benchmarking
start_time = system.now_raw()
2015-12-01 16:25:14 +00:00
log.ODM_INFO('Running ODM Georeferencing Cell')
2015-12-01 16:25:14 +00:00
# get inputs
args = self.inputs.args
tree = self.inputs.tree
2016-12-14 18:36:23 +00:00
gcpfile = io.join_paths(tree.root_path, self.params.gcp_file) \
if self.params.gcp_file else find('gcp_list.txt', tree.root_path)
geocreated = True
2016-12-11 22:16:11 +00:00
verbose = '-verbose' if self.params.verbose else ''
2015-12-01 16:25:14 +00:00
# define paths and create working directories
system.mkdir_p(tree.odm_georeferencing)
2015-12-01 16:25:14 +00:00
# in case a gcp file it's not provided, let's try to generate it using
# images metadata. Internally calls jhead.
2016-12-14 18:36:23 +00:00
log.ODM_DEBUG(self.params.gcp_file)
if not self.params.gcp_file: # and \
# not io.file_exists(tree.odm_georeferencing_coords):
2016-12-14 18:36:23 +00:00
log.ODM_WARNING('No coordinates file. '
'Generating coordinates file: %s'
2016-02-25 20:02:48 +00:00
% tree.odm_georeferencing_coords)
2016-12-14 18:36:23 +00:00
# odm_georeference definitions
kwargs = {
'bin': context.odm_modules_path,
'imgs': tree.dataset_resize,
'imgs_list': tree.opensfm_bundle_list,
'coords': tree.odm_georeferencing_coords,
'log': tree.odm_georeferencing_utm_log,
'verbose': verbose
2016-12-14 18:36:23 +00:00
}
# run UTM extraction binary
extract_utm = system.run_and_return('{bin}/odm_extract_utm -imagesPath {imgs}/ '
'-imageListFile {imgs_list} -outputCoordFile {coords} {verbose} '
2016-12-14 18:36:23 +00:00
'-logFile {log}'.format(**kwargs))
if extract_utm != '':
log.ODM_WARNING('Could not generate coordinates file. '
'Ignore if there is a GCP file. Error: %s'
% extract_utm)
2015-12-01 16:25:14 +00:00
# check if we rerun cell or not
rerun_cell = (args.rerun is not None and
args.rerun == 'odm_georeferencing') or \
(args.rerun_all) or \
(args.rerun_from is not None and
'odm_georeferencing' in args.rerun_from)
2016-02-25 19:51:03 +00:00
if not io.file_exists(tree.odm_georeferencing_model_obj_geo) or \
not io.file_exists(tree.odm_georeferencing_model_ply_geo) or rerun_cell:
2015-12-01 16:25:14 +00:00
# odm_georeference definitions
kwargs = {
'bin': context.odm_modules_path,
'bundle': tree.opensfm_bundle,
'imgs': tree.dataset_resize,
'imgs_list': tree.opensfm_bundle_list,
'model': tree.odm_textured_model_obj,
'log': tree.odm_georeferencing_log,
'coords': tree.odm_georeferencing_coords,
2016-02-25 19:51:03 +00:00
'pc_geo': tree.odm_georeferencing_model_ply_geo,
'geo_sys': tree.odm_georeferencing_model_txt_geo,
'model_geo': tree.odm_georeferencing_model_obj_geo,
'size': self.params.img_size,
2016-04-05 15:12:47 +00:00
'gcp': gcpfile,
2016-12-11 22:16:11 +00:00
'verbose': verbose
2015-12-01 16:25:14 +00:00
}
if not args.use_pmvs:
kwargs['pc'] = tree.opensfm_model
else:
kwargs['pc'] = tree.pmvs_model
2015-12-01 16:25:14 +00:00
2016-12-14 18:36:23 +00:00
# Check to see if the GCP file exists
if not self.params.use_exif and (self.params.gcp_file or find('gcp_list.txt', tree.root_path)):
2016-12-14 18:36:23 +00:00
log.ODM_INFO('Found %s' % gcpfile)
try:
system.run('{bin}/odm_georef -bundleFile {bundle} -imagesPath {imgs} -imagesListPath {imgs_list} '
'-bundleResizedTo {size} -inputFile {model} -outputFile {model_geo} '
'-inputPointCloudFile {pc} -outputPointCloudFile {pc_geo} {verbose} '
'-logFile {log} -georefFileOutputPath {geo_sys} -gcpFile {gcp} '
'-outputCoordFile {coords}'.format(**kwargs))
except Exception:
log.ODM_EXCEPTION('Georeferencing failed. ')
return ecto.QUIT
2016-12-14 18:36:23 +00:00
elif io.file_exists(tree.odm_georeferencing_coords):
log.ODM_INFO('Running georeferencing with generated coords file.')
2016-02-25 19:51:03 +00:00
system.run('{bin}/odm_georef -bundleFile {bundle} -inputCoordFile {coords} '
'-inputFile {model} -outputFile {model_geo} '
2016-12-11 22:16:11 +00:00
'-inputPointCloudFile {pc} -outputPointCloudFile {pc_geo} {verbose} '
2016-02-25 19:51:03 +00:00
'-logFile {log} -georefFileOutputPath {geo_sys}'.format(**kwargs))
2016-12-14 18:36:23 +00:00
else:
log.ODM_WARNING('Georeferencing failed. Make sure your '
'photos have geotags in the EXIF or you have '
'provided a GCP file. ')
geocreated = False # skip the rest of the georeferencing
if geocreated:
# update images metadata
geo_ref = types.ODM_GeoRef()
geo_ref.parse_coordinate_system(tree.odm_georeferencing_coords)
for idx, photo in enumerate(self.inputs.photos):
geo_ref.utm_to_latlon(tree.odm_georeferencing_latlon, photo, idx)
# convert ply model to LAS reference system
geo_ref.convert_to_las(tree.odm_georeferencing_model_ply_geo,
2017-04-06 13:06:09 +00:00
tree.odm_georeferencing_model_las,
2017-04-06 17:59:26 +00:00
tree.odm_georeferencing_las_json)
2016-12-14 18:36:23 +00:00
2017-04-06 13:06:09 +00:00
# If --dem, create a DEM
if args.dem:
2017-04-06 19:37:13 +00:00
demcreated = geo_ref.convert_to_dem(tree.odm_georeferencing_model_las,
tree.odm_georeferencing_dem,
tree.odm_georeferencing_dem_json,
self.params.sample_radius,
self.params.gdal_res,
self.params.gdal_radius)
2017-04-06 13:06:09 +00:00
if not demcreated:
log.ODM_WARNING('Something went wrong. Check the logs in odm_georeferencing.')
else:
log.ODM_INFO('DEM created at {0}'.format(tree.odm_georeferencing_dem))
2016-12-14 18:36:23 +00:00
# XYZ point cloud output
2017-04-06 19:37:13 +00:00
log.ODM_INFO("Creating geo-referenced CSV file (XYZ format)")
2016-12-14 18:36:23 +00:00
with open(tree.odm_georeferencing_xyz_file, "wb") as csvfile:
csvfile_writer = csv.writer(csvfile, delimiter=",")
reachedpoints = False
with open(tree.odm_georeferencing_model_ply_geo) as f:
for lineNumber, line in enumerate(f):
if reachedpoints:
tokens = line.split(" ")
csv_line = [float(tokens[0])+geo_ref.utm_east_offset,
float(tokens[1])+geo_ref.utm_north_offset,
tokens[2]]
csvfile_writer.writerow(csv_line)
if line.startswith("end_header"):
reachedpoints = True
csvfile.close()
2015-12-01 16:25:14 +00:00
else:
2016-02-25 19:51:03 +00:00
log.ODM_WARNING('Found a valid georeferenced model in: %s'
% tree.odm_georeferencing_model_ply_geo)
2015-12-10 17:17:39 +00:00
if args.time:
2016-02-29 14:45:00 +00:00
system.benchmark(start_time, tree.benchmarking, 'Georeferencing')
log.ODM_INFO('Running ODM Georeferencing Cell - Finished')
return ecto.OK if args.end_with != 'odm_georeferencing' else ecto.QUIT