Skip to content

Commit

Permalink
add error testing and fix some shapely things
Browse files Browse the repository at this point in the history
  • Loading branch information
aulemahal committed Oct 8, 2021
1 parent 061cd37 commit 992755d
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
7 changes: 5 additions & 2 deletions cf_xarray/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,14 @@ def shapely_to_cf(geometries: Union[xr.DataArray, Sequence], grid_mapping: str =
- Other variables are not implemented as only Points are currently understood.
"""
# Get all types to call the appropriate translation function.
types = {geom.geom_type for geom in np.asarray(geometries)}
types = {
geom.item().geom_type if isinstance(geom, xr.DataArray) else geom.geom_type
for geom in geometries
}
if types.issubset({"Point", "MultiPoint"}):
ds = points_to_cf(geometries)
elif types.issubset({"Polygon", "MultiPolygon"}) or types.issubset(
{"Line", "MultiLine"}
{"LineString", "MultiLineString"}
):
raise NotImplementedError("Only point geometries conversion is implemented.")
else:
Expand Down
51 changes: 49 additions & 2 deletions cf_xarray/tests/test_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ def geometry_ds():

@requires_shapely
def test_shapely_to_cf(geometry_ds):
from shapely.geometry import Point

expected, in_ds = geometry_ds

out = xr.merge([in_ds.drop_vars("geometry"), cfxr.shapely_to_cf(in_ds.geometry)])
Expand Down Expand Up @@ -81,15 +83,55 @@ def test_shapely_to_cf(geometry_ds):
assert "longitude" in out.cf
assert "latitude" in out.cf

out = cfxr.shapely_to_cf([Point(2, 3)])
assert set(out.dims) == {"features", "node"}


@requires_shapely
def test_shapely_to_cf_errors():
from shapely.geometry import LineString, Point

geoms = [LineString([[1, 2], [2, 3]]), LineString([[2, 3, 4], [4, 3, 2]])]
with pytest.raises(NotImplementedError, match="Only point geometries conversion"):
cfxr.shapely_to_cf(geoms)

geoms.append(Point(1, 2))
with pytest.raises(ValueError, match="Mixed geometry types are not supported"):
cfxr.shapely_to_cf(geoms)

with pytest.raises(
NotImplementedError, match="Only grid mapping longitude_latitude"
):
cfxr.shapely_to_cf([Point(4, 5)], grid_mapping="albers_conical_equal_area")


@requires_shapely
def test_cf_to_shapely(geometry_ds):
in_ds, expected = geometry_ds
in_ds, exp = geometry_ds

xr.testing.assert_identical(
cfxr.cf_to_shapely(in_ds).drop_vars(["crd_x", "crd_y"]), expected.geometry
cfxr.cf_to_shapely(in_ds).drop_vars(["crd_x", "crd_y"]), exp.geometry
)

in_ds = in_ds.isel(index=slice(1, None), node=slice(2, None)).drop_vars(
"node_count"
)
del in_ds.geometry_container.attrs["node_count"]
out = cfxr.cf_to_shapely(in_ds)
assert out.dims == ("index",)


@requires_shapely
def test_cf_to_shapely_errors(geometry_ds):
in_ds, expected = geometry_ds
in_ds.geometry_container.attrs["geometry_type"] = "line"
with pytest.raises(NotImplementedError, match="Only point geometries conversion"):
cfxr.cf_to_shapely(in_ds)

in_ds.geometry_container.attrs["geometry_type"] = "punkt"
with pytest.raises(ValueError, match="Valid CF geometry types are "):
cfxr.cf_to_shapely(in_ds)


@requires_shapely
def test_reshape_unique_geometries(geometry_ds):
Expand All @@ -109,3 +151,8 @@ def test_reshape_unique_geometries(geometry_ds):
assert out.geometry.dims == ("features",)
assert out.data.dims == ("features", "time")
np.testing.assert_array_equal(out.time, [0, 1])

geoms = in_ds.geometry.expand_dims(n=[1, 2])
in_ds = in_ds.assign(geometry=geoms)
with pytest.raises(ValueError, match="The geometry variable must be 1D"):
cfxr.geometry.reshape_unique_geometries(in_ds)

0 comments on commit 992755d

Please sign in to comment.