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
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.
27 Value will be returned by the query if <tt>bg::within(Geometry, Indexable)</tt>
32 bgi::query(spatial_index, bgi::contains(box), std::back_inserter(result));
37 \tparam Geometry The Geometry type.
39 \param g The Geometry object.
41 template <typename Geometry> inline
42 detail::predicates::spatial_predicate<Geometry, detail::predicates::contains_tag, false>
43 contains(Geometry const& g)
45 return detail::predicates::spatial_predicate
48 detail::predicates::contains_tag,
54 \brief Generate \c covered_by() predicate.
56 Generate a predicate defining Value and Geometry relationship.
57 Value will be returned by the query if <tt>bg::covered_by(Indexable, Geometry)</tt>
62 bgi::query(spatial_index, bgi::covered_by(box), std::back_inserter(result));
67 \tparam Geometry The Geometry type.
69 \param g The Geometry object.
71 template <typename Geometry> inline
72 detail::predicates::spatial_predicate<Geometry, detail::predicates::covered_by_tag, false>
73 covered_by(Geometry const& g)
75 return detail::predicates::spatial_predicate
78 detail::predicates::covered_by_tag,
84 \brief Generate \c covers() predicate.
86 Generate a predicate defining Value and Geometry relationship.
87 Value will be returned by the query if <tt>bg::covered_by(Geometry, Indexable)</tt>
92 bgi::query(spatial_index, bgi::covers(box), std::back_inserter(result));
97 \tparam Geometry The Geometry type.
99 \param g The Geometry object.
101 template <typename Geometry> inline
102 detail::predicates::spatial_predicate<Geometry, detail::predicates::covers_tag, false>
103 covers(Geometry const& g)
105 return detail::predicates::spatial_predicate
108 detail::predicates::covers_tag,
114 \brief Generate \c disjoint() predicate.
116 Generate a predicate defining Value and Geometry relationship.
117 Value will be returned by the query if <tt>bg::disjoint(Indexable, Geometry)</tt>
122 bgi::query(spatial_index, bgi::disjoint(box), std::back_inserter(result));
127 \tparam Geometry The Geometry type.
129 \param g The Geometry object.
131 template <typename Geometry> inline
132 detail::predicates::spatial_predicate<Geometry, detail::predicates::disjoint_tag, false>
133 disjoint(Geometry const& g)
135 return detail::predicates::spatial_predicate
138 detail::predicates::disjoint_tag,
144 \brief Generate \c intersects() predicate.
146 Generate a predicate defining Value and Geometry relationship.
147 Value will be returned by the query if <tt>bg::intersects(Indexable, Geometry)</tt>
152 bgi::query(spatial_index, bgi::intersects(box), std::back_inserter(result));
153 bgi::query(spatial_index, bgi::intersects(ring), std::back_inserter(result));
154 bgi::query(spatial_index, bgi::intersects(polygon), std::back_inserter(result));
159 \tparam Geometry The Geometry type.
161 \param g The Geometry object.
163 template <typename Geometry> inline
164 detail::predicates::spatial_predicate<Geometry, detail::predicates::intersects_tag, false>
165 intersects(Geometry const& g)
167 return detail::predicates::spatial_predicate
170 detail::predicates::intersects_tag,
176 \brief Generate \c overlaps() predicate.
178 Generate a predicate defining Value and Geometry relationship.
179 Value will be returned by the query if <tt>bg::overlaps(Indexable, Geometry)</tt>
184 bgi::query(spatial_index, bgi::overlaps(box), std::back_inserter(result));
189 \tparam Geometry The Geometry type.
191 \param g The Geometry object.
193 template <typename Geometry> inline
194 detail::predicates::spatial_predicate<Geometry, detail::predicates::overlaps_tag, false>
195 overlaps(Geometry const& g)
197 return detail::predicates::spatial_predicate
200 detail::predicates::overlaps_tag,
205 #ifdef BOOST_GEOMETRY_INDEX_DETAIL_EXPERIMENTAL
208 \brief Generate \c touches() predicate.
210 Generate a predicate defining Value and Geometry relationship.
211 Value will be returned by the query if <tt>bg::touches(Indexable, Geometry)</tt>
216 \tparam Geometry The Geometry type.
218 \param g The Geometry object.
220 template <typename Geometry> inline
221 detail::predicates::spatial_predicate<Geometry, detail::predicates::touches_tag, false>
222 touches(Geometry const& g)
224 return detail::predicates::spatial_predicate
227 detail::predicates::touches_tag,
232 #endif // BOOST_GEOMETRY_INDEX_DETAIL_EXPERIMENTAL
235 \brief Generate \c within() predicate.
237 Generate a predicate defining Value and Geometry relationship.
238 Value will be returned by the query if <tt>bg::within(Indexable, Geometry)</tt>
243 bgi::query(spatial_index, bgi::within(box), std::back_inserter(result));
248 \tparam Geometry The Geometry type.
250 \param g The Geometry object.
252 template <typename Geometry> inline
253 detail::predicates::spatial_predicate<Geometry, detail::predicates::within_tag, false>
254 within(Geometry const& g)
256 return detail::predicates::spatial_predicate
259 detail::predicates::within_tag,
265 \brief Generate satisfies() predicate.
267 A wrapper around user-defined UnaryPredicate checking if Value should be returned by spatial query.
271 bool is_red(Value const& v) { return v.is_red(); }
274 template <typename Value> bool operator()(Value const& v) { return v.is_red(); }
279 rt.query(index::intersects(box) && index::satisfies(is_red),
280 std::back_inserter(result));
282 rt.query(index::intersects(box) && index::satisfies(is_red_o()),
283 std::back_inserter(result));
285 #ifndef BOOST_NO_CXX11_LAMBDAS
286 rt.query(index::intersects(box) && index::satisfies([](Value const& v) { return v.is_red(); }),
287 std::back_inserter(result));
293 \tparam UnaryPredicate A type of unary predicate function or function object.
295 \param pred The unary predicate function or function object.
297 template <typename UnaryPredicate> inline
298 detail::predicates::satisfies<UnaryPredicate, false>
299 satisfies(UnaryPredicate const& pred)
301 return detail::predicates::satisfies<UnaryPredicate, false>(pred);
305 \brief Generate nearest() predicate.
307 When nearest predicate is passed to the query, k-nearest neighbour search will be performed.
308 \c nearest() predicate takes a \c Geometry from which distances to \c Values are calculated
309 and the maximum number of \c Values that should be returned. Internally
310 boost::geometry::comparable_distance() is used to perform the calculation.
314 bgi::query(spatial_index, bgi::nearest(pt, 5), std::back_inserter(result));
315 bgi::query(spatial_index, bgi::nearest(pt, 5) && bgi::intersects(box), std::back_inserter(result));
316 bgi::query(spatial_index, bgi::nearest(box, 5), std::back_inserter(result));
320 Only one \c nearest() predicate may be used in a query.
324 \param geometry The geometry from which distance is calculated.
325 \param k The maximum number of values to return.
327 template <typename Geometry> inline
328 detail::predicates::nearest<Geometry>
329 nearest(Geometry const& geometry, unsigned k)
331 return detail::predicates::nearest<Geometry>(geometry, k);
334 #ifdef BOOST_GEOMETRY_INDEX_DETAIL_EXPERIMENTAL
337 \brief Generate path() predicate.
339 When path predicate is passed to the query, the returned values are k values along the path closest to
340 its begin. \c path() predicate takes a \c Segment or a \c Linestring defining the path and the maximum
341 number of \c Values that should be returned.
345 bgi::query(spatial_index, bgi::path(segment, 5), std::back_inserter(result));
346 bgi::query(spatial_index, bgi::path(linestring, 5) && bgi::intersects(box), std::back_inserter(result));
350 Only one distance predicate (\c nearest() or \c path()) may be used in a query.
354 \param linestring The path along which distance is calculated.
355 \param k The maximum number of values to return.
357 template <typename SegmentOrLinestring> inline
358 detail::predicates::path<SegmentOrLinestring>
359 path(SegmentOrLinestring const& linestring, unsigned k)
361 return detail::predicates::path<SegmentOrLinestring>(linestring, k);
364 #endif // BOOST_GEOMETRY_INDEX_DETAIL_EXPERIMENTAL
366 namespace detail { namespace predicates {
368 // operator! generators
370 template <typename Fun, bool Negated> inline
371 satisfies<Fun, !Negated>
372 operator!(satisfies<Fun, Negated> const& p)
374 return satisfies<Fun, !Negated>(p);
377 template <typename Geometry, typename Tag, bool Negated> inline
378 spatial_predicate<Geometry, Tag, !Negated>
379 operator!(spatial_predicate<Geometry, Tag, Negated> const& p)
381 return spatial_predicate<Geometry, Tag, !Negated>(p.geometry);
384 // operator&& generators
386 template <typename Pred1, typename Pred2> inline
389 boost::tuples::cons<Pred2, boost::tuples::null_type>
391 operator&&(Pred1 const& p1, Pred2 const& p2)
393 /*typedef typename boost::mpl::if_c<is_predicate<Pred1>::value, Pred1, Pred1 const&>::type stored1;
394 typedef typename boost::mpl::if_c<is_predicate<Pred2>::value, Pred2, Pred2 const&>::type stored2;*/
395 namespace bt = boost::tuples;
398 bt::cons< Pred1, bt::cons<Pred2, bt::null_type> >
399 ( p1, bt::cons<Pred2, bt::null_type>(p2, bt::null_type()) );
402 template <typename Head, typename Tail, typename Pred> inline
403 typename tuples::push_back<
404 boost::tuples::cons<Head, Tail>, Pred
406 operator&&(boost::tuples::cons<Head, Tail> const& t, Pred const& p)
408 //typedef typename boost::mpl::if_c<is_predicate<Pred>::value, Pred, Pred const&>::type stored;
409 namespace bt = boost::tuples;
413 bt::cons<Head, Tail>, Pred
417 }} // namespace detail::predicates
419 }}} // namespace boost::geometry::index
421 #endif // BOOST_GEOMETRY_INDEX_PREDICATES_HPP