-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpredicates.cpp
120 lines (96 loc) · 3.36 KB
/
predicates.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <s2/s2boolean_operation.h>
#include <s2geography.h>
#include <functional>
#include "geography.hpp"
#include "pybind11.hpp"
namespace py = pybind11;
namespace s2geog = s2geography;
using namespace spherely;
/*
** Functor for predicate bindings.
*/
class Predicate {
public:
using FuncType = std::function<bool(const s2geog::ShapeIndexGeography&,
const s2geog::ShapeIndexGeography&,
const S2BooleanOperation::Options&)>;
template <class F>
Predicate(F&& func) : m_func(std::forward<F>(func)) {}
bool operator()(PyObjectGeography a, PyObjectGeography b) const {
const auto& a_index = a.as_geog_ptr()->geog_index();
const auto& b_index = b.as_geog_ptr()->geog_index();
return m_func(a_index, b_index, m_options);
}
private:
FuncType m_func;
S2BooleanOperation::Options m_options;
};
void init_predicates(py::module& m) {
m.def("intersects",
py::vectorize(Predicate(s2geog::s2_intersects)),
py::arg("a"),
py::arg("b"),
R"pbdoc(
Returns True if A and B share any portion of space.
Intersects implies that overlaps, touches and within are True.
Parameters
----------
a, b : :py:class:`Geography` or array_like
Geography object(s)
)pbdoc");
m.def("equals",
py::vectorize(Predicate(s2geog::s2_equals)),
py::arg("a"),
py::arg("b"),
R"pbdoc(
Returns True if A and B are spatially equal.
If A is within B and B is within A, A and B are considered equal. The
ordering of points can be different.
Parameters
----------
a, b : :py:class:`Geography` or array_like
Geography object(s)
)pbdoc");
m.def("contains",
py::vectorize(Predicate(s2geog::s2_contains)),
py::arg("a"),
py::arg("b"),
R"pbdoc(
Returns True if B is completely inside A.
Parameters
----------
a, b : :py:class:`Geography` or array_like
Geography object(s)
)pbdoc");
m.def("within",
py::vectorize(Predicate([](const s2geog::ShapeIndexGeography& a_index,
const s2geog::ShapeIndexGeography& b_index,
const S2BooleanOperation::Options& options) {
return s2geog::s2_contains(b_index, a_index, options);
})),
py::arg("a"),
py::arg("b"),
R"pbdoc(
Returns True if A is completely inside B.
Parameters
----------
a, b : :py:class:`Geography` or array_like
Geography object(s)
)pbdoc");
m.def("disjoint",
py::vectorize(Predicate([](const s2geog::ShapeIndexGeography& a_index,
const s2geog::ShapeIndexGeography& b_index,
const S2BooleanOperation::Options& options) {
return !s2geog::s2_intersects(a_index, b_index, options);
})),
py::arg("a"),
py::arg("b"),
R"pbdoc(
Returns True if A boundaries and interior does not intersect at all
with those of B.
Parameters
----------
a, b : :py:class:`Geography` or array_like
Geography object(s)
)pbdoc");
}