I am trying to execute PDAL within a python script for multiple tasks from conversion to filtering, etc. If there is any error with the file, PDAL stops executing and throws an error. But I am unable to capture any error that PDAL spits out.
You can find two approaches in the following. Could you provide some feedback on the code and potential errors?
Approach 1: Run pdal as bash command using subprocess.run.
Here pipeline_json contains the pdal pipeline I want to execute
pdal_command = ["pdal", "pipeline", "--stdin", "-v", "8"] result = run(pdal_command, input=pipeline_json, check=True, capture_output=True, text=True ) In this, on successful case, I get pdal's log in result.stderr. But if it fails, I access the stderr from CalledProcessError error thrown by subprocess.run But it is more often than not has process exited with errorcode 3221226505 along with initial PDAL debug messages and NOT the error message from PDAL.
(PDAL Debug) Debugging... (pdal pipeline Debug) Attempting to load plugin 'C: \Users\uuser\anaconda3\envs\volume\Library\bin\libpdal_plugin_reader_e57.dll'. (pdal pipeline Debug) Loaded plugin 'C:\Users\uuser\anaconda3\envs\volume\Library\bin\libpdal_plugin_reader_e57.dll'. (pdal pipeline Debug) Initialized plugin 'C:\Users\uuser\anaconda3\envs\volume\Library\bin\libpdal_plugin_reader_e57.dll'. (pdal pipeline Debug) Executing pipeline in standard mode. (pdal pipeline readers.e57 Info) Reading : .\test_file.e57 Approach 2: using pdal python library. This time, if any error, the script just stops even if I wrap the whole code in try, except block. Which leaves me with no option to capture why pdal failed
import sys import json import pdal def convert_to_copc(input_file, output_copc): pipeline = { "pipeline": [ { "type": "readers.e57", "filename": input_file }, { "type": "writers.copc", "filename": output_copc, "forward": "all", } ] } pdal_las_json = json.dumps(pipeline) las_pipeline = pdal.Pipeline(pdal_las_json) try: result = las_pipeline.execute() log = las_pipeline.log print(log) print("done") except RuntimeError as e: print("RuntimeError", e) except Exception as e: print(f"Error processing file {input_file}: {str(e)}") return None def main(): if len(sys.argv) != 3: print("Usage: python script.py <input_las_file> <output_copc_file>") sys.exit(1) input_las = sys.argv[1] output_copc = sys.argv[2] convert_to_copc(input_las, output_copc) print("end") if __name__ == "__main__": main()
econtains some info about what the problem is. Which sounds like what you want.3221226505) generally indicates that the program segfaulted/crashed, so it won't even have the opportunity to report a friendly error message. Python itself may be crashing in your 2nd example which is why your try/except isn't working. Probably an issue with how PDAL was installed