1 // Boost.Geometry Index
3 // Spatial query predicates
5 // Copyright (c) 2011-2015 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
15 #include <boost/tuple/tuple.hpp>
16 #include <boost/mpl/assert.hpp>
18 #include <boost/geometry/index/detail/predicates.hpp>
19 #include <boost/geometry/index/detail/tuples.hpp>
22 \defgroup predicates Predicates (boost::geometry::index::)
25 namespace boost { namespace geometry { namespace index {
28 \brief Generate \c contains() predicate.
30 Generate a predicate defining Value and Geometry relationship.
31 Value will be returned by the query if <tt>bg::within(Geometry, Indexable)</tt>
36 bgi::query(spatial_index, bgi::contains(box), std::back_inserter(result));
41 \tparam Geometry The Geometry type.
43 \param g The Geometry object.
45 template <typename Geometry> inline
46 detail::predicates::spatial_predicate<Geometry, detail::predicates::contains_tag, false>
47 contains(Geometry const& g)
49 return detail::predicates::spatial_predicate
52 detail::predicates::contains_tag,
58 \brief Generate \c covered_by() predicate.
60 Generate a predicate defining Value and Geometry relationship.
61 Value will be returned by the query if <tt>bg::covered_by(Indexable, Geometry)</tt>
66 bgi::query(spatial_index, bgi::covered_by(box), std::back_inserter(result));
71 \tparam Geometry The Geometry type.
73 \param g The Geometry object.
75 template <typename Geometry> inline
76 detail::predicates::spatial_predicate<Geometry, detail::predicates::covered_by_tag, false>
77 covered_by(Geometry const& g)
79 return detail::predicates::spatial_predicate
82 detail::predicates::covered_by_tag,
88 \brief Generate \c covers() predicate.
90 Generate a predicate defining Value and Geometry relationship.
91 Value will be returned by the query if <tt>bg::covered_by(Geometry, Indexable)</tt>
96 bgi::query(spatial_index, bgi::covers(box), std::back_inserter(result));
101 \tparam Geometry The Geometry type.
103 \param g The Geometry object.
105 template <typename Geometry> inline
106 detail::predicates::spatial_predicate<Geometry, detail::predicates::covers_tag, false>
107 covers(Geometry const& g)
109 return detail::predicates::spatial_predicate
112 detail::predicates::covers_tag,
118 \brief Generate \c disjoint() predicate.
120 Generate a predicate defining Value and Geometry relationship.
121 Value will be returned by the query if <tt>bg::disjoint(Indexable, Geometry)</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.
151 Value will be returned by the query if <tt>bg::intersects(Indexable, Geometry)</tt>
156 bgi::query(spatial_index, bgi::intersects(box), std::back_inserter(result));
157 bgi::query(spatial_index, bgi::intersects(ring), std::back_inserter(result));
158 bgi::query(spatial_index, bgi::intersects(polygon), std::back_inserter(result));
163 \tparam Geometry The Geometry type.
165 \param g The Geometry object.
167 template <typename Geometry> inline
168 detail::predicates::spatial_predicate<Geometry, detail::predicates::intersects_tag, false>
169 intersects(Geometry const& g)
171 return detail::predicates::spatial_predicate
174 detail::predicates::intersects_tag,
180 \brief Generate \c overlaps() predicate.
182 Generate a predicate defining Value and Geometry relationship.
183 Value will be returned by the query if <tt>bg::overlaps(Indexable, Geometry)</tt>
188 bgi::query(spatial_index, bgi::overlaps(box), std::back_inserter(result));
193 \tparam Geometry The Geometry type.
195 \param g The Geometry object.
197 template <typename Geometry> inline
198 detail::predicates::spatial_predicate<Geometry, detail::predicates::overlaps_tag, false>
199 overlaps(Geometry const& g)
201 return detail::predicates::spatial_predicate
204 detail::predicates::overlaps_tag,
209 #ifdef BOOST_GEOMETRY_INDEX_DETAIL_EXPERIMENTAL
212 \brief Generate \c touches() predicate.
214 Generate a predicate defining Value and Geometry relationship.
215 Value will be returned by the query if <tt>bg::touches(Indexable, Geometry)</tt>
220 \tparam Geometry The Geometry type.
222 \param g The Geometry object.
224 template <typename Geometry> inline
225 detail::predicates::spatial_predicate<Geometry, detail::predicates::touches_tag, false>
226 touches(Geometry const& g)
228 return detail::predicates::spatial_predicate
231 detail::predicates::touches_tag,
236 #endif // BOOST_GEOMETRY_INDEX_DETAIL_EXPERIMENTAL
239 \brief Generate \c within() predicate.
241 Generate a predicate defining Value and Geometry relationship.
242 Value will be returned by the query if <tt>bg::within(Indexable, Geometry)</tt>
247 bgi::query(spatial_index, bgi::within(box), std::back_inserter(result));
252 \tparam Geometry The Geometry type.
254 \param g The Geometry object.
256 template <typename Geometry> inline
257 detail::predicates::spatial_predicate<Geometry, detail::predicates::within_tag, false>
258 within(Geometry const& g)
260 return detail::predicates::spatial_predicate
263 detail::predicates::within_tag,
269 \brief Generate satisfies() predicate.
271 A wrapper around user-defined UnaryPredicate checking if Value should be returned by spatial query.
275 bool is_red(Value const& v) { return v.is_red(); }
278 template <typename Value> bool operator()(Value const& v) { return v.is_red(); }
283 rt.query(index::intersects(box) && index::satisfies(is_red),
284 std::back_inserter(result));
286 rt.query(index::intersects(box) && index::satisfies(is_red_o()),
287 std::back_inserter(result));
289 #ifndef BOOST_NO_CXX11_LAMBDAS
290 rt.query(index::intersects(box) && index::satisfies([](Value const& v) { return v.is_red(); }),
291 std::back_inserter(result));
297 \tparam UnaryPredicate A type of unary predicate function or function object.
299 \param pred The unary predicate function or function object.
301 template <typename UnaryPredicate> inline
302 detail::predicates::satisfies<UnaryPredicate, false>
303 satisfies(UnaryPredicate const& pred)
305 return detail::predicates::satisfies<UnaryPredicate, false>(pred);
309 \brief Generate nearest() predicate.
311 When nearest predicate is passed to the query, k-nearest neighbour search will be performed.
312 \c nearest() predicate takes a \c Geometry from which distances to \c Values are calculated
313 and the maximum number of \c Values that should be returned. Internally
314 boost::geometry::comparable_distance() is used to perform the calculation.
318 bgi::query(spatial_index, bgi::nearest(pt, 5), std::back_inserter(result));
319 bgi::query(spatial_index, bgi::nearest(pt, 5) && bgi::intersects(box), std::back_inserter(result));
320 bgi::query(spatial_index, bgi::nearest(box, 5), std::back_inserter(result));
324 Only one \c nearest() predicate may be used in a query.
328 \param geometry The geometry from which distance is calculated.
329 \param k The maximum number of values to return.
331 template <typename Geometry> inline
332 detail::predicates::nearest<Geometry>
333 nearest(Geometry const& geometry, unsigned k)
335 return detail::predicates::nearest<Geometry>(geometry, k);
338 #ifdef BOOST_GEOMETRY_INDEX_DETAIL_EXPERIMENTAL
341 \brief Generate path() predicate.
343 When path predicate is passed to the query, the returned values are k values along the path closest to
344 its begin. \c path() predicate takes a \c Segment or a \c Linestring defining the path and the maximum
345 number of \c Values that should be returned.
349 bgi::query(spatial_index, bgi::path(segment, 5), std::back_inserter(result));
350 bgi::query(spatial_index, bgi::path(linestring, 5) && bgi::intersects(box), std::back_inserter(result));
354 Only one distance predicate (\c nearest() or \c path()) may be used in a query.
358 \param linestring The path along which distance is calculated.
359 \param k The maximum number of values to return.
361 template <typename SegmentOrLinestring> inline
362 detail::predicates::path<SegmentOrLinestring>
363 path(SegmentOrLinestring const& linestring, unsigned k)
365 return detail::predicates::path<SegmentOrLinestring>(linestring, k);
368 #endif // BOOST_GEOMETRY_INDEX_DETAIL_EXPERIMENTAL
370 namespace detail { namespace predicates {
372 // operator! generators
374 template <typename Fun, bool Negated> inline
375 satisfies<Fun, !Negated>
376 operator!(satisfies<Fun, Negated> const& p)
378 return satisfies<Fun, !Negated>(p);
381 template <typename Geometry, typename Tag, bool Negated> inline
382 spatial_predicate<Geometry, Tag, !Negated>
383 operator!(spatial_predicate<Geometry, Tag, Negated> const& p)
385 return spatial_predicate<Geometry, Tag, !Negated>(p.geometry);
388 // operator&& generators
390 template <typename Pred1, typename Pred2> inline
393 boost::tuples::cons<Pred2, boost::tuples::null_type>
395 operator&&(Pred1 const& p1, Pred2 const& p2)
397 /*typedef typename boost::mpl::if_c<is_predicate<Pred1>::value, Pred1, Pred1 const&>::type stored1;
398 typedef typename boost::mpl::if_c<is_predicate<Pred2>::value, Pred2, Pred2 const&>::type stored2;*/
399 namespace bt = boost::tuples;
402 bt::cons< Pred1, bt::cons<Pred2, bt::null_type> >
403 ( p1, bt::cons<Pred2, bt::null_type>(p2, bt::null_type()) );
406 template <typename Head, typename Tail, typename Pred> inline
407 typename tuples::push_back<
408 boost::tuples::cons<Head, Tail>, Pred
410 operator&&(boost::tuples::cons<Head, Tail> const& t, Pred const& p)
412 //typedef typename boost::mpl::if_c<is_predicate<Pred>::value, Pred, Pred const&>::type stored;
413 namespace bt = boost::tuples;
417 bt::cons<Head, Tail>, Pred
421 }} // namespace detail::predicates
423 }}} // namespace boost::geometry::index
425 #endif // BOOST_GEOMETRY_INDEX_PREDICATES_HPP