1 // Boost.Geometry Index
3 // Spatial query predicates
5 // Copyright (c) 2011-2018 Adam Wulkiewicz, Lodz, Poland.
7 // Use, modification and distribution is subject to the Boost Software License,
8 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
9 // http://www.boost.org/LICENSE_1_0.txt)
11 #ifndef BOOST_GEOMETRY_INDEX_PREDICATES_HPP
12 #define BOOST_GEOMETRY_INDEX_PREDICATES_HPP
14 #include <boost/geometry/index/detail/predicates.hpp>
15 #include <boost/geometry/index/detail/tuples.hpp>
18 \defgroup predicates Predicates (boost::geometry::index::)
21 namespace boost { namespace geometry { namespace index {
24 \brief Generate \c contains() predicate.
26 Generate a predicate defining Value and Geometry relationship. With this
27 predicate query returns indexed Values that contain passed Geometry.
28 Value is returned by the query if <tt>bg::within(Geometry, Indexable)</tt>
29 returns <tt>true</tt>.
33 bgi::query(spatial_index, bgi::contains(box), std::back_inserter(result));
38 \tparam Geometry The Geometry type.
40 \param g The Geometry object.
42 template <typename Geometry> inline
43 detail::predicates::spatial_predicate<Geometry, detail::predicates::contains_tag, false>
44 contains(Geometry const& g)
46 return detail::predicates::spatial_predicate
49 detail::predicates::contains_tag,
55 \brief Generate \c covered_by() predicate.
57 Generate a predicate defining Value and Geometry relationship. With this
58 predicate query returns indexed Values that are covered by passed Geometry.
59 Value is returned by the query if <tt>bg::covered_by(Indexable, Geometry)</tt>
60 returns <tt>true</tt>.
64 bgi::query(spatial_index, bgi::covered_by(box), std::back_inserter(result));
69 \tparam Geometry The Geometry type.
71 \param g The Geometry object.
73 template <typename Geometry> inline
74 detail::predicates::spatial_predicate<Geometry, detail::predicates::covered_by_tag, false>
75 covered_by(Geometry const& g)
77 return detail::predicates::spatial_predicate
80 detail::predicates::covered_by_tag,
86 \brief Generate \c covers() predicate.
88 Generate a predicate defining Value and Geometry relationship. With this
89 predicate query returns indexed Values that cover passed Geometry.
90 Value is returned by the query if <tt>bg::covered_by(Geometry, Indexable)</tt>
91 returns <tt>true</tt>.
95 bgi::query(spatial_index, bgi::covers(box), std::back_inserter(result));
100 \tparam Geometry The Geometry type.
102 \param g The Geometry object.
104 template <typename Geometry> inline
105 detail::predicates::spatial_predicate<Geometry, detail::predicates::covers_tag, false>
106 covers(Geometry const& g)
108 return detail::predicates::spatial_predicate
111 detail::predicates::covers_tag,
117 \brief Generate \c disjoint() predicate.
119 Generate a predicate defining Value and Geometry relationship. With this
120 predicate query returns indexed Values that are disjoint with passed Geometry.
121 Value is returned by the query if <tt>bg::disjoint(Indexable, Geometry)</tt>
122 returns <tt>true</tt>.
126 bgi::query(spatial_index, bgi::disjoint(box), std::back_inserter(result));
131 \tparam Geometry The Geometry type.
133 \param g The Geometry object.
135 template <typename Geometry> inline
136 detail::predicates::spatial_predicate<Geometry, detail::predicates::disjoint_tag, false>
137 disjoint(Geometry const& g)
139 return detail::predicates::spatial_predicate
142 detail::predicates::disjoint_tag,
148 \brief Generate \c intersects() predicate.
150 Generate a predicate defining Value and Geometry relationship. With this
151 predicate query returns indexed Values that intersect passed Geometry.
152 Value is returned by the query if <tt>bg::intersects(Indexable, Geometry)</tt>
153 returns <tt>true</tt>.
157 bgi::query(spatial_index, bgi::intersects(box), std::back_inserter(result));
158 bgi::query(spatial_index, bgi::intersects(ring), std::back_inserter(result));
159 bgi::query(spatial_index, bgi::intersects(polygon), std::back_inserter(result));
164 \tparam Geometry The Geometry type.
166 \param g The Geometry object.
168 template <typename Geometry> inline
169 detail::predicates::spatial_predicate<Geometry, detail::predicates::intersects_tag, false>
170 intersects(Geometry const& g)
172 return detail::predicates::spatial_predicate
175 detail::predicates::intersects_tag,
181 \brief Generate \c overlaps() predicate.
183 Generate a predicate defining Value and Geometry relationship. With this
184 predicate query returns indexed Values that overlap passed Geometry.
185 Value is returned by the query if <tt>bg::overlaps(Indexable, Geometry)</tt>
186 returns <tt>true</tt>.
190 bgi::query(spatial_index, bgi::overlaps(box), std::back_inserter(result));
195 \tparam Geometry The Geometry type.
197 \param g The Geometry object.
199 template <typename Geometry> inline
200 detail::predicates::spatial_predicate<Geometry, detail::predicates::overlaps_tag, false>
201 overlaps(Geometry const& g)
203 return detail::predicates::spatial_predicate
206 detail::predicates::overlaps_tag,
211 #ifdef BOOST_GEOMETRY_INDEX_DETAIL_EXPERIMENTAL
214 \brief Generate \c touches() predicate.
216 Generate a predicate defining Value and Geometry relationship. With this
217 predicate query returns indexed Values that touch passed Geometry.
218 Value is returned by the query if <tt>bg::touches(Indexable, Geometry)</tt>
219 returns <tt>true</tt>.
223 \tparam Geometry The Geometry type.
225 \param g The Geometry object.
227 template <typename Geometry> inline
228 detail::predicates::spatial_predicate<Geometry, detail::predicates::touches_tag, false>
229 touches(Geometry const& g)
231 return detail::predicates::spatial_predicate
234 detail::predicates::touches_tag,
239 #endif // BOOST_GEOMETRY_INDEX_DETAIL_EXPERIMENTAL
242 \brief Generate \c within() predicate.
244 Generate a predicate defining Value and Geometry relationship. With this
245 predicate query returns indexed Values that are within passed Geometry.
246 Value is returned by the query if <tt>bg::within(Indexable, Geometry)</tt>
247 returns <tt>true</tt>.
251 bgi::query(spatial_index, bgi::within(box), std::back_inserter(result));
256 \tparam Geometry The Geometry type.
258 \param g The Geometry object.
260 template <typename Geometry> inline
261 detail::predicates::spatial_predicate<Geometry, detail::predicates::within_tag, false>
262 within(Geometry const& g)
264 return detail::predicates::spatial_predicate
267 detail::predicates::within_tag,
273 \brief Generate satisfies() predicate.
275 A wrapper around user-defined UnaryPredicate checking if Value should be returned by spatial query.
279 bool is_red(Value const& v) { return v.is_red(); }
282 template <typename Value> bool operator()(Value const& v) { return v.is_red(); }
287 rt.query(index::intersects(box) && index::satisfies(is_red),
288 std::back_inserter(result));
290 rt.query(index::intersects(box) && index::satisfies(is_red_o()),
291 std::back_inserter(result));
293 #ifndef BOOST_NO_CXX11_LAMBDAS
294 rt.query(index::intersects(box) && index::satisfies([](Value const& v) { return v.is_red(); }),
295 std::back_inserter(result));
301 \tparam UnaryPredicate A type of unary predicate function or function object.
303 \param pred The unary predicate function or function object.
305 template <typename UnaryPredicate> inline
306 detail::predicates::satisfies<UnaryPredicate, false>
307 satisfies(UnaryPredicate const& pred)
309 return detail::predicates::satisfies<UnaryPredicate, false>(pred);
313 \brief Generate nearest() predicate.
315 When nearest predicate is passed to the query, k-nearest neighbour search will be performed.
316 \c nearest() predicate takes a \c Geometry from which distances to \c Values are calculated
317 and the maximum number of \c Values that should be returned. Internally
318 boost::geometry::comparable_distance() is used to perform the calculation.
322 bgi::query(spatial_index, bgi::nearest(pt, 5), std::back_inserter(result));
323 bgi::query(spatial_index, bgi::nearest(pt, 5) && bgi::intersects(box), std::back_inserter(result));
324 bgi::query(spatial_index, bgi::nearest(box, 5), std::back_inserter(result));
328 Only one \c nearest() predicate may be used in a query.
332 \param geometry The geometry from which distance is calculated.
333 \param k The maximum number of values to return.
335 template <typename Geometry> inline
336 detail::predicates::nearest<Geometry>
337 nearest(Geometry const& geometry, unsigned k)
339 return detail::predicates::nearest<Geometry>(geometry, k);
342 #ifdef BOOST_GEOMETRY_INDEX_DETAIL_EXPERIMENTAL
345 \brief Generate path() predicate.
347 When path predicate is passed to the query, the returned values are k values along the path closest to
348 its begin. \c path() predicate takes a \c Segment or a \c Linestring defining the path and the maximum
349 number of \c Values that should be returned.
353 bgi::query(spatial_index, bgi::path(segment, 5), std::back_inserter(result));
354 bgi::query(spatial_index, bgi::path(linestring, 5) && bgi::intersects(box), std::back_inserter(result));
358 Only one distance predicate (\c nearest() or \c path()) may be used in a query.
362 \param linestring The path along which distance is calculated.
363 \param k The maximum number of values to return.
365 template <typename SegmentOrLinestring> inline
366 detail::predicates::path<SegmentOrLinestring>
367 path(SegmentOrLinestring const& linestring, unsigned k)
369 return detail::predicates::path<SegmentOrLinestring>(linestring, k);
372 #endif // BOOST_GEOMETRY_INDEX_DETAIL_EXPERIMENTAL
374 namespace detail { namespace predicates {
376 // operator! generators
378 template <typename Fun, bool Negated> inline
379 satisfies<Fun, !Negated>
380 operator!(satisfies<Fun, Negated> const& p)
382 return satisfies<Fun, !Negated>(p);
385 template <typename Geometry, typename Tag, bool Negated> inline
386 spatial_predicate<Geometry, Tag, !Negated>
387 operator!(spatial_predicate<Geometry, Tag, Negated> const& p)
389 return spatial_predicate<Geometry, Tag, !Negated>(p.geometry);
392 // operator&& generators
394 template <typename Pred1, typename Pred2> inline
397 boost::tuples::cons<Pred2, boost::tuples::null_type>
399 operator&&(Pred1 const& p1, Pred2 const& p2)
401 /*typedef typename boost::mpl::if_c<is_predicate<Pred1>::value, Pred1, Pred1 const&>::type stored1;
402 typedef typename boost::mpl::if_c<is_predicate<Pred2>::value, Pred2, Pred2 const&>::type stored2;*/
403 namespace bt = boost::tuples;
406 bt::cons< Pred1, bt::cons<Pred2, bt::null_type> >
407 ( p1, bt::cons<Pred2, bt::null_type>(p2, bt::null_type()) );
410 template <typename Head, typename Tail, typename Pred> inline
411 typename tuples::push_back<
412 boost::tuples::cons<Head, Tail>, Pred
414 operator&&(boost::tuples::cons<Head, Tail> const& t, Pred const& p)
416 //typedef typename boost::mpl::if_c<is_predicate<Pred>::value, Pred, Pred const&>::type stored;
417 namespace bt = boost::tuples;
421 bt::cons<Head, Tail>, Pred
425 }} // namespace detail::predicates
427 }}} // namespace boost::geometry::index
429 #endif // BOOST_GEOMETRY_INDEX_PREDICATES_HPP