Skip to content

Commit

Permalink
Add contains predicate (#11)
Browse files Browse the repository at this point in the history
* Add contains predicate

* add to docs
  • Loading branch information
jorisvandenbossche authored Dec 19, 2022
1 parent 34af513 commit c13cef0
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
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)

0 comments on commit c13cef0

Please sign in to comment.