Skip to content

Instantly share code, notes, and snippets.

@mjacksonw
Created November 13, 2014 17:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjacksonw/0888709df522ec05fc32 to your computer and use it in GitHub Desktop.
Save mjacksonw/0888709df522ec05fc32 to your computer and use it in GitHub Desktop.
Find out if a point is inside a geographic area
colorado = [(37.0, -102.03),
(37.0, -109.03),
(41.0, -109.03),
(41.0, -102.03)]
denver = (39.44, -104.59)
cheyenne = (41.14, -104.80)
def poly_contains_point(poly, point):
points = len(poly)
inside = False
p1 = poly[0]
for i in range(points+1):
p2 = poly[i % points]
if min(p1[1], p2[1]) < point[1] <= max(p1[1], p2[1]):
if point[0] <= max(p1[0], p2[0]):
if p1[1] != p2[1]:
intercept = (point[1]-p1[1])*(p2[0]-p1[0])/(p2[1]-p1[1])+p1[0]
if p1[0] == p2[0] or point[0] <= intercept:
inside = not inside
p1 = p2
return inside
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment