Skip to content

Commit

Permalink
Fix tests with new geoutils
Browse files Browse the repository at this point in the history
  • Loading branch information
rhugonnet committed Mar 20, 2024
1 parent 36ce632 commit 42ae7da
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 16 deletions.
9 changes: 0 additions & 9 deletions tests/test_coreg/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,6 @@ def test_error_method(self) -> None:
dem3 = dem1.copy() + np.random.random(size=dem1.size).reshape(dem1.shape)
assert abs(vshiftcorr.error(dem1, dem3, transform=affine, crs=crs, error_type="std") - np.std(dem3)) < 1e-6

def test_ij_xy(self, i: int = 10, j: int = 20) -> None:
"""
Test the reversibility of ij2xy and xy2ij, which is important for point co-registration.
"""
x, y = self.ref.ij2xy(i, j, offset="ul")
i, j = self.ref.xy2ij(x, y, shift_area_or_point=False)
assert i == pytest.approx(10)
assert j == pytest.approx(20)

@pytest.mark.parametrize("subsample", [10, 10000, 0.5, 1]) # type: ignore
def test_get_subsample_on_valid_mask(self, subsample: float | int) -> None:
"""Test the subsampling function called by all subclasses"""
Expand Down
8 changes: 7 additions & 1 deletion tests/test_dem.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,9 @@ def test_set_vcrs(self) -> None:
assert dem.vcrs_grid == "us_nga_egm08_25.tif"

# -- Test 2: we check with grids --
# Most grids aren't going to be downloaded, so this warning can be raised
warnings.filterwarnings("ignore", category=UserWarning, message="Grid not found in *")

dem.set_vcrs(new_vcrs="us_nga_egm96_15.tif")
assert dem.vcrs_name == "unknown using geoidgrids=us_nga_egm96_15.tif"
assert dem.vcrs_grid == "us_nga_egm96_15.tif"
Expand Down Expand Up @@ -312,13 +315,16 @@ def test_to_vcrs__equal_warning(self) -> None:
# Compare to manually-extracted shifts at specific coordinates for the geoid grids
egm96_chile = {"grid": "us_nga_egm96_15.tif", "lon": -68, "lat": -20, "shift": 42}
egm08_chile = {"grid": "us_nga_egm08_25.tif", "lon": -68, "lat": -20, "shift": 42}
geoid96_alaska = {"grid": "us_noaa_geoid06_ak.tif", "lon": -145, "lat": 62, "shift": 17}
geoid96_alaska = {"grid": "us_noaa_geoid06_ak.tif", "lon": -145, "lat": 62, "shift": 15}
isn93_iceland = {"grid": "is_lmi_Icegeoid_ISN93.tif", "lon": -18, "lat": 65, "shift": 68}

@pytest.mark.parametrize("grid_shifts", [egm08_chile, egm08_chile, geoid96_alaska, isn93_iceland]) # type: ignore
def test_to_vcrs__grids(self, grid_shifts: dict[str, Any]) -> None:
"""Tests grids to convert vertical CRS."""

# Most grids aren't going to be downloaded, so this warning can be raised
warnings.filterwarnings("ignore", category=UserWarning, message="Grid not found in *")

# Using an arbitrary elevation of 100 m (no influence on the transformation)
dem = DEM.from_array(
data=np.array([[100, 100]]),
Expand Down
15 changes: 14 additions & 1 deletion tests/test_vcrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import pathlib
import re
import warnings
from typing import Any

import numpy as np
Expand All @@ -12,8 +13,8 @@
import xdem
import xdem.vcrs


class TestVCRS:

def test_parse_vcrs_name_from_product(self) -> None:
"""Test parsing of vertical CRS name from DEM product name."""

Expand Down Expand Up @@ -66,6 +67,9 @@ def test_vcrs_from_crs(self, input_output: tuple[CRS, CRS]) -> None:
def test_vcrs_from_user_input(self, vcrs_input: str | pathlib.Path | int | CRS) -> None:
"""Tests the function _vcrs_from_user_input for varying user inputs, for which it will return a CRS."""

# Most grids aren't going to be downloaded, so this warning can be raised
warnings.filterwarnings("ignore", category=UserWarning, message="Grid not found in *")

# Get user input
vcrs = xdem.dem._vcrs_from_user_input(vcrs_input)

Expand Down Expand Up @@ -116,6 +120,9 @@ def test_vcrs_from_user_input__errors(self) -> None:
def test_build_vcrs_from_grid(self, grid: str) -> None:
"""Test that vertical CRS are correctly built from grid"""

# Most grids aren't going to be downloaded, so this warning can be raised
warnings.filterwarnings("ignore", category=UserWarning, message="Grid not found in *")

# Build vertical CRS
vcrs = xdem.vcrs._build_vcrs_from_grid(grid=grid)
assert vcrs.is_vertical
Expand All @@ -132,6 +139,9 @@ def test_build_vcrs_from_grid(self, grid: str) -> None:
def test_build_ccrs_from_crs_and_vcrs(self, crs: CRS, vcrs_input: CRS | str) -> None:
"""Test the function build_ccrs_from_crs_and_vcrs."""

# Most grids aren't going to be downloaded, so this warning can be raised
warnings.filterwarnings("ignore", category=UserWarning, message="Grid not found in *")

# Get the vertical CRS from user input
vcrs = xdem.vcrs._vcrs_from_user_input(vcrs_input=vcrs_input)

Expand Down Expand Up @@ -180,6 +190,9 @@ def test_build_ccrs_from_crs_and_vcrs__errors(self) -> None:
def test_transform_zz(self, grid_shifts: dict[str, Any]) -> None:
"""Tests grids to convert vertical CRS."""

# Most grids aren't going to be downloaded, so this warning can be raised
warnings.filterwarnings("ignore", category=UserWarning, message="Grid not found in *")

# Using an arbitrary elevation of 100 m (no influence on the transformation)
zz = 100
xx = grid_shifts["lon"]
Expand Down
6 changes: 2 additions & 4 deletions xdem/coreg/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,7 @@ def _residuals_df(
arr_ = dem.data.astype(np.float32)

# get residual error at the point on DEM.
i, j = dem.xy2ij(
df_shifted["E"].values, df_shifted["N"].values, op=np.float32, shift_area_or_point=("AREA_OR_POINT" in dem.tags)
)
i, j = dem.xy2ij(df_shifted["E"].values, df_shifted["N"].values)

# ndimage return
dem_h = scipy.ndimage.map_coordinates(arr_, [i, j], order=1, mode="nearest", **kwargs)
Expand Down Expand Up @@ -177,7 +175,7 @@ def _df_sampling_from_dem(
mask = dem.data.mask

# Get value
x, y = dem.ij2xy(i[~mask[i, j]], j[~mask[i, j]], offset=offset)
x, y = dem.ij2xy(i[~mask[i, j]], j[~mask[i, j]])
z = scipy.ndimage.map_coordinates(
dem.data.astype(np.float32), [i[~mask[i, j]], j[~mask[i, j]]], order=order, mode="nearest"
)
Expand Down
2 changes: 1 addition & 1 deletion xdem/dem.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ def to_vcrs(

# Transform elevation with new vertical CRS
zz = self.data
xx, yy = self.coords(offset="center")
xx, yy = self.coords()
zz_trans = _transform_zz(crs_from=src_ccrs, crs_to=dst_ccrs, xx=xx, yy=yy, zz=zz)

# Update DEM
Expand Down

0 comments on commit 42ae7da

Please sign in to comment.