Created
November 13, 2014 17:56
-
-
Save mjacksonw/0888709df522ec05fc32 to your computer and use it in GitHub Desktop.
Find out if a point is inside a geographic area
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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