Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

PrevUpHomeNext

Index of iterators of a map storing variant geometries

#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()
{
    // 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(bgi::intersects(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));

    // note: in Boost.Geometry the WKT representation of a box is polygon

    // note: the values store the bounding boxes of geometries
    // the geometries aren't used for querying but are printed

    // 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;
}

Expected results
generated geometries:
POLYGON((1 0,0.4 0.8,-0.5 0.8,-0.9 0,-0.4 -0.8,0.5 -0.8))
POLYGON((2 1,1.4 1.8,0.5 1.8,0.1 1,0.6 0.2,1.5 0.2))
POLYGON((3 2,2.4 2.8,1.5 2.8,1.1 2,1.6 1.2,2.5 1.2,3 2))
POLYGON((4 3,3.4 3.8,2.5 3.8,2.1 3,2.6 2.2,3.5 2.2))
LINESTRING(5 4,4.4 4.8,3.5 4.8,3.1 4,3.6 3.2,4.5 3.2)
POLYGON((6 5,5.4 5.8,4.5 5.8,4.1 5,4.6 4.2,5.5 4.2))
POLYGON((7 6,6.4 6.8,5.5 6.8,5.1 6,5.6 5.2,6.5 5.2))
POLYGON((8 7,7.4 7.8,6.5 7.8,6.1 7,6.6 6.2,7.5 6.2,8 7))
POLYGON((9 8,8.4 8.8,7.5 8.8,7.1 8,7.6 7.2,8.5 7.2,9 8))
POLYGON((10 9,9.4 9.8,8.5 9.8,8.1 9,8.6 8.2,9.5 8.2))
spatial query box:
POLYGON((0 0,0 5,5 5,5 0,0 0))
spatial query result:
POLYGON((1 0,0.4 0.8,-0.5 0.8,-0.9 0,-0.4 -0.8,0.5 -0.8))
POLYGON((2 1,1.4 1.8,0.5 1.8,0.1 1,0.6 0.2,1.5 0.2))
POLYGON((3 2,2.4 2.8,1.5 2.8,1.1 2,1.6 1.2,2.5 1.2,3 2))
POLYGON((4 3,3.4 3.8,2.5 3.8,2.1 3,2.6 2.2,3.5 2.2))
LINESTRING(5 4,4.4 4.8,3.5 4.8,3.1 4,3.6 3.2,4.5 3.2)
POLYGON((6 5,5.4 5.8,4.5 5.8,4.1 5,4.6 4.2,5.5 4.2))
knn query point:
POINT(0 0)
knn query result:
LINESTRING(5 4,4.4 4.8,3.5 4.8,3.1 4,3.6 3.2,4.5 3.2)
POLYGON((4 3,3.4 3.8,2.5 3.8,2.1 3,2.6 2.2,3.5 2.2))
POLYGON((3 2,2.4 2.8,1.5 2.8,1.1 2,1.6 1.2,2.5 1.2,3 2))
POLYGON((1 0,0.4 0.8,-0.5 0.8,-0.9 0,-0.4 -0.8,0.5 -0.8))
POLYGON((2 1,1.4 1.8,0.5 1.8,0.1 1,0.6 0.2,1.5 0.2))

PrevUpHomeNext