i like this post (click again to cancel)
0
i dont like this post (click again to cancel) remove favorite mark from this question (click again to restore mark)

How can I test if a point is inside a certain polygon? How can I test if a lot of points are inside a certain polygon?

flag offensive community wiki
add comment
3 Answers:
i like this answer (click again to cancel)
1
i dont like this answer (click again to cancel)

Use the algorithm described on this site is decades old and well tested.

If you have matplotlib installed, you can use the implementation of this algorithm in matplotlib.nxutils, i.e.

from matplotlib.nxutils import pnpoly

This tests one. For testing an array of points use points_in_poly instead.

See also the matplotlib FAQ.

add comment
i like this answer (click again to cancel)
1
i dont like this answer (click again to cancel)

At

http://mentat.za.net/source/pnpoly.tar.bz2

you'll find pure Python, ctypes and weave wrappers implementing this algorithm:

http://local.wasp.uwa.edu.au/~pbourke/geometry/insidepoly/

(same as in matplotlib, I believe)

Paul Bourke's website is an excellent reference for computational geometry!

permanent link | flag offensive
asked 2009-08-22 12:34:23
Stefan's gravatar image
31
comments (1)
i like this answer (click again to cancel)
0
i dont like this answer (click again to cancel)

The matplotlib way is probably the simplest. If you end up needing something fancier, check out the shapely package. If offers geometric classes such as Point and Polygon. Polygon has a contains method that checks whether or not another geometry is contained within its bounds.

permanent link | flag offensive
asked 2010-03-15 16:06:03
Huard's gravatar image
1
add comment
Your answer:
You are now not logged in but you can answer first and then login
toggle preview

Made with Django.