diff --git a/.gitignore b/.gitignore index 8e94d97..7a1eb5c 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,6 @@ __pycache__ *.egg-info *.idea + +# test output +tmp diff --git a/README.md b/README.md index 8b1e758..5bf9a35 100755 --- a/README.md +++ b/README.md @@ -31,9 +31,8 @@ The code is structured as: ├── macro # Python module with ready-to-use filters combinations │   ├── __init__.py │   ├── macro.py -│   └── version.py -├── scripts -│   ├── *.py # Example scripts to use the plugin filters + the filters combinations contained in `macro` +│   ├── version.py +│   └── *.py # Example scripts to use the plugin filters + the filters combinations contained in `macro` ├── test ├── CMakeLists.txt ├── environment*.yml diff --git a/environment.yml b/environment.yml index 64da390..a15c196 100755 --- a/environment.yml +++ b/environment.yml @@ -18,4 +18,4 @@ dependencies: # --------- pip & pip libraries --------- # - pip - pip: - - ign-pdal-tools + - ign-pdal-tools==1.7.1 diff --git a/environment_docker.yml b/environment_docker.yml index 6d00e85..4dab5e9 100755 --- a/environment_docker.yml +++ b/environment_docker.yml @@ -11,5 +11,5 @@ dependencies: # --------- pip & pip libraries --------- # - pip - pip: - - ign-pdal-tools - + - ign-pdal-tools==1.7.1 + diff --git a/pdal_ign_macro/mark_points_to_use_for_digital_models_with_new_dimension.py b/pdal_ign_macro/mark_points_to_use_for_digital_models_with_new_dimension.py index e455489..20e7d86 100755 --- a/pdal_ign_macro/mark_points_to_use_for_digital_models_with_new_dimension.py +++ b/pdal_ign_macro/mark_points_to_use_for_digital_models_with_new_dimension.py @@ -1,6 +1,9 @@ import argparse +import shutil +import tempfile import pdal +from pdaltools.las_remove_dimensions import remove_dimensions_from_las from pdal_ign_macro import macro @@ -39,23 +42,22 @@ def parse_args(): parser.add_argument( "--output_dtm", "-t", type=str, required=False, default="", help="Output dtm tiff file" ) + parser.add_argument( + "--keep_temporary_dims", + "-k", + action="store_true", + help="If set, do not delete temporary dimensions", + ) return parser.parse_args() -def mark_points_to_use_for_digital_models_with_new_dimension( - input_las, output_las, dsm_dimension, dtm_dimension, output_dsm, output_dtm -): +def define_marking_pipeline(input_las, output_las, dsm_dimension, dtm_dimension): pipeline = pdal.Pipeline() | pdal.Reader.las(input_las) # 0 - ajout de dimensions temporaires et de sortie - added_dimensions = [ - dtm_dimension, - dsm_dimension, - "PT_VEG_DSM", - "PT_ON_BRIDGE", - "PT_ON_BUILDING", - "PT_ON_VEGET", - ] + temporary_dimensions = ["PT_VEG_DSM", "PT_ON_BRIDGE", "PT_ON_BUILDING", "PT_ON_VEGET"] + added_dimensions = [dtm_dimension, dsm_dimension] + temporary_dimensions + pipeline |= pdal.Filter.ferry(dimensions="=>" + ", =>".join(added_dimensions)) # 1 - recherche des points max de végétation (4,5) sur une grille régulière, avec prise en @@ -220,29 +222,53 @@ def mark_points_to_use_for_digital_models_with_new_dimension( ) # ERREUR EN 4!###############################################################################################! # 5 - export du nuage et des DSM - # TODO: n'ajouter que les dimensions de sortie utiles ! pipeline |= pdal.Writer.las(extra_dims="all", forward="all", filename=output_las) - if output_dtm: - pipeline |= pdal.Writer.gdal( - gdaldriver="GTiff", - output_type="max", - resolution=0.5, - filename=output_dtm, - where=f"{dtm_dimension}==1", - ) + return pipeline, temporary_dimensions - if output_dsm: - pipeline |= pdal.Writer.gdal( - gdaldriver="GTiff", - output_type="max", - resolution=0.5, - filename=output_dsm, - where=f"{dsm_dimension}==1", + +def mark_points_to_use_for_digital_models_with_new_dimension( + input_las, + output_las, + dsm_dimension, + dtm_dimension, + output_dsm, + output_dtm, + keep_temporary_dimensions=False, +): + with tempfile.NamedTemporaryFile(suffix="_with_buffer.las", dir=".") as tmp_las: + pipeline, temporary_dimensions = define_marking_pipeline( + input_las, + tmp_las.name, + dsm_dimension, + dtm_dimension, ) - pipeline.execute() + if output_dtm: + pipeline |= pdal.Writer.gdal( + gdaldriver="GTiff", + output_type="max", + resolution=0.5, + filename=output_dtm, + where=f"{dtm_dimension}==1", + ) + + if output_dsm: + pipeline |= pdal.Writer.gdal( + gdaldriver="GTiff", + output_type="max", + resolution=0.5, + filename=output_dsm, + where=f"{dsm_dimension}==1", + ) + + pipeline.execute() + + if keep_temporary_dimensions: + shutil.copy(tmp_las.name, output_las) + else: + remove_dimensions_from_las(tmp_las.name, temporary_dimensions, output_las) if __name__ == "__main__": diff --git a/test/pdal_ign_macro/test_mark_points_to_use_for_digital_models_with_new_dimension.py b/test/pdal_ign_macro/test_mark_points_to_use_for_digital_models_with_new_dimension.py index d66a811..a98b647 100644 --- a/test/pdal_ign_macro/test_mark_points_to_use_for_digital_models_with_new_dimension.py +++ b/test/pdal_ign_macro/test_mark_points_to_use_for_digital_models_with_new_dimension.py @@ -8,7 +8,7 @@ ) -def test_main(): +def test_mark_points_to_use_for_digital_models_with_new_dimension(): ini_las = "test/data/4_6.las" dsm_dimension = "dsm_marker" dtm_dimension = "dtm_marker" @@ -18,8 +18,16 @@ def test_main(): ) pipeline = pdal.Pipeline() pipeline |= pdal.Reader.las(las_output.name) - assert dsm_dimension in pipeline.quickinfo["readers.las"]["dimensions"].split(", ") - assert dtm_dimension in pipeline.quickinfo["readers.las"]["dimensions"].split(", ") + output_dimensions = pipeline.quickinfo["readers.las"]["dimensions"].split(", ") + assert dsm_dimension in output_dimensions + assert dtm_dimension in output_dimensions + + assert all( + [ + dim not in output_dimensions + for dim in ["PT_VEG_DSM", "PT_ON_BRIDGE", "PT_ON_BUILDING", "PT_ON_VEGET"] + ] + ) pipeline.execute() arr = pipeline.arrays[0]