matplotlib.nxutils.pnpoly returns True for some vertexes, and False for others

Hello,

I tested the following code on my Mac laptop and our production Linux server both running matplotlib V1.0.1. Both machines observe the same output from the code, so I was wondering if somebody is aware of the problem or if it’s some undocumented feature of “pnpoly()” function from matplotlib.nxutils?

I use matplotlib.nxutils.pnpoly() function from matplotlib to determine if point belongs to the polygon.

The following code:

import numpy as np

import matplotlib.nxutils as nx

coords = np.array([[4.0, 1.0], [4.0, 4.0], [5.0, 5.0], [6.0, 4.0], [5.0, 0.0]])

nx.pnpoly(4.0, 1.0, coords)

1

nx.pnpoly(4.0, 4.0, coords)

1

nx.pnpoly(5.0, 5.0, coords)

0

nx.pnpoly(6.0, 4.0, coords)

0

nx.pnpoly(5.0, 0.0, coords)

0

The question is why first two vertexes are considered to be inside of defined polygon, and last 3 vertexes are not? My guess, it’s treating the polygon as a semi-open set, and I wonder if it can be changed to make all vertexes inclusive?

Any help would be greatly appreciated.

Many thanks,

Masha

···

liukis@…4271…

The documentation for pnpoly() for that version states:

“”"
A point on the boundary may be treated as inside or outside.

    See `pnpoly <[http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html](http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html)>`_

“”"

Note that in version 1.2.0, the nxutils module was deprecated. pnpoly() and points_inside_poly() are now merely wrappers around the polygon’s implementations of point-testing, which differs from the nxutils’ implementation, so you may get slightly different results.

Do note that the point-testing algorithm in matplotlib was more geared for visualization purposes rather than for strict geometric needs. If you need a more well-behaved point-tester (and faster if the polygon is “prepared”), use the shapely package instead.

I hope that clears things up!

Cheers!
Ben Root

···

On Tue, Jan 29, 2013 at 5:55 PM, Maria Liukis <liukis@…1887…> wrote:

Hello,

I tested the following code on my Mac laptop and our production Linux server both running matplotlib V1.0.1. Both machines observe the same output from the code, so I was wondering if somebody is aware of the problem or if it’s some undocumented feature of “pnpoly()” function from matplotlib.nxutils?

I use matplotlib.nxutils.pnpoly() function from matplotlib to determine if point belongs to the polygon.

The following code:

import numpy as np

import matplotlib.nxutils as nx

coords = np.array([[4.0, 1.0], [4.0, 4.0], [5.0, 5.0], [6.0, 4.0], [5.0, 0.0]])

nx.pnpoly(4.0, 1.0, coords)

1

nx.pnpoly(4.0, 4.0, coords)

1

nx.pnpoly(5.0, 5.0, coords)

0

nx.pnpoly(6.0, 4.0, coords)

0

nx.pnpoly(5.0, 0.0, coords)

0

The question is why first two vertexes are considered to be inside of defined polygon, and last 3 vertexes are not? My guess, it’s treating the polygon as a semi-open set, and I wonder if it can be changed to make all vertexes inclusive?

Any help would be greatly appreciated.

Many thanks,

Masha

Ben,

Many thanks! Will try to use shapely package then.

Masha

···

liukis@…1887…

On Jan 30, 2013, at 6:59 AM, Benjamin Root wrote:

On Tue, Jan 29, 2013 at 5:55 PM, Maria Liukis <liukis@…1887…> wrote:

Hello,

I tested the following code on my Mac laptop and our production Linux server both running matplotlib V1.0.1. Both machines observe the same output from the code, so I was wondering if somebody is aware of the problem or if it’s some undocumented feature of “pnpoly()” function from matplotlib.nxutils?

I use matplotlib.nxutils.pnpoly() function from matplotlib to determine if point belongs to the polygon.

The following code:

import numpy as np

import matplotlib.nxutils as nx

coords = np.array([[4.0, 1.0], [4.0, 4.0], [5.0, 5.0], [6.0, 4.0], [5.0, 0.0]])

nx.pnpoly(4.0, 1.0, coords)

1

nx.pnpoly(4.0, 4.0, coords)

1

nx.pnpoly(5.0, 5.0, coords)

0

nx.pnpoly(6.0, 4.0, coords)

0

nx.pnpoly(5.0, 0.0, coords)

0

The question is why first two vertexes are considered to be inside of defined polygon, and last 3 vertexes are not? My guess, it’s treating the polygon as a semi-open set, and I wonder if it can be changed to make all vertexes inclusive?

Any help would be greatly appreciated.

Many thanks,

Masha

The documentation for pnpoly() for that version states:

“”"
A point on the boundary may be treated as inside or outside.

    See `pnpoly <[http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html](http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html)>`_

“”"

Note that in version 1.2.0, the nxutils module was deprecated. pnpoly() and points_inside_poly() are now merely wrappers around the polygon’s implementations of point-testing, which differs from the nxutils’ implementation, so you may get slightly different results.

Do note that the point-testing algorithm in matplotlib was more geared for visualization purposes rather than for strict geometric needs. If you need a more well-behaved point-tester (and faster if the polygon is “prepared”), use the shapely package instead.

I hope that clears things up!

Cheers!
Ben Root