Boost logo

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