 # Geometry :

Subject: [geometry] Order of (clockwise) polygon coordinates significant? Calls to 'within' fail?
From: Bas van Schaik (list-boost-geometry_at_[hidden])
Date: 2014-07-02 10:29:21

Hi all,

I'm struggling with the construction of polygons in boost::geometry,
hopefully someone can shed some light on this.

Consider the following code to create a polygon (defined using 4
coordinates in clockwise order) around the 180 degree meridian, and
subsequently test whether some locations are contained in it (namespace
bg = boost::geometry):

> typedef bg::model::point<double, 2, bg::cs::geographic<bg::degree> >
> point_type;
> bg::model::polygon<point_type> polygon_across_180;
> bg::read_wkt("POLYGON((170 40, -150 40, -150 5, 170 5))",
> polygon_across_180);
>
> // Majuro (west of the 180 degree meridian, but within the polygon)
> double majuro_lon = 171.266667;
> double majuro_lat = 7.066667;
> point_type majuro(majuro_lon, majuro_lat);
>
> // Honolulu, Hawaii (east of the 180 degree meridian, but within the
> polygon)
> double honolulu_lon = -157.816667;
> double honolulu_lat = 21.3;
> point_type honolulu(honolulu_lon, honolulu_lat);
>
> // New Delhi: far west of the polygon
> double newdelhi_lon = 77.208889;
> double newdelhi_lat = 28.613889;
> point_type newdelhi(newdelhi_lon, newdelhi_lat);
>
> assert(bg::within(majuro, polygon_across_180));
> assert(bg::within(honolulu, polygon_across_180));
> assert(!bg::within(newdelhi, polygon_across_180));

The first two assertions are fine, but the third assertion (note the
negation) fails. However, New Delhi is not even close to the polygon.
After altering the order of the polygon coordinates, the assertions are
valid:
> bg::read_wkt("POLYGON((-150 40, -150 5, 170 5, 170 40))",
> polygon_across_180);

Is this behaviour expected, and if so: can someone please explain it to
me (or point me to some documentation)? I have a dataset with a large
number of polygon definitions, but can't get the 'within' method to work
properly.

Many, many thanks!

Bas

Geometry list run by mateusz at loskot.net