Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

add transform point and points #409

Merged
merged 9 commits into from
Sep 16, 2023
4 changes: 2 additions & 2 deletions src/geovista/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ def transform_points(
raise ValueError(emsg)

def combine(xs: ArrayLike, ys: ArrayLike, zs: ArrayLike | None = None) -> ArrayLike:
"""Combine the provided points into a single array with shape (N,3).
"""Combine the provided points into a single array with shape (N, 3).

Parameters
----------
Expand Down Expand Up @@ -362,6 +362,6 @@ def combine(xs: ArrayLike, ys: ArrayLike, zs: ArrayLike | None = None) -> ArrayL

if xndim == 2:
shape.append(3)
result = result.reshape(**shape)
result = result.reshape(tuple(shape))

return result
43 changes: 42 additions & 1 deletion tests/transform/test_transform_points.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import pytest

from geovista.crs import WGS84
from geovista.transform import transform_points
from geovista.transform import Transformer, transform_points


@pytest.mark.parametrize(
Expand Down Expand Up @@ -70,3 +70,44 @@ def test_z_size_fail():
emsg = "Cannot transform points, 'xs' and 'zs' require same length"
with pytest.raises(ValueError, match=emsg):
_ = transform_points(src_crs=WGS84, tgt_crs=WGS84, xs=data, ys=data, zs=zs)


@pytest.mark.parametrize("zoffset", [None, 100])
@pytest.mark.parametrize("reshape", [False, True])
@pytest.mark.parametrize("roundtrip", [False, True])
def test_transform(mocker, zoffset, reshape, roundtrip):
"""Test transformation with identical and different CRSs."""
xs = np.arange(size := 10, dtype=float)
ys = np.arange(size, dtype=float) + 10
zs = np.arange(size, dtype=float) + zoffset if zoffset is not None else zoffset
if reshape:
shape = (2, 5)
xs, ys = xs.reshape(shape), ys.reshape(shape)
if zs is not None:
zs = zs.reshape(shape)
else:
shape = (size,)
shape = shape + (3,)
spy_from_crs = mocker.spy(Transformer, "from_crs")
spy_transform = mocker.spy(Transformer, "transform")
if roundtrip:
tmp = transform_points(src_crs=WGS84, tgt_crs="+proj=eqc", xs=xs, ys=ys, zs=zs)
result = transform_points(
src_crs="+proj=eqc",
tgt_crs=WGS84,
xs=tmp[..., 0],
ys=tmp[..., 1],
zs=tmp[..., 2],
)
bjlittle marked this conversation as resolved.
Show resolved Hide resolved
else:
result = transform_points(src_crs=WGS84, tgt_crs=WGS84, xs=xs, ys=ys, zs=zs)
if zoffset is None:
zs = np.zeros_like(xs)
expected = np.vstack([xs.flatten(), ys.flatten(), zs.flatten()]).T
if reshape:
expected = expected.reshape(shape)
np.testing.assert_array_almost_equal(result, expected)
call_count = 2 if roundtrip else 0
assert spy_from_crs.call_count == call_count
assert spy_transform.call_count == call_count
assert result.shape == shape