Skip to content

Commit d874073

Browse files
authored
Merge pull request #23 from IGNF/fix/duplicatd_new_column
Overwrite "NEW_COLUMN" when it already exists in recipient file
2 parents 1077c81 + 4cdb43a commit d874073

5 files changed

Lines changed: 48 additions & 20 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# CHANGELOG
22

3+
## 1.4.2
4+
- fix: écrase la colonne "NEW_COLUMN" si elle existe dans le fichier receveur
5+
36
## 1.4.1
47
- fix lorsque les las donneurs ne sont pas de même version (1.2 et 1.4): on ne garde que les attributs communs
58

patchwork/patchwork.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import warnings
23
from shutil import copy2
34
from typing import List, Tuple
45

@@ -136,14 +137,16 @@ def get_complementary_points(
136137
)
137138

138139
dfs_donor_points = []
139-
140+
140141
if len(df_donor_info.index):
141142
donor_common_columns = get_common_donor_columns(df_donor_info)
142143
for index, row in df_donor_info.iterrows():
143144
with laspy.open(row["full_path"]) as donor_file:
144145
raw_donor_points = donor_file.read().points
145146
points_loc_gdf = gpd.GeoDataFrame(
146-
geometry=gpd.points_from_xy(raw_donor_points.x, raw_donor_points.y, raw_donor_points.z, crs=config.CRS)
147+
geometry=gpd.points_from_xy(
148+
raw_donor_points.x, raw_donor_points.y, raw_donor_points.z, crs=config.CRS
149+
)
147150
)
148151
footprint_gdf = gpd.GeoDataFrame(geometry=[row["geometry"]], crs=config.CRS)
149152
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]:
190193

191194
def test_field_exists(file_path: str, column: str) -> bool:
192195
output_file = laspy.read(file_path)
193-
return column in get_field_from_header(output_file)
196+
return column.lower() in get_field_from_header(output_file)
194197

195198

196199
def append_points(config: DictConfig, extra_points: pd.DataFrame):
@@ -217,19 +220,21 @@ def append_points(config: DictConfig, extra_points: pd.DataFrame):
217220

218221
copy2(recipient_filepath, output_filepath)
219222

220-
# if we want a new column, we start by adding its name
223+
# if we want a new column, we start by adding its name (or remove the existing column)
224+
221225
if config.NEW_COLUMN:
226+
output_las = laspy.read(output_filepath)
227+
222228
if test_field_exists(recipient_filepath, config.NEW_COLUMN):
223-
raise ValueError(
224-
f"{config.NEW_COLUMN} already exists as \
225-
column name in {recipient_filepath}"
229+
warnings.warn(
230+
f"{config.NEW_COLUMN} already exists as a column name in {recipient_filepath}. Overwriting it."
226231
)
227-
new_column_type = get_type(config.NEW_COLUMN_SIZE)
228-
output_las = laspy.read(output_filepath)
232+
output_las.remove_extra_dim(config.NEW_COLUMN)
233+
229234
output_las.add_extra_dim(
230235
laspy.ExtraBytesParams(
231236
name=config.NEW_COLUMN,
232-
type=new_column_type,
237+
type=get_type(config.NEW_COLUMN_SIZE),
233238
description="Point origin: 0=initial las",
234239
)
235240
)
Binary file not shown.

test/test_patchwork.py

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
import patchwork.constants as c
1212
from patchwork.patchwork import (
1313
append_points,
14+
get_common_las_columns,
1415
get_complementary_points,
1516
get_field_from_header,
16-
get_common_las_columns,
1717
get_selected_classes_points,
1818
get_type,
1919
patchwork,
@@ -434,6 +434,11 @@ def test_patchwork_default(tmp_path_factory, recipient_path, expected_nb_added_p
434434
True,
435435
0,
436436
), # No donor
437+
(
438+
"test/data/lidar_HD_decimated_with_origin_value/Semis_2022_0673_6362_LA93_IGN69_with_origin.laz",
439+
True,
440+
128675,
441+
), # One donor. Origin dimension already in input file
437442
],
438443
)
439444
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
563568
assert np.all(output_points.classification[output_points.Origin == 1] == 11)
564569
assert not np.any(output_points.classification[output_points.Origin == 0] == 11)
565570

571+
566572
@pytest.mark.parametrize(
567573
"las_liste",
568574
[
569-
(["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"]),
570-
(['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']),
575+
(
576+
[
577+
"test/data/aveyron_lidarBD/data/NUALID_1-0_IAVEY_PTS_0673_6363_LAMB93_IGN69_20170519.laz",
578+
"test/data/aveyron_lidarBD/data/NUALID_1-0_IAVEY_PTS_0673_6364_LAMB93_IGN69_20170519.laz",
579+
]
580+
),
581+
(
582+
[
583+
"test/data/grand_geneve/grand_geneve_BD/data"
584+
+ "/NUALID_1-0_DS19RFAN_PTS_0963_6543_LAMB93_IGN69_20191002.laz",
585+
"test/data/grand_geneve/grand_geneve_BD/data2/0963_6543.laz",
586+
]
587+
),
571588
],
572589
)
573590
def test_get_common_las_columns(las_liste):
574591
common_columns = get_common_las_columns(las_liste)
575-
assert all(col in common_columns for col in ["x", "y", "z", "classification", "gps_time", "intensity", "return_number", "number_of_returns"])
592+
assert all(
593+
col in common_columns
594+
for col in ["x", "y", "z", "classification", "gps_time", "intensity", "return_number", "number_of_returns"]
595+
)
576596

577597

578598
def test_patchwork_with_different_las_format(tmp_path_factory):
579599

580-
recipient_path= "test/data/grand_geneve/lidar_HD_decimate/Semis_2021_0963_6543_LA93_IGN69_decimate.laz"
600+
recipient_path = "test/data/grand_geneve/lidar_HD_decimate/Semis_2021_0963_6543_LA93_IGN69_decimate.laz"
581601
input_shp_path = "test/data/grand_geneve/geometry_GrandGeneve/zones.geojson"
582602
tmp_file_dir = tmp_path_factory.mktemp("data")
583603
tmp_output_las_name = "result_patchwork_different_las.laz"
584-
604+
585605
tmp_output_indices_map_name = "result_patchwork_indices.tif"
586606
donor_class_translation = {2: 11, 9: 11}
587607

@@ -594,7 +614,7 @@ def test_patchwork_with_different_las_format(tmp_path_factory):
594614
f"filepath.SHP_DIRECTORY={os.path.dirname(input_shp_path)}",
595615
f"filepath.SHP_NAME={os.path.basename(input_shp_path)}",
596616
f"filepath.OUTPUT_DIR={tmp_file_dir}",
597-
f"filepath.DONOR_SUBDIRECTORY=''",
617+
"filepath.DONOR_SUBDIRECTORY=''",
598618
f"filepath.OUTPUT_NAME={tmp_output_las_name}",
599619
f"filepath.OUTPUT_INDICES_MAP_DIR={tmp_file_dir}",
600620
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):
614634

615635
with laspy.open(recipient_path) as recipient_file:
616636
recipient_points = recipient_file.read().points
617-
637+
618638
with laspy.open(output_path) as las_file:
619639
output_points = las_file.read().points
620640
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):
625645
assert np.sum(output_points.Origin == 1) == expected_nb_added_points
626646

627647
assert np.all(output_points.classification[output_points.Origin == 1] == 11)
628-
assert not np.any(output_points.classification[output_points.Origin == 0] == 11)
648+
assert not np.any(output_points.classification[output_points.Origin == 0] == 11)

version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "1.4.1"
1+
__version__ = "1.4.2"
22

33
if __name__ == "__main__":
44
print(__version__)

0 commit comments

Comments
 (0)