Skip to content

Commit

Permalink
Merge branch '164-mise-a-jour-des-pixels-size-en-sortie-en-fonction-d…
Browse files Browse the repository at this point in the history
…u-pas' into 'release'

Resolve "Mise à jour des pixels size en sortie en fonction du pas"

See merge request 3d/PandoraBox/pandora2d!148
  • Loading branch information
lecontm committed Sep 10, 2024
2 parents a79d066 + 455e1fb commit eea304a
Show file tree
Hide file tree
Showing 4 changed files with 278 additions and 122 deletions.
52 changes: 49 additions & 3 deletions pandora2d/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
from xarray import Coordinate as Coordinates

import os
from typing import Dict, Union
from typing import Dict, Union, Tuple
import xarray as xr
import numpy as np
from numpy.typing import NDArray
Expand Down Expand Up @@ -65,8 +65,8 @@ def save_dataset(dataset: xr.Dataset, cfg: Dict, output: str) -> None:
# remove ROI margins to save only user ROI in tif files
if "ROI" in cfg:
dataset = remove_roi_margins(dataset, cfg)
# Translate georeferencement origin to ROI origin:
dataset.attrs["transform"] *= Affine.translation(cfg["ROI"]["col"]["first"], cfg["ROI"]["row"]["first"])
if dataset.attrs["transform"] is not None:
adjust_georeferencement(dataset, cfg)
# create output dir
mkdir_p(output)

Expand Down Expand Up @@ -95,6 +95,52 @@ def save_dataset(dataset: xr.Dataset, cfg: Dict, output: str) -> None:
)


def adjust_georeferencement(dataset: xr.Dataset, cfg: Dict) -> None:
"""
Change origin in case a ROI is present and set pixel size to the matching cost step.
:param dataset: dataset to configure.
:type dataset: xr.Dataset
:param cfg: configuration
:type cfg: Dict
"""
if "ROI" in cfg:
# Translate georeferencement origin to ROI origin:
dataset.attrs["transform"] *= Affine.translation(cfg["ROI"]["col"]["first"], cfg["ROI"]["row"]["first"])
row_step, col_step = get_step(cfg)
set_pixel_size(dataset, row_step, col_step)


def get_step(cfg: Dict) -> Tuple[int, int]:
"""
Get step from matching cost or retun default value.
:param cfg: configuration
:type cfg: Dict
:return: row_step, col_step
:rtype: Tuple[int, int]
"""
try:
return cfg["pipeline"]["matching_cost"]["step"]
except KeyError:
return 1, 1


def set_pixel_size(dataset: xr.Dataset, row_step: int = 1, col_step: int = 1) -> None:
"""
Set the pixel size according to the step used in calculating the matching cost.
This ensures that all pixels are well geo-referenced in case a step is applied.
:param dataset: Data to save
:type dataset: xr.Dataset
:param row_step: step used in row
:type row_step: int
:param col_step: step used in column
:type col_step: int
"""
dataset.attrs["transform"] *= Affine.scale(col_step, row_step)


def dataset_disp_maps(
delta_row: np.ndarray,
delta_col: np.ndarray,
Expand Down
117 changes: 0 additions & 117 deletions tests/functional_tests/target_grid/roi/test_georeferencement.py

This file was deleted.

Loading

0 comments on commit eea304a

Please sign in to comment.