Reputation: 33
Code: I am using boost_1_61_0. I am using the geometry part of the library for a GIS application. The idea is to find points within defined regions (a rectangle in this case). This works sometimes but not always. Here is an example where the point should be within the rectangle but isn't...
I have the following test case:
#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;
}
I expect within
to be true
but it is false
. Why is it false
?
Upvotes: 3
Views: 1069
Reputation: 442
On further investigation, see my comments on the other answer, it appears that this is a difference in how boost geometry builds the polygon that you give it.
When we give a point as 0 0 to 180 0 this in boost land actually wraps the world to the west in this case and not the east as I and I'm sure you were expecting.
To prevent this from happening I would suggest inserting an extra point to break up any single vertex that has a longitude component greater than or equal to 180 degrees. This forces boost to plot in the direction you intended rather than the shortest distance.
Upvotes: 1
Reputation: 313
If you don't put the points in clockwise order the boost::geometry::within
may be undefined.
Try boost::geometry::read_wkt( "POLYGON((0 89, 0 0, 180 0, 180 89, 0 89 ))", poly );
instead
Upvotes: 1