mirror of
https://github.com/boostorg/geometry.git
synced 2026-02-11 11:52:11 +00:00
78 lines
2.1 KiB
C++
78 lines
2.1 KiB
C++
// Boost.Geometry Index
|
|
//
|
|
// Quickbook Examples
|
|
//
|
|
// Copyright (c) 2011-2012 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_quickstart_include
|
|
|
|
#include <boost/geometry.hpp>
|
|
#include <boost/geometry/geometries/point_xy.hpp>
|
|
#include <boost/geometry/geometries/box.hpp>
|
|
|
|
#include <boost/geometry/index/rtree.hpp>
|
|
|
|
// to store queries results
|
|
#include <vector>
|
|
|
|
// just for output
|
|
#include <iostream>
|
|
#include <boost/foreach.hpp>
|
|
|
|
namespace bg = boost::geometry;
|
|
namespace bgi = boost::geometry::index;
|
|
//]
|
|
|
|
int main(void)
|
|
{
|
|
//[rtree_quickstart_valuetype
|
|
typedef bg::model::point<float, 2, bg::cs::cartesian> point;
|
|
typedef bg::model::box<point> box;
|
|
typedef std::pair<box, unsigned> value;
|
|
//]
|
|
|
|
//[rtree_quickstart_create
|
|
// create the rtree using default constructor
|
|
bgi::rtree< value, bgi::quadratic<32, 8> > rtree;
|
|
//]
|
|
|
|
//[rtree_quickstart_insert
|
|
// create some values
|
|
for ( unsigned i = 0 ; i < 10 ; ++i )
|
|
{
|
|
// create a box
|
|
box b(point(i, i), point(i + 0.5f, i + 0.5f));
|
|
// insert new value
|
|
rtree.insert(std::make_pair(b, i));
|
|
}
|
|
//]
|
|
|
|
//[rtree_quickstart_spatial_query
|
|
// find values intersecting some area defined by a box
|
|
box query_box(point(0, 0), point(5, 5));
|
|
std::vector<value> result_s;
|
|
rtree.spatial_query(query_box, std::back_inserter(result_s));
|
|
//]
|
|
|
|
//[rtree_quickstart_nearest_query
|
|
// find 5 nearest values to a point
|
|
std::vector<value> result_n;
|
|
rtree.nearest_query(point(0, 0), 5, std::back_inserter(result_n));
|
|
//]
|
|
|
|
//[rtree_quickstart_output
|
|
std::cout << "spatial query result:" << std::endl;
|
|
BOOST_FOREACH(value const& v, result_s)
|
|
std::cout << bg::wkt<box>(v.first) << " - " << v.second << std::endl;
|
|
std::cout << "knn query result:" << std::endl;
|
|
BOOST_FOREACH(value const& v, result_n)
|
|
std::cout << bg::wkt<box>(v.first) << " - " << v.second << std::endl;
|
|
//]
|
|
|
|
return 0;
|
|
}
|