对于地理坐标系中的某个点,boost :: geometry :: within失败 - 为什么?

问题描述 投票:3回答:2

代码:我正在使用boost_1_61_0。我正在使用库的几何部分来构建GIS应用程序。我们的想法是在定义的区域内找到点(在这种情况下是一个矩形)。这有时有效,但并非总是如此。这是一个例子,这个点应该在矩形内,但不是......

我有以下测试用例:

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/multi/geometries/multi_polygon.hpp>
#include <boost/geometry/geometries/register/point.hpp>
#include <iostream>
#include <boost/geometry/io/wkt/wkt.hpp>

class wxPoint
{
public :
    double  getx() const
    {
        return m_x;
    }
    double gety() const
    {
        return m_y;
    }
    void setx( double in)
    {
        m_x = in;
    }
    void sety(double in)
    {
        m_y = in;
    }
private:
    double m_x;
    double m_y;
};

BOOST_GEOMETRY_REGISTER_POINT_2D_GET_SET(
    wxPoint,
    double,
    boost::geometry::cs::geographic<boost::geometry::degree>,
    wxPoint::getx,
    wxPoint::gety,
    wxPoint::setx,
    wxPoint::sety )

int main()
{
    boost::geometry::model::polygon< wxPoint > poly;

    boost::geometry::read_wkt( "POLYGON((0 89, 180 89, 180 0, 0 0, 0 89 ))", poly );  

    wxPoint point;
    point.setx( 150 );
    point.sety( 88 );

    bool within = boost::geometry::within( point, poly );


    return 0;
}

我希望withintrue,但它是false。为什么是false

c++ boost boost-geometry
2个回答
1
投票

在进一步研究中,看到我对另一个答案的评论,看起来这与增强几何构建你给它的多边形的方式不同。

当我们给出一个0 0到180 0的点时,在这个案例中,在这个案例中实际上将世界包裹在西方,而不是东方,因为我和我确定你在期待。

为了防止这种情况发生,我建议插入一个额外的点来分解任何具有大于或等于180度的经度分量的单个顶点。这会强制提升到你想要的方向,而不是最短的距离。


1
投票

如果您不按顺时针顺序放置点,则boost::geometry::within可能未定义。

试试boost::geometry::read_wkt( "POLYGON((0 89, 0 0, 180 0, 180 89, 0 89 ))", poly );

© www.soinside.com 2019 - 2024. All rights reserved.