Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
25 changes: 15 additions & 10 deletions patchwork/patchwork.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import warnings
from shutil import copy2
from typing import List, Tuple

Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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):
Expand All @@ -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",
)
)
Expand Down
Binary file not shown.
38 changes: 29 additions & 9 deletions test/test_patchwork.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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}

Expand All @@ -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}",
Expand All @@ -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} == {
Expand All @@ -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)
assert not np.any(output_points.classification[output_points.Origin == 0] == 11)
2 changes: 1 addition & 1 deletion version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "1.4.1"
__version__ = "1.4.2"

if __name__ == "__main__":
print(__version__)
Loading