Skip to content

Commit

Permalink
test{aer,geodetic,ned} verify scalar-in,scalar-out
Browse files Browse the repository at this point in the history
  • Loading branch information
scivision committed Jan 29, 2024
1 parent ebe10dc commit 9c8a0ac
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 52 deletions.
1 change: 0 additions & 1 deletion src/pymap3d/tests/test_aer.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ def test_aer_enu(aer, enu):

@pytest.mark.parametrize("aer,ned", [((33, 70, 1000), (286.8422, 186.2775, -939.6926))])
def test_aer_ned(aer, ned):

ned1 = pm.aer2ned(*aer)
assert ned1 == approx(ned)
assert all(isinstance(n, float) for n in ned1)
Expand Down
62 changes: 34 additions & 28 deletions src/pymap3d/tests/test_geodetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,26 +58,33 @@ def test_scalar_geodetic2ecef(lla):

if isinstance(lla[0], list):
np = pytest.importorskip("numpy")
scalar = False
else:
scalar = True

xyz = pm.geodetic2ecef(*lla)
lla1 = pm.ecef2geodetic(*xyz)

try:
assert np.isclose(lla1, lla, rtol=1e-4).all()
np.testing.assert_allclose(lla1, lla, rtol=1e-4)
except NameError:
assert lla1 == approx(lla, rel=1e-4)

if scalar:
assert all(isinstance(n, float) for n in xyz)
assert all(isinstance(n, float) for n in lla1)


def test_array_geodetic2ecef():
np = pytest.importorskip("numpy")

lla = (np.asarray(lla0[0]), np.asarray(lla0[1]), np.asarray(lla0[2]))
xyz = pm.geodetic2ecef(*lla)
assert np.isclose(pm.ecef2geodetic(*xyz), lla).all()
np.testing.assert_allclose(pm.ecef2geodetic(*xyz), lla)

lla = (np.atleast_1d(lla0[0]), np.atleast_1d(lla0[1]), np.atleast_1d(lla0[2]))
xyz = pm.geodetic2ecef(*lla)
assert np.isclose(pm.ecef2geodetic(*xyz), lla).all()
np.testing.assert_allclose(pm.ecef2geodetic(*xyz), lla)


@pytest.mark.parametrize("xyz", [xyz0, ([xyz0[0]], [xyz0[1]], [xyz0[2]])], ids=("scalar", "list"))
Expand All @@ -88,26 +95,33 @@ def test_scalar_ecef2geodetic(xyz):

if isinstance(xyz[0], list):
np = pytest.importorskip("numpy")
scalar = False
else:
scalar = True

lla = pm.ecef2geodetic(*xyz)
xyz1 = pm.geodetic2ecef(*lla)

try:
assert np.isclose(xyz1, xyz, rtol=1e-4).all()
np.testing.assert_allclose(xyz1, xyz, rtol=1e-4)
except NameError:
assert xyz1 == approx(xyz, rel=1e-4)

if scalar:
assert all(isinstance(n, float) for n in xyz1)
assert all(isinstance(n, float) for n in lla)


def test_array_ecef2geodetic():
np = pytest.importorskip("numpy")

xyz = (np.asarray(xyz0[0]), np.asarray(xyz0[1]), np.asarray(xyz0[2]))
lla = pm.ecef2geodetic(*xyz)
assert np.isclose(pm.geodetic2ecef(*lla), xyz).all()
np.testing.assert_allclose(pm.geodetic2ecef(*lla), xyz)

xyz = (np.atleast_1d(xyz0[0]), np.atleast_1d(xyz0[1]), np.atleast_1d(xyz0[2]))
lla = pm.ecef2geodetic(*xyz)
assert np.isclose(pm.geodetic2ecef(*lla), xyz).all()
np.testing.assert_allclose(pm.geodetic2ecef(*lla), xyz)


def test_inside_ecef2geodetic():
Expand Down Expand Up @@ -201,19 +215,15 @@ def test_ecef2geodetic(xyz, lla):
],
)
def test_aer_geodetic(aer, lla, lla0):
lat1, lon1, alt1 = pm.aer2geodetic(*aer, *lla0)
assert lat1 == approx(lla[0])
assert lon1 == approx(lla[1])
assert alt1 == approx(lla[2])
assert isinstance(lat1, float)
assert isinstance(lon1, float)
assert isinstance(alt1, float)
lla1 = pm.aer2geodetic(*aer, *lla0)
assert lla1 == approx(lla)
assert all(isinstance(n, float) for n in lla1)

raer = (radians(aer[0]), radians(aer[1]), aer[2])
rlla0 = (radians(lla0[0]), radians(lla0[1]), lla0[2])
assert pm.aer2geodetic(*raer, *rlla0, deg=False) == approx(
(radians(lla[0]), radians(lla[1]), lla[2])
)
lla1 = pm.aer2geodetic(*raer, *rlla0, deg=False)
assert lla1 == approx((radians(lla[0]), radians(lla[1]), lla[2]))
assert all(isinstance(n, float) for n in lla1)

with pytest.raises(ValueError):
pm.aer2geodetic(aer[0], aer[1], -1, *lla0)
Expand All @@ -225,11 +235,11 @@ def test_aer_geodetic(aer, lla, lla0):


def test_scalar_nan():
a, e, r = pm.geodetic2aer(nan, nan, nan, *lla0)
assert isnan(a) and isnan(e) and isnan(r)
aer = pm.geodetic2aer(nan, nan, nan, *lla0)
assert all(isnan(n) for n in aer)

lat, lon, alt = pm.aer2geodetic(nan, nan, nan, *lla0)
assert isnan(lat) and isnan(lon) and isnan(alt)
llat = pm.aer2geodetic(nan, nan, nan, *lla0)
assert all(isnan(n) for n in llat)


def test_allnan():
Expand All @@ -251,23 +261,21 @@ def test_somenan():
@pytest.mark.parametrize("xyz, lla", xyzlla)
def test_numpy_ecef2geodetic(xyz, lla):
np = pytest.importorskip("numpy")
lat, lon, alt = pm.ecef2geodetic(
lla1 = pm.ecef2geodetic(
*np.array(
[
[xyz],
],
dtype=np.float32,
).T
)
assert lat[0] == approx(lla[0])
assert lon[0] == approx(lla[1])
assert alt[0] == approx(lla[2])
assert lla1 == approx(lla)


@pytest.mark.parametrize("lla, xyz", llaxyz)
def test_numpy_geodetic2ecef(lla, xyz):
np = pytest.importorskip("numpy")
x, y, z = pm.geodetic2ecef(
xyz1 = pm.geodetic2ecef(
*np.array(
[
[lla],
Expand All @@ -277,6 +285,4 @@ def test_numpy_geodetic2ecef(lla, xyz):
)

atol_dist = 1 # meters
assert x[0] == approx(xyz[0], abs=atol_dist)
assert y[0] == approx(xyz[1], abs=atol_dist)
assert z[0] == approx(xyz[2], abs=atol_dist)
assert xyz1 == approx(xyz, abs=atol_dist)
36 changes: 13 additions & 23 deletions src/pymap3d/tests/test_ned.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@ def test_ecef_ned():
ned = (enu[1], enu[0], -enu[2])
xyz = pm.aer2ecef(*aer0, *lla0)

n, e, d = pm.ecef2ned(*xyz, *lla0)
assert n == approx(ned[0])
assert e == approx(ned[1])
assert d == approx(ned[2])
ned1 = pm.ecef2ned(*xyz, *lla0)
assert ned1 == approx(ned)

assert pm.ned2ecef(*ned, *lla0) == approx(xyz)

Expand All @@ -31,25 +29,17 @@ def test_enuv_nedv():


def test_ned_geodetic():
lat1, lon1, alt1 = pm.aer2geodetic(*aer0, *lla0)
lla1 = pm.aer2geodetic(*aer0, *lla0)

enu3 = pm.geodetic2enu(lat1, lon1, alt1, *lla0)
enu3 = pm.geodetic2enu(*lla1, *lla0)
ned3 = (enu3[1], enu3[0], -enu3[2])

assert pm.geodetic2ned(lat1, lon1, alt1, *lla0) == approx(ned3)

lat, lon, alt = pm.enu2geodetic(*enu3, *lla0)
assert lat == approx(lat1)
assert lon == approx(lon1)
assert alt == approx(alt1)
assert isinstance(lat, float)
assert isinstance(lon, float)
assert isinstance(alt, float)

lat, lon, alt = pm.ned2geodetic(*ned3, *lla0)
assert lat == approx(lat1)
assert lon == approx(lon1)
assert alt == approx(alt1)
assert isinstance(lat, float)
assert isinstance(lon, float)
assert isinstance(alt, float)
assert pm.geodetic2ned(*lla1, *lla0) == approx(ned3)

lla2 = pm.enu2geodetic(*enu3, *lla0)
assert lla2 == approx(lla1)
assert all(isinstance(n, float) for n in lla2)

lla2 = pm.ned2geodetic(*ned3, *lla0)
assert lla2 == approx(lla1)
assert all(isinstance(n, float) for n in lla2)

0 comments on commit 9c8a0ac

Please sign in to comment.