From ad100525b506ceabc15fc10bd98b22babdf5de67 Mon Sep 17 00:00:00 2001 From: Piero Toffanin Date: Wed, 24 May 2023 12:27:58 -0400 Subject: [PATCH] Embed GeoJSON ground control points file in point cloud outputs --- opendm/entwine.py | 42 +++++++++++++++++++++++------------- opendm/point_cloud.py | 4 +++- stages/odm_georeferencing.py | 2 +- 3 files changed, 31 insertions(+), 17 deletions(-) diff --git a/opendm/entwine.py b/opendm/entwine.py index fbfddbf9..84babb9e 100644 --- a/opendm/entwine.py +++ b/opendm/entwine.py @@ -62,7 +62,7 @@ def build_untwine(input_point_cloud_files, tmpdir, output_path, max_concurrency= # Run untwine system.run('untwine --temp_dir "{tmpdir}" {files} --output_dir "{outputdir}"'.format(**kwargs)) -def build_copc(input_point_cloud_files, output_file, convert_rgb_8_to_16=False): +def build_copc(input_point_cloud_files, output_file, convert_rgb_8_to_16=False, embed_gcp=None): if len(input_point_cloud_files) == 0: logger.ODM_WARNING("Cannot build COPC, no input files") return @@ -75,13 +75,15 @@ def build_copc(input_point_cloud_files, output_file, convert_rgb_8_to_16=False): shutil.rmtree(tmpdir) cleanup = [tmpdir] - if convert_rgb_8_to_16: - tmpdir16 = io.related_file_path(base_path, postfix="-tmp16") - if os.path.exists(tmpdir16): - log.ODM_WARNING("Removing previous directory %s" % tmpdir16) - shutil.rmtree(tmpdir16) - os.makedirs(tmpdir16, exist_ok=True) - cleanup.append(tmpdir16) + do_embed_gcp = embed_gcp is not None and os.path.isfile(embed_gcp) + + if convert_rgb_8_to_16 or do_embed_gcp: + tmpdir_2 = io.related_file_path(base_path, postfix="-tmp2") + if os.path.exists(tmpdir_2): + log.ODM_WARNING("Removing previous directory %s" % tmpdir_2) + shutil.rmtree(tmpdir_2) + os.makedirs(tmpdir_2, exist_ok=True) + cleanup.append(tmpdir_2) converted = [] ok = True @@ -89,16 +91,26 @@ def build_copc(input_point_cloud_files, output_file, convert_rgb_8_to_16=False): # Convert 8bit RGB to 16bit RGB (per COPC spec) base = os.path.basename(f) filename, ext = os.path.splitext(base) - out_16 = os.path.join(tmpdir16, "%s_16%s" % (filename, ext)) + out_intermediate = os.path.join(tmpdir_2, "%s_inter%s" % (filename, ext)) try: - system.run('pdal translate -i "{input}" -o "{output}" assign ' - '--filters.assign.value="Red = Red / 255 * 65535" ' - '--filters.assign.value="Green = Green / 255 * 65535" ' - '--filters.assign.value="Blue = Blue / 255 * 65535" '.format(input=f, output=out_16)) + stages = [] + params = [] + + if convert_rgb_8_to_16: + stages.append("assign") + params.append('--filters.assign.value="Red = Red / 255 * 65535"') + params.append('--filters.assign.value="Green = Green / 255 * 65535"') + params.append('--filters.assign.value="Blue = Blue / 255 * 65535"') - converted.append(out_16) + # if do_embed_gcp: + # params.append('--writers.las.vlrs="{\\\"filename\\\": \\\"%s\\\", \\\"user_id\\\": \\\"ODM\\\", \\\"record_id\\\": \\\"1\\\", \\\"description\\\": \\\"Ground Control Points (GML)\\\"}"' % gcp_geojson_export_file.replace(os.sep, "/") + # ]") + + system.run('pdal translate -i "{input}" -o "{output}" {stages} {params}'.format(input=f, output=out_intermediate, stages=" ".join(stages), params=" ".join(params))) + + converted.append(out_intermediate) except Exception as e: - log.ODM_WARNING("Cannot convert point cloud to 16bit RGB, COPC is not going to follow the official spec: %s" % str(e)) + log.ODM_WARNING("Cannot execute pdal translate: %s" % str(e)) ok = False break if ok: diff --git a/opendm/point_cloud.py b/opendm/point_cloud.py index abe7ee91..f47aaf47 100644 --- a/opendm/point_cloud.py +++ b/opendm/point_cloud.py @@ -311,4 +311,6 @@ def post_point_cloud_steps(args, tree, rerun=False): log.ODM_INFO("Creating Cloud Optimized Point Cloud (COPC)") copc_output = io.related_file_path(tree.odm_georeferencing_model_laz, postfix=".copc") - entwine.build_copc([tree.odm_georeferencing_model_laz], copc_output, convert_rgb_8_to_16=True) \ No newline at end of file + gcp_geojson_export_file = tree.path("odm_georeferencing", "ground_control_points.geojson") + + entwine.build_copc([tree.odm_georeferencing_model_laz], copc_output, convert_rgb_8_to_16=True, embed_gcp=gcp_geojson_export_file) \ No newline at end of file diff --git a/stages/odm_georeferencing.py b/stages/odm_georeferencing.py index 5115ae74..787d4d6c 100644 --- a/stages/odm_georeferencing.py +++ b/stages/odm_georeferencing.py @@ -134,7 +134,7 @@ class ODMGeoreferencingStage(types.ODM_Stage): if reconstruction.has_gcp() and io.file_exists(gcp_gml_export_file): log.ODM_INFO("Embedding GCP info in point cloud") params += [ - '--writers.las.vlrs="{\\\"filename\\\": \\\"%s\\\", \\\"user_id\\\": \\\"ODM\\\", \\\"record_id\\\": \\\"1\\\", \\\"description\\\": \\\"Ground Control Points (GML)\\\"}"' % gcp_gml_export_file.replace(os.sep, "/") + '--writers.las.vlrs="{\\\"filename\\\": \\\"%s\\\", \\\"user_id\\\": \\\"ODM\\\", \\\"record_id\\\": 1, \\\"description\\\": \\\"Ground Control Points (GML)\\\"}"' % gcp_geojson_export_file.replace(os.sep, "/") ] system.run(cmd + ' ' + ' '.join(stages) + ' ' + ' '.join(params))