diff --git a/CHANGELOG.md b/CHANGELOG.md index 8320b32..c2ade06 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # CHANGELOG +## 1.4.2 +- fix: écrase la colonne "NEW_COLUMN" si elle existe dans le fichier receveur + ## 1.4.1 - fix lorsque les las donneurs ne sont pas de même version (1.2 et 1.4): on ne garde que les attributs communs diff --git a/patchwork/patchwork.py b/patchwork/patchwork.py index cfde82b..69cb82c 100644 --- a/patchwork/patchwork.py +++ b/patchwork/patchwork.py @@ -1,4 +1,5 @@ import os +import warnings from shutil import copy2 from typing import List, Tuple @@ -136,14 +137,16 @@ def get_complementary_points( ) dfs_donor_points = [] - + if len(df_donor_info.index): donor_common_columns = get_common_donor_columns(df_donor_info) for index, row in df_donor_info.iterrows(): with laspy.open(row["full_path"]) as donor_file: raw_donor_points = donor_file.read().points points_loc_gdf = gpd.GeoDataFrame( - geometry=gpd.points_from_xy(raw_donor_points.x, raw_donor_points.y, raw_donor_points.z, crs=config.CRS) + geometry=gpd.points_from_xy( + raw_donor_points.x, raw_donor_points.y, raw_donor_points.z, crs=config.CRS + ) ) footprint_gdf = gpd.GeoDataFrame(geometry=[row["geometry"]], crs=config.CRS) points_in_footprint_gdf = points_loc_gdf.sjoin(footprint_gdf, how="inner", predicate="intersects") @@ -190,7 +193,7 @@ def get_field_from_header(las_file: LasReader) -> List[str]: def test_field_exists(file_path: str, column: str) -> bool: output_file = laspy.read(file_path) - return column in get_field_from_header(output_file) + return column.lower() in get_field_from_header(output_file) def append_points(config: DictConfig, extra_points: pd.DataFrame): @@ -217,19 +220,21 @@ def append_points(config: DictConfig, extra_points: pd.DataFrame): copy2(recipient_filepath, output_filepath) - # if we want a new column, we start by adding its name + # if we want a new column, we start by adding its name (or remove the existing column) + if config.NEW_COLUMN: + output_las = laspy.read(output_filepath) + if test_field_exists(recipient_filepath, config.NEW_COLUMN): - raise ValueError( - f"{config.NEW_COLUMN} already exists as \ - column name in {recipient_filepath}" + warnings.warn( + f"{config.NEW_COLUMN} already exists as a column name in {recipient_filepath}. Overwriting it." ) - new_column_type = get_type(config.NEW_COLUMN_SIZE) - output_las = laspy.read(output_filepath) + output_las.remove_extra_dim(config.NEW_COLUMN) + output_las.add_extra_dim( laspy.ExtraBytesParams( name=config.NEW_COLUMN, - type=new_column_type, + type=get_type(config.NEW_COLUMN_SIZE), description="Point origin: 0=initial las", ) ) diff --git a/test/data/lidar_HD_decimated_with_origin_value/Semis_2022_0673_6362_LA93_IGN69_with_origin.laz b/test/data/lidar_HD_decimated_with_origin_value/Semis_2022_0673_6362_LA93_IGN69_with_origin.laz new file mode 100644 index 0000000..915c4a8 Binary files /dev/null and b/test/data/lidar_HD_decimated_with_origin_value/Semis_2022_0673_6362_LA93_IGN69_with_origin.laz differ diff --git a/test/test_patchwork.py b/test/test_patchwork.py index 6c61b67..eed2153 100644 --- a/test/test_patchwork.py +++ b/test/test_patchwork.py @@ -11,9 +11,9 @@ import patchwork.constants as c from patchwork.patchwork import ( append_points, + get_common_las_columns, get_complementary_points, get_field_from_header, - get_common_las_columns, get_selected_classes_points, get_type, patchwork, @@ -434,6 +434,11 @@ def test_patchwork_default(tmp_path_factory, recipient_path, expected_nb_added_p True, 0, ), # No donor + ( + "test/data/lidar_HD_decimated_with_origin_value/Semis_2022_0673_6362_LA93_IGN69_with_origin.laz", + True, + 128675, + ), # One donor. Origin dimension already in input file ], ) def test_patchwork_with_origin(tmp_path_factory, recipient_path, donor_use_synthetic_points, expected_nb_added_points): @@ -563,25 +568,40 @@ def test_patchwork_with_mount_points(tmp_path_factory, input_shp_path, recipient assert np.all(output_points.classification[output_points.Origin == 1] == 11) assert not np.any(output_points.classification[output_points.Origin == 0] == 11) + @pytest.mark.parametrize( "las_liste", [ - (["test/data/aveyron_lidarBD/data/NUALID_1-0_IAVEY_PTS_0673_6363_LAMB93_IGN69_20170519.laz", "test/data/aveyron_lidarBD/data/NUALID_1-0_IAVEY_PTS_0673_6364_LAMB93_IGN69_20170519.laz"]), - (['test/data/grand_geneve/grand_geneve_BD/data/NUALID_1-0_DS19RFAN_PTS_0963_6543_LAMB93_IGN69_20191002.laz', 'test/data/grand_geneve/grand_geneve_BD/data2/0963_6543.laz']), + ( + [ + "test/data/aveyron_lidarBD/data/NUALID_1-0_IAVEY_PTS_0673_6363_LAMB93_IGN69_20170519.laz", + "test/data/aveyron_lidarBD/data/NUALID_1-0_IAVEY_PTS_0673_6364_LAMB93_IGN69_20170519.laz", + ] + ), + ( + [ + "test/data/grand_geneve/grand_geneve_BD/data" + + "/NUALID_1-0_DS19RFAN_PTS_0963_6543_LAMB93_IGN69_20191002.laz", + "test/data/grand_geneve/grand_geneve_BD/data2/0963_6543.laz", + ] + ), ], ) def test_get_common_las_columns(las_liste): common_columns = get_common_las_columns(las_liste) - assert all(col in common_columns for col in ["x", "y", "z", "classification", "gps_time", "intensity", "return_number", "number_of_returns"]) + assert all( + col in common_columns + for col in ["x", "y", "z", "classification", "gps_time", "intensity", "return_number", "number_of_returns"] + ) def test_patchwork_with_different_las_format(tmp_path_factory): - recipient_path= "test/data/grand_geneve/lidar_HD_decimate/Semis_2021_0963_6543_LA93_IGN69_decimate.laz" + recipient_path = "test/data/grand_geneve/lidar_HD_decimate/Semis_2021_0963_6543_LA93_IGN69_decimate.laz" input_shp_path = "test/data/grand_geneve/geometry_GrandGeneve/zones.geojson" tmp_file_dir = tmp_path_factory.mktemp("data") tmp_output_las_name = "result_patchwork_different_las.laz" - + tmp_output_indices_map_name = "result_patchwork_indices.tif" donor_class_translation = {2: 11, 9: 11} @@ -594,7 +614,7 @@ def test_patchwork_with_different_las_format(tmp_path_factory): f"filepath.SHP_DIRECTORY={os.path.dirname(input_shp_path)}", f"filepath.SHP_NAME={os.path.basename(input_shp_path)}", f"filepath.OUTPUT_DIR={tmp_file_dir}", - f"filepath.DONOR_SUBDIRECTORY=''", + "filepath.DONOR_SUBDIRECTORY=''", f"filepath.OUTPUT_NAME={tmp_output_las_name}", f"filepath.OUTPUT_INDICES_MAP_DIR={tmp_file_dir}", f"filepath.OUTPUT_INDICES_MAP_NAME={tmp_output_indices_map_name}", @@ -614,7 +634,7 @@ def test_patchwork_with_different_las_format(tmp_path_factory): with laspy.open(recipient_path) as recipient_file: recipient_points = recipient_file.read().points - + with laspy.open(output_path) as las_file: output_points = las_file.read().points assert {n for n in las_file.header.point_format.dimension_names} == { @@ -625,4 +645,4 @@ def test_patchwork_with_different_las_format(tmp_path_factory): assert np.sum(output_points.Origin == 1) == expected_nb_added_points assert np.all(output_points.classification[output_points.Origin == 1] == 11) - assert not np.any(output_points.classification[output_points.Origin == 0] == 11) \ No newline at end of file + assert not np.any(output_points.classification[output_points.Origin == 0] == 11) diff --git a/version.py b/version.py index 38a30c8..f78ab09 100644 --- a/version.py +++ b/version.py @@ -1,4 +1,4 @@ -__version__ = "1.4.1" +__version__ = "1.4.2" if __name__ == "__main__": print(__version__)