From 6f3442c30481cd9c3f3ac76080c3dc0469f8f57d Mon Sep 17 00:00:00 2001 From: Lea Vauchier Date: Thu, 26 Oct 2023 17:07:34 +0200 Subject: [PATCH] Generalize unlock to count_occurences and replace_attribute --- CHANGELOG.md | 1 + .../count_occurences/count_occurences_for_attribute.py | 3 +++ pdaltools/replace_attribute_in_las.py | 8 +++++--- pdaltools/standardize_format.py | 5 ++--- 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2294400..4df04c7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,5 @@ # dev +- count_occurences / replace_value: add copy_and_hack decorator to run on tscan output files # 1.3.1 - fix color: ensure that tmp orthoimages are deleted after use by using the namedTemporaryFile properly. diff --git a/pdaltools/count_occurences/count_occurences_for_attribute.py b/pdaltools/count_occurences/count_occurences_for_attribute.py index 5ddedae..c555d39 100644 --- a/pdaltools/count_occurences/count_occurences_for_attribute.py +++ b/pdaltools/count_occurences/count_occurences_for_attribute.py @@ -10,6 +10,8 @@ from tqdm import tqdm from typing import List +from pdaltools.unlock_file import copy_and_hack_decorator + def parse_args(): parser = argparse.ArgumentParser("Count points with each value of an attribute.") @@ -25,6 +27,7 @@ def parse_args(): return parser.parse_args() +@copy_and_hack_decorator def compute_count_one_file(filepath: str, attribute: str = "Classification") -> Counter: pipeline = pdal.Reader.las(filepath) pipeline |= pdal.Filter.stats(dimensions=attribute, count=attribute) diff --git a/pdaltools/replace_attribute_in_las.py b/pdaltools/replace_attribute_in_las.py index 4d791c5..b59a161 100644 --- a/pdaltools/replace_attribute_in_las.py +++ b/pdaltools/replace_attribute_in_las.py @@ -10,6 +10,8 @@ import tempfile from typing import List, Dict +from pdaltools.unlock_file import copy_and_hack_decorator + def parse_args(): parser = argparse.ArgumentParser("Replace values of a given attribute in a las/laz file.") @@ -65,6 +67,7 @@ def dict_to_pdal_assign_list(d: Dict, output_attribute: str = "Classification", return assignment_list +@copy_and_hack_decorator def replace_values( input_file: str, output_file: str, @@ -109,9 +112,8 @@ def replace_values_clean( attribute: str = "Classification", writer_parameters: Dict = {}, ): - _, extension = os.path.splitext(output_file) - with tempfile.NamedTemporaryFile(suffix=extension) as tmp: - tmp.close() + filename = os.path.basename(output_file) + with tempfile.NamedTemporaryFile(suffix=filename) as tmp: replace_values(input_file, tmp.name, replacement_map, attribute, writer_parameters) exec_las2las(tmp.name, output_file) diff --git a/pdaltools/standardize_format.py b/pdaltools/standardize_format.py index b0d45f9..f638c34 100644 --- a/pdaltools/standardize_format.py +++ b/pdaltools/standardize_format.py @@ -76,9 +76,8 @@ def exec_las2las(input_file: str, output_file: str): @copy_and_hack_decorator def standardize(input_file: str, output_file: str, params_from_parser: Dict) -> None: - _, extension = os.path.splitext(output_file) - with tempfile.NamedTemporaryFile(suffix=extension) as tmp: - tmp.close() + filename = os.path.basename(output_file) + with tempfile.NamedTemporaryFile(suffix=filename) as tmp: rewrite_with_pdal(input_file, tmp.name, params_from_parser) exec_las2las(tmp.name, output_file)