Skip to content

Commit

Permalink
128 support conversion of spherical ugrid2d/ugrid1d to meshkernel (#186)
Browse files Browse the repository at this point in the history
* added is_geographic property and test

* minimized diff

* fixed test

* also added for 1d

* black

* manual linting fix, was changed by blck

* formatting

* formatting
  • Loading branch information
veenstrajelmer authored Dec 4, 2023
1 parent 4699b32 commit 699452b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
10 changes: 10 additions & 0 deletions tests/test_ugrid_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,16 @@ def test_crs(self):
assert not np.array_equal(result.ugrid.grid.node_x, uda.ugrid.grid.node_x)
assert not np.array_equal(result.ugrid.grid.node_y, uda.ugrid.grid.node_y)

def test_is_geographic(self):
uda = self.uda
assert uda.grid.is_geographic is False

uda.ugrid.set_crs(epsg=4326)
assert uda.grid.is_geographic is True

result = uda.ugrid.to_crs(epsg=28992)
assert result.grid.is_geographic is False

def test_to_geodataframe(self):
with pytest.raises(ValueError, match="unable to convert unnamed"):
self.uda.ugrid.to_geodataframe()
Expand Down
6 changes: 5 additions & 1 deletion xugrid/ugrid/ugrid1d.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,11 @@ def meshkernel(self) -> "mk.MeshKernel": # type: ignore # noqa
import meshkernel as mk

if self._meshkernel is None:
self._meshkernel = mk.MeshKernel()
if self.is_geographic:
mk_projection = mk.ProjectionType.SPHERICAL
else:
mk_projection = mk.ProjectionType.CARTESIAN
self._meshkernel = mk.MeshKernel(mk_projection)
self._meshkernel.mesh1d_set(self.mesh)
return self._meshkernel

Expand Down
6 changes: 5 additions & 1 deletion xugrid/ugrid/ugrid2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,11 @@ def meshkernel(self) -> "mk.MeshKernel": # type: ignore # noqa
import meshkernel as mk

if self._meshkernel is None:
self._meshkernel = mk.MeshKernel()
if self.is_geographic:
mk_projection = mk.ProjectionType.SPHERICAL
else:
mk_projection = mk.ProjectionType.CARTESIAN
self._meshkernel = mk.MeshKernel(mk_projection)
self._meshkernel.mesh2d_set(self.mesh)
return self._meshkernel

Expand Down
6 changes: 6 additions & 0 deletions xugrid/ugrid/ugridbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,12 @@ def to_crs(

return grid

@property
def is_geographic(self):
if self.crs is None:
return False
return self.crs.is_geographic

def plot(self, **kwargs):
"""
Plot the edges of the mesh.
Expand Down

0 comments on commit 699452b

Please sign in to comment.