Skip to content

Commit

Permalink
Test correct usage of the shapefile
Browse files Browse the repository at this point in the history
  • Loading branch information
leavauchier committed Apr 4, 2024
1 parent e4b3489 commit 02db674
Showing 1 changed file with 105 additions and 22 deletions.
127 changes: 105 additions & 22 deletions tests/lidar_prod/tasks/test_building_validation_preparation.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,102 @@
import os
import tempfile
from pathlib import Path
import shutil

import hydra

import numpy as np

from lidar_prod.tasks.building_validation import BuildingValidator
from lidar_prod.tasks.utils import BDUniConnectionParams
from lidar_prod.tasks.utils import BDUniConnectionParams, get_las_data_from_las

TMP_DIR = Path("tmp/lidar_prod/tasks/building_validation_preparation")


def setup_module(module):
try:
shutil.rmtree(TMP_DIR)
except FileNotFoundError:
pass
TMP_DIR.mkdir(parents=True, exist_ok=True)


def test_shapefile_overlay_in_building_module(hydra_cfg):
"""Check that that the prepare function does not add any presence data if the laz geometry does not intersect the
BDUni territoire corresponding with the configured epsg"""
# Run application on the data subset for which vector data is expected to be invalid.
laz_input_file = "tests/files/St_Barth_RGAF09_UTM20N_IGN_1988_SB_subset_100m.laz"
epsg = 5490

target_las_path = str(
TMP_DIR / "St_Barth_RGAF09_UTM20N_IGN_1988_SB_subset_100m_wrong_epsg.laz"
)
cfg = hydra_cfg.copy()
cfg.data_format.epsg = epsg

INVALID_OVERLAY_LAZ_PATH = "tests/files/invalid_overlay_data/842_6521-invalid_shapefile_area-borders.las"
bd_uni_connection_params: BDUniConnectionParams = hydra.utils.instantiate(
cfg.bd_uni_connection_params
)

bv = BuildingValidator(
shp_path=cfg.building_validation.application.shp_path,
bd_uni_connection_params=bd_uni_connection_params,
cluster=cfg.building_validation.application.cluster,
bd_uni_request=cfg.building_validation.application.bd_uni_request,
data_format=cfg.building_validation.application.data_format,
thresholds=cfg.building_validation.application.thresholds,
use_final_classification_codes=cfg.building_validation.application.use_final_classification_codes,
)

bv.prepare(laz_input_file, target_las_path, save_result=True)
data = get_las_data_from_las(target_las_path)
overlay = data[cfg.data_format.las_dimensions.uni_db_overlay]
assert np.any(overlay == 1) # assert some points are marked
assert np.any(overlay == 0) # assert not all points are marked


def test_shapefile_overlay_in_building_module_no_data(hydra_cfg):
"""Check that that the prepare function does not add any presence data if the laz geometry does not intersect the
BDUni territoire corresponding with the configured epsg"""
# Run application on the data subset for which vector data is expected to be invalid.
laz_input_file = "tests/files/St_Barth_RGAF09_UTM20N_IGN_1988_SB_subset_100m.laz"
wrong_epsg = 2154

target_las_path = str(
TMP_DIR / "St_Barth_RGAF09_UTM20N_IGN_1988_SB_subset_100m_wrong_epsg.laz"
)
cfg = hydra_cfg.copy()
cfg.data_format.epsg = wrong_epsg

bd_uni_connection_params: BDUniConnectionParams = hydra.utils.instantiate(
cfg.bd_uni_connection_params
)

bv = BuildingValidator(
shp_path=cfg.building_validation.application.shp_path,
bd_uni_connection_params=bd_uni_connection_params,
cluster=cfg.building_validation.application.cluster,
bd_uni_request=cfg.building_validation.application.bd_uni_request,
data_format=cfg.building_validation.application.data_format,
thresholds=cfg.building_validation.application.thresholds,
use_final_classification_codes=cfg.building_validation.application.use_final_classification_codes,
)

bv.prepare(laz_input_file, target_las_path, save_result=True)
data = get_las_data_from_las(target_las_path)
overlay = data[cfg.data_format.las_dimensions.uni_db_overlay]
assert np.all(
overlay == 0
) # assert not point is marked as the BDUni query should not return anything


# We try to reduce size of LAZ to isolate the problem first to make it quick to test when it is ok.


# Normal execution on subset of LAZ lasts ~ 3sec.
# If a regression occurs, the pdal execution will hang and a timeout would make it more apparent.
# However, pytest-timeout does not stop pdal for some reasons. For now this should be sufficient.
def test_shapefile_overlay_in_building_module(hydra_cfg):
def test_shapefile_overlay_in_building_module_invalid_overlay(hydra_cfg):
"""We test the application against a LAS subset for which the BDUni shapefile shows overlapping vectors.
We only need points at the borders of the area in order to request the error-generating shapefile.
Expand All @@ -23,23 +105,24 @@ def test_shapefile_overlay_in_building_module(hydra_cfg):
shapefile to remove this bug.
"""
invalid_overlay_laz_path = (
"tests/files/invalid_overlay_data/842_6521-invalid_shapefile_area-borders.las"
)
# Run application on the data subset for which vector data is expected to be invalid.
with tempfile.TemporaryDirectory() as hydra_cfg.paths.output_dir:
target_las_path = os.path.join(
hydra_cfg.paths.output_dir,
os.path.basename(INVALID_OVERLAY_LAZ_PATH),
)

bd_uni_connection_params: BDUniConnectionParams = hydra.utils.instantiate(hydra_cfg.bd_uni_connection_params)

bv = BuildingValidator(
shp_path=hydra_cfg.building_validation.application.shp_path,
bd_uni_connection_params=bd_uni_connection_params,
cluster=hydra_cfg.building_validation.application.cluster,
bd_uni_request=hydra_cfg.building_validation.application.bd_uni_request,
data_format=hydra_cfg.building_validation.application.data_format,
thresholds=hydra_cfg.building_validation.application.thresholds,
use_final_classification_codes=hydra_cfg.building_validation.application.use_final_classification_codes
)

bv.prepare(INVALID_OVERLAY_LAZ_PATH, target_las_path)
target_las_path = str(TMP_DIR / "invalid_overlay.laz")

bd_uni_connection_params: BDUniConnectionParams = hydra.utils.instantiate(
hydra_cfg.bd_uni_connection_params
)

bv = BuildingValidator(
shp_path=hydra_cfg.building_validation.application.shp_path,
bd_uni_connection_params=bd_uni_connection_params,
cluster=hydra_cfg.building_validation.application.cluster,
bd_uni_request=hydra_cfg.building_validation.application.bd_uni_request,
data_format=hydra_cfg.building_validation.application.data_format,
thresholds=hydra_cfg.building_validation.application.thresholds,
use_final_classification_codes=hydra_cfg.building_validation.application.use_final_classification_codes,
)

bv.prepare(invalid_overlay_laz_path, target_las_path)

0 comments on commit 02db674

Please sign in to comment.