Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add contains predicate #11

Merged
merged 2 commits into from
Dec 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,4 @@ Predicates

equals
intersects
contains
22 changes: 22 additions & 0 deletions src/predicates.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ bool equals(PyObjectGeography a, PyObjectGeography b) {
return s2geog::s2_equals(a_index, b_index, options);
}

bool contains(PyObjectGeography a, PyObjectGeography b) {
const auto& a_index = a.as_geog_ptr()->geog_index();
const auto& b_index = b.as_geog_ptr()->geog_index();

S2BooleanOperation::Options options;
return s2geog::s2_contains(a_index, b_index, options);
}

void init_predicates(py::module& m) {
m.def("intersects", py::vectorize(&intersects), py::arg("a"), py::arg("b"),
R"pbdoc(
Expand Down Expand Up @@ -51,4 +59,18 @@ void init_predicates(py::module& m) {
Geography object(s)

)pbdoc");

m.def("contains", py::vectorize(&contains), py::arg("a"), py::arg("b"),
R"pbdoc(
Returns True if B is completely inside A.

A contains B if no points of B lie in the exterior of A and at least
one point of the interior of B lies in the interior of A.

Parameters
----------
a, b : :py:class:`Geography` or array_like
Geography object(s)

)pbdoc");
}
1 change: 1 addition & 0 deletions src/spherely.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ destroy_prepared: _VFunc_Nin1_Nout1[Literal["destroy_prepared"], Geography, Any]

intersects: _VFunc_Nin2_Nout1[Literal["intersects"], bool, bool]
equals: _VFunc_Nin2_Nout1[Literal["intersects"], bool, bool]
contains: _VFunc_Nin2_Nout1[Literal["contains"], bool, bool]

# temp (remove)

Expand Down
20 changes: 20 additions & 0 deletions tests/test_predicates.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,23 @@ def test_equals() -> None:
a2 = spherely.Point(50, 8)
b2 = spherely.Point(50, 8)
assert spherely.equals(a2, b2)


def test_contains():
# test array + scalar
a = np.array(
[
spherely.LineString([(40, 8), (60, 8)]),
spherely.LineString([(20, 0), (30, 0)]),
]
)
b = spherely.Point(40, 8)

actual = spherely.contains(a, b)
expected = np.array([True, False])
np.testing.assert_array_equal(actual, expected)

# two scalars
a2 = spherely.LineString([(50, 8), (60, 8)])
b2 = spherely.Point(50, 8)
assert spherely.contains(a2, b2)