|
Geometry : |
Subject: Re: [geometry] Boost Geometry Index to optimize computing distance point to ring
From: Adam Wulkiewicz (adam.wulkiewicz_at_[hidden])
Date: 2019-01-09 01:23:39
Matthieu Beghin Via Geometry wrote:
> So I could use comparable_distance after converting my rings to
> linestring or multi linestring ? Would the R-tree of the linestirng be
> kept between each call ? (in case I can't do all in a single call)
No, the R-tree is created internally and destroyed afterwards so if you
want to keep it you have to create the rtree by yourself.
Would this be enough as an example?
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/geometries.hpp>
#include <boost/geometry/index/rtree.hpp>
#include <iostream>
#include <vector>
int main()
{
   namespace bg = boost::geometry;
   namespace bgi = boost::geometry::index;
   typedef bg::model::point<double, 2, bg::cs::cartesian> point;
   typedef bg::model::polygon<point> polygon;
   point p{ 0, 0 };
   // create some polygon and fill it with data
   polygon poly;
   double a = 0;
   double as = bg::math::two_pi<double>() / 100;
   for (int i = 0; i < 100; ++i, a += as)
   {
       double c = cos(a);
       double s = sin(a);
       poly.outer().push_back(point{10 * c, 10 * s});
       poly.inners().resize(1);
       poly.inners()[0].push_back(point{5 * c, 5 * s});
   }
   // make sure it is valid
   bg::correct(poly);
   // create rtree containing objects of type bg::model::pointing_segment
   typedef bg::segment_iterator<polygon const> segment_iterator;
   typedef std::iterator_traits<segment_iterator>::value_type
segment_type;
   bgi::rtree<segment_type, bgi::rstar<4> >
rtree(bg::segments_begin(poly),
bg::segments_end(poly));
   // get 1 nearest segment
   std::vector<segment_type> result;
   rtree.query(bgi::nearest(p, 1), std::back_inserter(result));
   BOOST_ASSERT(!result.empty());
   std::cout << bg::wkt(result[0]) << ", " << bg::distance(p,
result[0]) << std::endl;
   return 0;
}
For me the result is:
   LINESTRING(-2.40877 4.38153,-2.67913 4.22164), 4.99753
I keep all of the segments of a polygon in the rtree but you can keep
segments of whatever rings you like. The value_type of the rtree is
pointing_segment returned by segment_iterator. This is a segment type
holding pointers to the original points in the polygon. You may store
whatever segments you like. It's only for convenience. This way I'm able
to use the values directly returned by segment_iterator.
Adam
Geometry list run by mateusz at loskot.net