From fc13840d8f7b511ff7de91486835ed1be0778cab Mon Sep 17 00:00:00 2001 From: leavauchier <120112647+leavauchier@users.noreply.github.com> Date: Thu, 14 Nov 2024 10:56:36 +0100 Subject: [PATCH] Add examples to use python scripts in the docker image (#27) --- README.md | 12 +++++ RELEASE_NOTES.md | 2 + examples/demo_script.py | 33 +++++++++++++ examples/run_custom_script_in_docker_image.sh | 49 +++++++++++++++++++ 4 files changed, 96 insertions(+) create mode 100644 examples/demo_script.py create mode 100755 examples/run_custom_script_in_docker_image.sh diff --git a/README.md b/README.md index 5bf9a35..8ca0b45 100755 --- a/README.md +++ b/README.md @@ -11,6 +11,9 @@ to be able to run the compilation in this environment. run ci/build.sh +Set the `PDAL_DRIVER_PATH` environment variable to point to `${THIS REPO}/install/lib` +in order for pdal to find the plugins. + ### Windows one day, maybe... @@ -27,6 +30,7 @@ The code is structured as: │ │ ├── CMakeLists.txt ├── doc │ ├── pluginFilter.md +├── examples # examples of usage of the code or the docker image ├── ci ├── macro # Python module with ready-to-use filters combinations │   ├── __init__.py @@ -129,3 +133,11 @@ docker run \ python /scripts/my_script.py --input /data/my_data_file.las -o /output/my_output.las ``` +Another example can be found in the [./example](./examples/) folder: + +Run the following command to run the [demo_script](./examples/demo_script.py) python script +which copies an input las file into the output folder as a las1.4 file : + +```bash +./examples/run_custom_script_in_docker_image.sh -i ./test/data/mnx/input/bat.laz -o ./tmp/demo -s ./examples/demo_script.py +``` diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 54b0065..a71f9b6 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -1,3 +1,5 @@ +- Add example to run a local python script in the pdal_ign_plugin docker image + ### 0.3.1 - improve code readability in the radius_assign filter (Z limits part). diff --git a/examples/demo_script.py b/examples/demo_script.py new file mode 100644 index 0000000..efd9450 --- /dev/null +++ b/examples/demo_script.py @@ -0,0 +1,33 @@ +"""Example python script used to demonstrate the usage of local python scripts inside the pdal_ign_plugin +docker image +""" + +import argparse + +import pdal + +from pdal_ign_macro import version as pim_version + + +def parse_args(): + parser = argparse.ArgumentParser() + parser.add_argument( + "-i", "--input_file", type=str, help="Input las/laz file on which to run the script" + ) + parser.add_argument("-o", "--output_file", type=str, help="Out las/laz file of the script") + return parser.parse_args() + + +def main(input_file, output_file): + print("Pdal version is:", pdal.__version__) + print("pdal_ign_macro version is:", pim_version.__version__) + print("Copy input file to output file as LAS1.4") + pipeline = pdal.Pipeline() + pipeline |= pdal.Reader.las(filename=input_file) + pipeline |= pdal.Writer.las(filename=output_file, major_version=1, minor_version=4) + pipeline.execute() + + +if __name__ == "__main__": + args = parse_args() + main(args.input_file, args.output_file) diff --git a/examples/run_custom_script_in_docker_image.sh b/examples/run_custom_script_in_docker_image.sh new file mode 100755 index 0000000..027c422 --- /dev/null +++ b/examples/run_custom_script_in_docker_image.sh @@ -0,0 +1,49 @@ +#!/bin/bash +# Bash script to run a local python script in the pdal_ign_plugin docker image + +while [[ $# -gt 0 ]]; do + case $1 in + -i|--input_file) + input_dir=$(realpath $(dirname "$2")) + input_filename=$(basename "$2") + shift # past argument + shift # past value + ;; + -o|--output_dir) + output_dir=$(realpath "$2") + shift # past argument + shift # past value + ;; + -s|--script) + script_dir=$(realpath $(dirname "$2")) + script_filename=$(basename "$2") + shift # past argument + shift # past value + ;; + *) + echo "Unknown option $1" + exit 1 + ;; + esac +done + +echo "Run script:" +echo "* dir=${script_dir}" +echo "* name=${script_filename}" +echo "on file:" +echo "* dir=${input_dir}" +echo "* name=${input_filename}" +echo "save output to:" +echo "* dir=${output_dir}" +echo "* name=${input_filename}" +echo "--------" + + +docker run --rm --userns=host \ +-v ${input_dir}:/input \ +-v ${output_dir}:/output \ +-v ${script_dir}:/script \ +ghcr.io/ignf/pdal-ign-plugin:latest \ +python /script/${script_filename} \ + -i /input/${input_filename} \ + -o /output/${input_filename} \ No newline at end of file