bg::cs::geographic and nearest query

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

bg::cs::geographic and nearest query

Boost - Users mailing list
Hi all!

I need to store geographic points in rtree and be able to answer nearest
query.

Can someone provide an example similar to  this
<https://www.boost.org/doc/libs/1_65_1/libs/geometry/doc/html/geometry/spatial_indexes/rtree_examples/quick_start.html>  
but with point type bg::model::point<float, 2,
bg::cs::geographic&lt;bg::degree>>? Is that supported? For me it fails to
compile.

--
Kind regards, Sergey.



--
Sent from: http://boost.2283326.n4.nabble.com/Boost-Users-f2553780.html
_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: bg::cs::geographic and nearest query

Boost - Users mailing list
Hi,

W dniu 14.10.2019 o 16:57, vexakul via Boost-users pisze:

> Hi all!
>
> I need to store geographic points in rtree and be able to answer nearest
> query.
>
> Can someone provide an example similar to  this
> <https://www.boost.org/doc/libs/1_65_1/libs/geometry/doc/html/geometry/spatial_indexes/rtree_examples/quick_start.html>
> but with point type bg::model::point<float, 2,
> bg::cs::geographic&lt;bg::degree>>? Is that supported? For me it fails to
> compile.
When you define the geographic point as R-tree element type the
computation should be done in geographic CS by default using WGS84
ellipsoid and andoyer formula to calculate distances. So simply:

using boost::geometry;

using point = model::point<double, 2, cs::geographic<degree>>;

using rtree = index::rtree<point, index::rstar<4>>;

rtree rt;

// or

std::vector<point> pts;

rtree rt2{pts.begin(), pts.end()};

If you need different ellipsoid or more accurate formula it is also
possible (since Boost 1.71) though it is not documented. It's because
I'm not entirely sure what to do with the fact that with this approach
the EqualTo is called with two or three arguments depending on the
parameters passed into the R-tree so it may be confusing. So there is
slight probability that the interface will change in case I found some
better way of defining CS-specific parts of the computation. So if you
e.g. need to pass a different ellipsoid you can wrap the R-tree
parameters together with Index strategy and pass it into the R-tree like
that:

using boost::geometry;

using formula = strategy::andoyer;// or more accurate strategy::vincenty
srs::spheroid<double> sph(6378137.0, 6356752.3142451793);

using point = model::point<double, 2, cs::geographic<degree>>;
using parameters = index::parameters
     <
         index::rstar<4>,
         strategy::index::geographic<formula>
     >;

using rtree = index::rtree<point, parameters>;

rtree rt{parameters{index::rstar<4>{},
strategy::index::geographic<formula>{sph}}};

// or

std::vector<point> pts;

rtree rt2{pts.begin(), pts.end(),
parameters{index::rstar<4>{},
strategy::index::geographic<formula>{sph}}};

Have in mind that I didn't test the code above so there may be some
mistakes.

Adam
_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: bg::cs::geographic and nearest query

Boost - Users mailing list
The tree itself compiles just fine. It's the nearest query that fails. Here is a minimal not working example. I'll send output as an attachment. What bothers me is Strategy=int in the output.

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point.hpp>
#include <boost/geometry/geometries/box.hpp>

#include <boost/geometry/index/rtree.hpp>

// to store queries results
#include <vector>

namespace bg = boost::geometry;
namespace bgi = boost::geometry::index;

int foobar()
{
    typedef bg::model::point<float, 2, bg::cs::geographic<bg::degree>> point;
    typedef bg::model::box<point> box;
    typedef std::pair<box, unsigned> value;

    // create the rtree using default constructor
    bgi::rtree< value, bgi::quadratic<16> > rtree;

    // create some values
    for(unsigned i = 0; i < 10; ++i)
    {
        // create a box
        box b(point(i + 0.0f, i + 0.0f), point(i + 0.5f, i + 0.5f));
        // insert new value
        rtree.insert(std::make_pair(b, i));
    }

    std::vector<value> result_n;
    rtree.query(bgi::nearest(point(0., 0.), 5), std::back_inserter(result_n));

    return 0;
}


14.10.2019, 20:23, "Adam Wulkiewicz" <[hidden email]>:

> Hi,
>
> W dniu 14.10.2019 o 16:57, vexakul via Boost-users pisze:
>>  Hi all!
>>
>>  I need to store geographic points in rtree and be able to answer nearest
>>  query.
>>
>>  Can someone provide an example similar to this
>>  <https://www.boost.org/doc/libs/1_65_1/libs/geometry/doc/html/geometry/spatial_indexes/rtree_examples/quick_start.html>
>>  but with point type bg::model::point<float, 2,
>>  bg::cs::geographic&lt;bg::degree>>? Is that supported? For me it fails to
>>  compile.
>
> When you define the geographic point as R-tree element type the
> computation should be done in geographic CS by default using WGS84
> ellipsoid and andoyer formula to calculate distances. So simply:
>
> using boost::geometry;
>
> using point = model::point<double, 2, cs::geographic<degree>>;
>
> using rtree = index::rtree<point, index::rstar<4>>;
>
> rtree rt;
>
> // or
>
> std::vector<point> pts;
>
> rtree rt2{pts.begin(), pts.end()};
>
> If you need different ellipsoid or more accurate formula it is also
> possible (since Boost 1.71) though it is not documented. It's because
> I'm not entirely sure what to do with the fact that with this approach
> the EqualTo is called with two or three arguments depending on the
> parameters passed into the R-tree so it may be confusing. So there is
> slight probability that the interface will change in case I found some
> better way of defining CS-specific parts of the computation. So if you
> e.g. need to pass a different ellipsoid you can wrap the R-tree
> parameters together with Index strategy and pass it into the R-tree like
> that:
>
> using boost::geometry;
>
> using formula = strategy::andoyer;// or more accurate strategy::vincenty
> srs::spheroid<double> sph(6378137.0, 6356752.3142451793);
>
> using point = model::point<double, 2, cs::geographic<degree>>;
> using parameters = index::parameters
>      <
>          index::rstar<4>,
>          strategy::index::geographic<formula>
>      >;
>
> using rtree = index::rtree<point, parameters>;
>
> rtree rt{parameters{index::rstar<4>{},
> strategy::index::geographic<formula>{sph}}};
>
> // or
>
> std::vector<point> pts;
>
> rtree rt2{pts.begin(), pts.end(),
> parameters{index::rstar<4>{},
> strategy::index::geographic<formula>{sph}}};
>
> Have in mind that I didn't test the code above so there may be some
> mistakes.
>
> Adam
_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users

log.txt (30K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: bg::cs::geographic and nearest query

Boost - Users mailing list
W dniu 14.10.2019 o 20:21, Sergey Lukashev pisze:
> The tree itself compiles just fine. It's the nearest query that fails. Here is a minimal not working example. I'll send output as an attachment. What bothers me is Strategy=int in the output.
>
This error message means that the geographic strategy for calculating
the distance between point and box is not implemented or at least
bg::strategy::distance::services::default_strategy (a utility used to
get the type of default strategy for distance algorithm) for point and
box is not specialized.

This code compiles for me with boost-1.67.0 and newer for sure, maybe
with 1.65 and/or 1.66. I get the same message as you with 1.64.0. Which
version are you testing?

Adam

_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: bg::cs::geographic and nearest query

Boost - Users mailing list
1.65.1

14.10.2019, 23:10, "Adam Wulkiewicz" <[hidden email]>:

> W dniu 14.10.2019 o 20:21, Sergey Lukashev pisze:
>>  The tree itself compiles just fine. It's the nearest query that fails. Here is a minimal not working example. I'll send output as an attachment. What bothers me is Strategy=int in the output.
>
> This error message means that the geographic strategy for calculating
> the distance between point and box is not implemented or at least
> bg::strategy::distance::services::default_strategy (a utility used to
> get the type of default strategy for distance algorithm) for point and
> box is not specialized.
>
> This code compiles for me with boost-1.67.0 and newer for sure, maybe
> with 1.65 and/or 1.66. I get the same message as you with 1.64.0. Which
> version are you testing?
>
> Adam
_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: bg::cs::geographic and nearest query

Boost - Users mailing list
W dniu 14.10.2019 o 22:19, Sergey Lukashev pisze:
> 1.65.1
Right, could you check at least 1.67.0 or preferably the latest one
(1.71.0)?

Adam
_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: bg::cs::geographic and nearest query

Boost - Users mailing list
I will but I'm afraid I cannot update Boost right now. Could you come up with a workaround for this version to define the strategy?

--
Kind regards, Sergey.

14.10.2019, 23:24, "Adam Wulkiewicz" <[hidden email]>:
> W dniu 14.10.2019 o 22:19, Sergey Lukashev pisze:
>>  1.65.1
>
> Right, could you check at least 1.67.0 or preferably the latest one
> (1.71.0)?
>
> Adam
_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: bg::cs::geographic and nearest query

Boost - Users mailing list
W dniu 14.10.2019 o 22:58, Sergey Lukashev pisze:
> I will but I'm afraid I cannot update Boost right now. Could you come up with a workaround for this version to define the strategy?
Not really. The easiest would be to update Boost to newer version.

You could get the files from the newer version of Boost:
https://github.com/boostorg/geometry/blob/develop/include/boost/geometry/strategies/geographic/distance_cross_track_box_box.hpp
https://github.com/boostorg/geometry/blob/develop/include/boost/geometry/strategies/geographic/distance_cross_track_point_box.hpp
together with all other files that are needed by these two.
I can't guarantee that it will work. You'd have to take care about it by
yourself. And seeing how many files were changed in the pull request
adding these strategies it wouldn't be trivial:
https://github.com/boostorg/geometry/pull/438/files
What makes it even harder is that it may depend on other changes made
before and after it. But I guess you could try to traverse the header
files and copy them from the newer version to the point when the code
starts to compile and the program calculates correct results. But it is
not guaranteed that the library will work this way.

Using newer Boost.Geometry with the older rest of Boost is not an option
because some libraries we depend on changed around 1.65/1.66 so you'd
have to update them too.

Approximating the computation with spherical CS is also not an option
because I think the spherical strategies were added as well at this
point. You can check it out by replacing cs::geographic with
cs::spherical_equatorial.

Adam
_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users