From 67edc979a1439c3b9f162e79751d2f2c4e9d2122 Mon Sep 17 00:00:00 2001 From: Adam Wulkiewicz Date: Sun, 24 Feb 2013 03:29:11 +0000 Subject: [PATCH] Added example: index of iterators of a map storing variant geometries, Interprocess example name changed. [SVN r83112] --- doc/html/geometry_index/r_tree.html | 5 +- .../geometry_index/r_tree/rtree_examples.html | 148 +++++++++++++++++- doc/html/index.html | 2 +- doc/imports.qbk | 1 + doc/rtree/examples.qbk | 6 +- doc/src/examples/rtree/Jamfile.v2 | 2 + doc/src/examples/rtree/variants_map.cpp | 143 +++++++++++++++++ 7 files changed, 302 insertions(+), 5 deletions(-) create mode 100644 doc/src/examples/rtree/variants_map.cpp diff --git a/doc/html/geometry_index/r_tree.html b/doc/html/geometry_index/r_tree.html index e4e8c3cec..54adadc83 100644 --- a/doc/html/geometry_index/r_tree.html +++ b/doc/html/geometry_index/r_tree.html @@ -72,7 +72,10 @@ of polygons stored in vector
Index of shared pointers to polygons
-
Interprocess
+
Index + of iterators of a map storing variant geometries
+
Index + stored in shared memory using Boost.Interprocess
Reference
diff --git a/doc/html/geometry_index/r_tree/rtree_examples.html b/doc/html/geometry_index/r_tree/rtree_examples.html index 056fb38a4..1e565f1b6 100644 --- a/doc/html/geometry_index/r_tree/rtree_examples.html +++ b/doc/html/geometry_index/r_tree/rtree_examples.html @@ -33,7 +33,10 @@ of polygons stored in vector
Index of shared pointers to polygons
-
Interprocess
+
Index + of iterators of a map storing variant geometries
+
Index + stored in shared memory using Boost.Interprocess

@@ -284,7 +287,148 @@

+

+

+
#include <boost/geometry.hpp>
+#include <boost/geometry/geometries/point.hpp>
+#include <boost/geometry/geometries/box.hpp>
+#include <boost/geometry/geometries/polygon.hpp>
+#include <boost/geometry/geometries/ring.hpp>
+#include <boost/geometry/geometries/linestring.hpp>
+
+#include <boost/geometry/index/rtree.hpp>
+
+#include <cmath>
+#include <vector>
+#include <map>
+#include <iostream>
+#include <boost/foreach.hpp>
+#include <boost/variant.hpp>
+
+namespace bg = boost::geometry;
+namespace bgi = boost::geometry::index;
+
+typedef bg::model::point<float, 2, bg::cs::cartesian> point;
+typedef bg::model::box<point> box;
+typedef bg::model::polygon<point, false, false> polygon; // ccw, open polygon
+typedef bg::model::ring<point, false, false> ring; // ccw, open ring
+typedef bg::model::linestring<point> linestring;
+typedef boost::variant<polygon, ring, linestring> geometry;
+
+typedef std::map<unsigned, geometry> map;
+typedef std::pair<box, map::iterator> value;
+
+template <class Container>
+void fill(unsigned i, Container & container)
+{
+    for ( float a = 0 ; a < 6.28316f ; a += 1.04720f )
+    {
+        float x = i + int(10*::cos(a))*0.1f;
+        float y = i + int(10*::sin(a))*0.1f;
+        container.push_back(point(x, y));
+    }
+}
+
+struct print_visitor : public boost::static_visitor<>
+{
+    void operator()(polygon const& g) const { std::cout << bg::wkt<polygon>(g) << std::endl; }
+    void operator()(ring const& g) const { std::cout << bg::wkt<ring>(g) << std::endl; }
+    void operator()(linestring const& g) const { std::cout << bg::wkt<linestring>(g) << std::endl; }
+};
+
+struct envelope_visitor : public boost::static_visitor<box>
+{
+    box operator()(polygon const& g) const { return bg::return_envelope<box>(g); }
+    box operator()(ring const& g) const { return bg::return_envelope<box>(g); }
+    box operator()(linestring const& g) const { return bg::return_envelope<box>(g); }
+};
+
+
+int main(void)
+{
+    // geometries container
+    map geometries;
+
+    // create some geometries
+    for ( unsigned i = 0 ; i < 10 ; ++i )
+    {
+        unsigned c = rand() % 3;
+
+        if ( 0 == c )
+        {
+            // create polygon
+            polygon p;
+            fill(i, p.outer());
+            geometries.insert(std::make_pair(i, geometry(p)));
+        }
+        else if ( 1 == c )
+        {
+            // create ring
+            ring r;
+            fill(i, r);
+            geometries.insert(std::make_pair(i, geometry(r)));
+        }
+        else if ( 2 == c )
+        {
+            // create linestring
+            linestring l;
+            fill(i, l);
+            geometries.insert(std::make_pair(i, geometry(l)));
+        }
+    }
+
+    // display geometries
+    std::cout << "generated geometries:" << std::endl;
+    BOOST_FOREACH(map::value_type const& p, geometries)
+        boost::apply_visitor(print_visitor(), p.second);
+
+    // create the rtree using default constructor
+    bgi::rtree< value, bgi::quadratic<16, 4> > rtree;
+
+    // fill the spatial index
+    for ( map::iterator it = geometries.begin() ; it != geometries.end() ; ++it )
+    {
+        // calculate polygon bounding box
+        box b = boost::apply_visitor(envelope_visitor(), it->second);
+        // insert new value
+        rtree.insert(std::make_pair(b, it));
+    }
+
+    // find values intersecting some area defined by a box
+    box query_box(point(0, 0), point(5, 5));
+    std::vector<value> result_s;
+    rtree.query(query_box, std::back_inserter(result_s));
+
+    // find 5 nearest values to a point
+    std::vector<value> result_n;
+    rtree.query(bgi::nearest(point(0, 0), 5), std::back_inserter(result_n));
+
+    // display results
+    std::cout << "spatial query box:" << std::endl;
+    std::cout << bg::wkt<box>(query_box) << std::endl;
+    std::cout << "spatial query result:" << std::endl;
+    BOOST_FOREACH(value const& v, result_s)
+        boost::apply_visitor(print_visitor(), v.second->second);
+
+    std::cout << "knn query point:" << std::endl;
+    std::cout << bg::wkt<point>(point(0, 0)) << std::endl;
+    std::cout << "knn query result:" << std::endl;
+    BOOST_FOREACH(value const& v, result_n)
+        boost::apply_visitor(print_visitor(), v.second->second);
+
+    return 0;
+}
+
+

+

+
+
+

diff --git a/doc/html/index.html b/doc/html/index.html index e1c19784f..dff9d3363 100644 --- a/doc/html/index.html +++ b/doc/html/index.html @@ -51,7 +51,7 @@
- +

Last revised: February 23, 2013 at 23:45:13 GMT

Last revised: February 24, 2013 at 03:27:27 GMT


diff --git a/doc/imports.qbk b/doc/imports.qbk index 68c57c29a..5e384dba0 100644 --- a/doc/imports.qbk +++ b/doc/imports.qbk @@ -11,4 +11,5 @@ [import src/examples/rtree/quick_start.cpp] [import src/examples/rtree/polygons_vector.cpp] [import src/examples/rtree/polygons_shared_ptr.cpp] +[import src/examples/rtree/variants_map.cpp] [import src/examples/rtree/interprocess.cpp] diff --git a/doc/rtree/examples.qbk b/doc/rtree/examples.qbk index 917de425a..f796da212 100644 --- a/doc/rtree/examples.qbk +++ b/doc/rtree/examples.qbk @@ -22,7 +22,11 @@ [rtree_polygons_shared_ptr] [endsect] -[section Interprocess] +[section Index of iterators of a map storing variant geometries] +[rtree_variants_map] +[endsect] + +[section Index stored in shared memory using Boost.Interprocess] [rtree_interprocess] [endsect] diff --git a/doc/src/examples/rtree/Jamfile.v2 b/doc/src/examples/rtree/Jamfile.v2 index dcf90b575..528972cdb 100644 --- a/doc/src/examples/rtree/Jamfile.v2 +++ b/doc/src/examples/rtree/Jamfile.v2 @@ -12,6 +12,8 @@ exe polygons_vector : polygons_vector.cpp ; exe polygons_shared_ptr : polygons_shared_ptr.cpp ; +exe variants_map : variants_map.cpp + ; exe interprocess : interprocess.cpp /boost/thread//boost_thread : diff --git a/doc/src/examples/rtree/variants_map.cpp b/doc/src/examples/rtree/variants_map.cpp new file mode 100644 index 000000000..38fd9aafc --- /dev/null +++ b/doc/src/examples/rtree/variants_map.cpp @@ -0,0 +1,143 @@ +// Boost.Geometry Index +// +// Quickbook Examples +// +// Copyright (c) 2011-2013 Adam Wulkiewicz, Lodz, Poland. +// +// Use, modification and distribution is subject to the Boost Software License, +// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +//[rtree_variants_map + +#include +#include +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include + +namespace bg = boost::geometry; +namespace bgi = boost::geometry::index; + +typedef bg::model::point point; +typedef bg::model::box box; +typedef bg::model::polygon polygon; // ccw, open polygon +typedef bg::model::ring ring; // ccw, open ring +typedef bg::model::linestring linestring; +typedef boost::variant geometry; + +typedef std::map map; +typedef std::pair value; + +template +void fill(unsigned i, Container & container) +{ + for ( float a = 0 ; a < 6.28316f ; a += 1.04720f ) + { + float x = i + int(10*::cos(a))*0.1f; + float y = i + int(10*::sin(a))*0.1f; + container.push_back(point(x, y)); + } +} + +struct print_visitor : public boost::static_visitor<> +{ + void operator()(polygon const& g) const { std::cout << bg::wkt(g) << std::endl; } + void operator()(ring const& g) const { std::cout << bg::wkt(g) << std::endl; } + void operator()(linestring const& g) const { std::cout << bg::wkt(g) << std::endl; } +}; + +struct envelope_visitor : public boost::static_visitor +{ + box operator()(polygon const& g) const { return bg::return_envelope(g); } + box operator()(ring const& g) const { return bg::return_envelope(g); } + box operator()(linestring const& g) const { return bg::return_envelope(g); } +}; + + +int main(void) +{ + // geometries container + map geometries; + + // create some geometries + for ( unsigned i = 0 ; i < 10 ; ++i ) + { + unsigned c = rand() % 3; + + if ( 0 == c ) + { + // create polygon + polygon p; + fill(i, p.outer()); + geometries.insert(std::make_pair(i, geometry(p))); + } + else if ( 1 == c ) + { + // create ring + ring r; + fill(i, r); + geometries.insert(std::make_pair(i, geometry(r))); + } + else if ( 2 == c ) + { + // create linestring + linestring l; + fill(i, l); + geometries.insert(std::make_pair(i, geometry(l))); + } + } + + // display geometries + std::cout << "generated geometries:" << std::endl; + BOOST_FOREACH(map::value_type const& p, geometries) + boost::apply_visitor(print_visitor(), p.second); + + // create the rtree using default constructor + bgi::rtree< value, bgi::quadratic<16, 4> > rtree; + + // fill the spatial index + for ( map::iterator it = geometries.begin() ; it != geometries.end() ; ++it ) + { + // calculate polygon bounding box + box b = boost::apply_visitor(envelope_visitor(), it->second); + // insert new value + rtree.insert(std::make_pair(b, it)); + } + + // find values intersecting some area defined by a box + box query_box(point(0, 0), point(5, 5)); + std::vector result_s; + rtree.query(query_box, std::back_inserter(result_s)); + + // find 5 nearest values to a point + std::vector result_n; + rtree.query(bgi::nearest(point(0, 0), 5), std::back_inserter(result_n)); + + // display results + std::cout << "spatial query box:" << std::endl; + std::cout << bg::wkt(query_box) << std::endl; + std::cout << "spatial query result:" << std::endl; + BOOST_FOREACH(value const& v, result_s) + boost::apply_visitor(print_visitor(), v.second->second); + + std::cout << "knn query point:" << std::endl; + std::cout << bg::wkt(point(0, 0)) << std::endl; + std::cout << "knn query result:" << std::endl; + BOOST_FOREACH(value const& v, result_n) + boost::apply_visitor(print_visitor(), v.second->second); + + return 0; +} + +//] \ No newline at end of file