Skip to content

Commit

Permalink
add geovista.crs.has_wkt (#440)
Browse files Browse the repository at this point in the history
  • Loading branch information
bjlittle authored Sep 16, 2023
1 parent dc4c195 commit 42d8336
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 18 deletions.
24 changes: 23 additions & 1 deletion src/geovista/crs.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"WGS84",
"from_wkt",
"get_central_meridian",
"has_wkt",
"projected",
"set_central_meridian",
"to_wkt",
Expand Down Expand Up @@ -59,7 +60,7 @@ def from_wkt(mesh: pv.PolyData) -> CRS:
"""
crs = None

if GV_FIELD_CRS in mesh.field_data:
if has_wkt(mesh):
wkt = str(mesh.field_data[GV_FIELD_CRS][0])
crs = CRS.from_wkt(wkt)

Expand Down Expand Up @@ -100,6 +101,27 @@ def get_central_meridian(crs: CRS) -> float | None:
return result


def has_wkt(mesh: pv.PolyData) -> bool:
"""Determine whether the provided mesh has a CRS serialized as WKT attached.
Parameters
----------
mesh : PolyData
The mesh to be inspected for a serialized CRS.
Returns
-------
bool
Whether the mesh has a CRS serialized as WKT attached.
Notes
-----
.. versionadded:: 0.4.0
"""
return GV_FIELD_CRS in mesh.field_data


def projected(mesh: pv.PolyData) -> bool:
"""Determine if the mesh is a planar projection.
Expand Down
7 changes: 7 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import numpy as np
import pytest
import pyvista as pv

from geovista.bridge import Transform
from geovista.crs import WGS84
Expand Down Expand Up @@ -68,6 +69,12 @@ def lfric_sst():
return mesh


@pytest.fixture()
def sphere():
"""Fixture to provide a pyvista sphere mesh."""
return pv.Sphere()


@pytest.fixture
def wgs84_wkt():
"""Fixture for generating WG284 CRS WKT as a string."""
Expand Down
2 changes: 1 addition & 1 deletion tests/core/test_slice_lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def test_projected_fail(mesh):
_ = slice_lines(mesh)


@pytest.mark.parametrize("mesh", [pv.Sphere(), lam_uk(), lfric()])
@pytest.mark.parametrize("mesh", [pv.Cube(), lam_uk(), lfric()])
def test_no_lines(mesh):
"""Test nop slicing mesh with no lines."""
result = slice_lines(mesh)
Expand Down
14 changes: 14 additions & 0 deletions tests/crs/test_has_wkt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"""Unit-test for :func:`geovista.crs.has_wkt`."""
from __future__ import annotations

import pytest

from geovista.crs import has_wkt


@pytest.mark.parametrize("mesh, expected", [("sphere", False), ("lfric", True)])
def test(mesh, expected, request):
"""Test mesh wkt serialization availability."""
mesh = request.getfixturevalue(mesh)
result = has_wkt(mesh)
assert result is expected
15 changes: 7 additions & 8 deletions tests/crs/test_projected.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from pyproj import CRS
import pyvista as pv

from geovista.crs import WGS84, projected, to_wkt
from geovista.crs import WGS84, has_wkt, projected, to_wkt


def test_planar__no_crs():
Expand All @@ -20,15 +20,14 @@ def test_planer__with_crs():
assert not projected(mesh)


def test_non_planar__no_crs():
def test_non_planar__no_crs(sphere):
"""Test that the mesh is not projected."""
mesh = pv.Sphere()
assert not projected(mesh)
assert not projected(sphere)


def test_non_planer__with_crs():
def test_non_planer__with_crs(sphere):
"""Test that the mesh CRS is used over the geometry heuristic."""
mesh = pv.Sphere()
assert has_wkt(sphere) is False
crs = CRS.from_user_input("+proj=eqc")
to_wkt(mesh, crs)
assert projected(mesh)
to_wkt(sphere, crs)
assert projected(sphere)
13 changes: 5 additions & 8 deletions tests/crs/test_to_wkt.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
"""Unit-tests for :func:`geovista.crs.to_wkt`."""
from __future__ import annotations

import pyvista as pv

from geovista.common import GV_FIELD_CRS
from geovista.crs import WGS84, from_wkt, to_wkt


def test():
def test(sphere):
"""Test OGC WKT serialization of CRS on mesh."""
mesh = pv.Sphere()
crs = WGS84
to_wkt(mesh, crs)
assert GV_FIELD_CRS in mesh.field_data
to_wkt(sphere, crs)
assert GV_FIELD_CRS in sphere.field_data
wkt = WGS84.to_wkt()
assert mesh.field_data[GV_FIELD_CRS] == wkt
assert from_wkt(mesh) == WGS84
assert sphere.field_data[GV_FIELD_CRS] == wkt
assert from_wkt(sphere) == WGS84

0 comments on commit 42d8336

Please sign in to comment.