diff --git a/src/accessors-geog.cpp b/src/accessors-geog.cpp index 4d413df..2eaa158 100644 --- a/src/accessors-geog.cpp +++ b/src/accessors-geog.cpp @@ -26,6 +26,10 @@ PyObjectGeography convex_hull(PyObjectGeography a) { return make_py_geography(s2geog::s2_convex_hull(a_ptr)); } +bool is_empty(PyObjectGeography a) { + return s2geog::s2_is_empty(a.as_geog_ptr()->geog()); +} + double distance(PyObjectGeography a, PyObjectGeography b, double radius = EARTH_RADIUS_METERS) { const auto& a_index = a.as_geog_ptr()->geog_index(); const auto& b_index = b.as_geog_ptr()->geog_index(); @@ -78,6 +82,19 @@ void init_accessors(py::module& m) { )pbdoc"); + m.def("is_empty", + py::vectorize(&is_empty), + py::arg("a"), + R"pbdoc( + Returns True if the geography object is empty, False otherwise. + + Parameters + ---------- + a : :py:class:`Geography` or array_like + Geography object + + )pbdoc"); + m.def("distance", py::vectorize(&distance), py::arg("a"), diff --git a/src/spherely.pyi b/src/spherely.pyi index d2382c8..ab53318 100644 --- a/src/spherely.pyi +++ b/src/spherely.pyi @@ -182,6 +182,7 @@ is_geography: _VFunc_Nin1_Nout1[Literal["is_geography"], bool, bool] is_prepared: _VFunc_Nin1_Nout1[Literal["is_prepared"], bool, bool] prepare: _VFunc_Nin1_Nout1[Literal["prepare"], Geography, Any] destroy_prepared: _VFunc_Nin1_Nout1[Literal["destroy_prepared"], Geography, Any] +is_empty: _VFunc_Nin1_Nout1[Literal["is_empty"], bool, bool] # predicates diff --git a/tests/test_accessors.py b/tests/test_accessors.py index a203db3..3fb0f06 100644 --- a/tests/test_accessors.py +++ b/tests/test_accessors.py @@ -84,6 +84,24 @@ def test_convex_hull(geog, expected) -> None: assert spherely.equals(actual, expected) +def test_is_empty(): + arr = spherely.from_wkt( + [ + "POINT (0 0)", + "POINT EMPTY", + "LINESTRING (0 0, 1 1)", + "LINESTRING EMPTY", + "POLYGON ((0 0, 10 0, 10 10, 0 10, 0 0))", + "POLYGON EMPTY", + "GEOMETRYCOLLECTION EMPTY", + "GEOMETRYCOLLECTION (POINT EMPTY)", + ] + ) + result = spherely.is_empty(arr) + expected = np.array([False, True, False, True, False, True, True, True]) + np.testing.assert_array_equal(result, expected) + + @pytest.mark.parametrize( "geog_a, geog_b, expected", [