Skip to content

Commit

Permalink
Merge branch '1014-erreur-pneo' into 'master'
Browse files Browse the repository at this point in the history
fix: robustify angles infos

Closes #1014

See merge request 3d/cars-park/cars!827
  • Loading branch information
dyoussef committed Mar 4, 2025
2 parents b62693b + 03c9f52 commit 6a0c3e5
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 25 deletions.
68 changes: 45 additions & 23 deletions cars/applications/grid_generation/epipolar_grid_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,32 +174,54 @@ def run(
geomodel2 = image_right[sens_cst.INPUT_GEO_MODEL]

# Get satellites angles from ground: Azimuth to north, Elevation angle
(
left_az,
left_elev_angle,
right_az,
right_elev_angle,
convergence_angle,
) = projection.get_ground_angles(
sensor1, sensor2, geomodel1, geomodel2, geometry_plugin
)
try:
(
left_az,
left_elev_angle,
right_az,
right_elev_angle,
convergence_angle,
) = projection.get_ground_angles(
sensor1, sensor2, geomodel1, geomodel2, geometry_plugin
)

logging.info(
"Left satellite acquisition angles: "
"Azimuth angle: {:.1f} degrees, "
"Elevation angle: {:.1f} degrees".format(left_az, left_elev_angle)
)
logging.info(
"Left satellite acquisition angles: "
"Azimuth angle: {:.1f} degrees, "
"Elevation angle: {:.1f} degrees".format(
left_az, left_elev_angle
)
)

logging.info(
"Right satellite acquisition angles: "
"Azimuth angle: {:.1f} degrees, "
"Elevation angle: {:.1f} degrees".format(right_az, right_elev_angle)
)
logging.info(
"Right satellite acquisition angles: "
"Azimuth angle: {:.1f} degrees, "
"Elevation angle: {:.1f} degrees".format(
right_az, right_elev_angle
)
)

logging.info(
"Stereo satellite convergence angle from ground: "
"{:.1f} degrees".format(convergence_angle)
)
logging.info(
"Stereo satellite convergence angle from ground: "
"{:.1f} degrees".format(convergence_angle)
)
except Exception as exc:
logging.error(
"Error in Angles information retrieval: {}".format(exc)
)
(
left_az,
left_elev_angle,
right_az,
right_elev_angle,
convergence_angle,
) = (
None,
None,
None,
None,
None,
)

# Generate rectification grids
(
Expand Down
6 changes: 4 additions & 2 deletions cars/core/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ def create_im_dataset(
# Reorder dimensions in color dataset in order that the first dimension
# is band.
if band_coords == cst.BAND_IM:
if np.any(descriptions) is None:
if descriptions is None or None in descriptions:
if nb_bands > 4:
raise RuntimeError("Not implemented case")
default_band = ["R", "G", "B", "N"]
descriptions = default_band[:nb_bands]

Expand All @@ -96,7 +98,7 @@ def create_im_dataset(
},
)
else:
if np.any(descriptions) is None:
if descriptions is None or None in descriptions:
descriptions = None
dataset = xr.Dataset(
{cst.EPI_IMAGE: ([cst.ROW, cst.COL], img[0, ...])},
Expand Down
18 changes: 18 additions & 0 deletions cars/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
# Standard imports
import errno
import os
import re
import shutil
from typing import Tuple

Expand Down Expand Up @@ -72,6 +73,21 @@ def make_relative_path_absolute(path, directory):
return out


def safe_cast_float(data):
"""
Safe cast to float data
:param data: string to get float in
:return float from data
"""
if isinstance(data, str):
match = re.search(r"[-+]?\d*\.\d+|\d+", data)
if match:
return float(match.group())
return None


def get_elevation_range_from_metadata(
img: str, default_min: float = 0, default_max: float = 300
) -> Tuple[float, float]:
Expand All @@ -92,6 +108,8 @@ def get_elevation_range_from_metadata(
with rio.open(img) as descriptor:
gdal_height_offset = descriptor.get_tag_item("HEIGHT_OFF", "RPC")
gdal_height_scale = descriptor.get_tag_item("HEIGHT_SCALE", "RPC")
gdal_height_offset = safe_cast_float(gdal_height_offset)
gdal_height_scale = safe_cast_float(gdal_height_scale)

if gdal_height_scale is not None and gdal_height_offset is not None:
if isinstance(gdal_height_offset, str):
Expand Down
12 changes: 12 additions & 0 deletions tests/core/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,15 @@ def test_angle_vectors():
angle_result = utils.angle_vectors(vector_1, vector_2)

assert angle_result == angle_ref


@pytest.mark.unit_tests
def test_safe_cast_float():
"""
Testing safe_cast_float
"""

data = "1256.36586 meters"
transformed_data = utils.safe_cast_float(data)

assert transformed_data == 1256.36586

0 comments on commit 6a0c3e5

Please sign in to comment.