Skip to content

Commit

Permalink
ENH: add is_empty
Browse files Browse the repository at this point in the history
  • Loading branch information
jorisvandenbossche committed Nov 24, 2024
1 parent acf3641 commit 691b576
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/accessors-geog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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"),
Expand Down
1 change: 1 addition & 0 deletions src/spherely.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
18 changes: 18 additions & 0 deletions tests/test_accessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
[
Expand Down

0 comments on commit 691b576

Please sign in to comment.