JorgeDeJesus

Untitled

Aug 4th, 2013
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.35 KB | None | 0 0
  1. def point_in_poly(x,y,poly):
  2.    # check if point is a vertex
  3.    if (x,y) in poly: return True
  4.    # check if point is on a boundary
  5.    for i in range(len(poly)):
  6.       p1 = None
  7.       p2 = None
  8.       if i==0:
  9.          p1 = poly[0]
  10.          p2 = poly[1]
  11.       else:
  12.          p1 = poly[i-1]
  13.          p2 = poly[i]
  14.       if p1[1] == p2[1] and p1[1] == y and \
  15.       x > min(p1[0], p2[0]) and x < max(p1[0], p2[0]):
  16.          return True      
  17.    n = len(poly)
  18.    inside = False
  19.    p1x,p1y = poly[0]
  20.    for i in range(n+1):
  21.       p2x,p2y = poly[i % n]
  22.       if y > min(p1y,p2y):
  23.          if y <= max(p1y,p2y):
  24.             if x <= max(p1x,p2x):
  25.                if p1y != p2y:
  26.                   xints = (y-p1y)*(p2x-p1x)/(p2y-p1y)+p1x
  27.                if p1x == p2x or x <= xints:
  28.                   inside = not inside
  29.       p1x,p1y = p2x,p2y
  30.    if inside: return True
  31.    else: return False
  32. myPolygon = [(-33.416032,-70.593016), (-33.415370,-70.589604),(-33.417340,-70.589046), (-33.417949,-70.592351),(-33.416032,-70.593016)]
  33. lat = -33.416032
  34. lon = -70.593016
  35. print point_in_poly(lon, lat, myPolygon) #--> False
  36. lat = -33.416744
  37. lon = -70.590699  
  38. print point_in_poly(lon, lat, myPolygon) # --> False Point in the center of polygon
  39. lat = -33.414161
  40. lon = -70.593932  
  41. print point_in_poly(lon, lat, myPolygon) # --> False Point outside polygon --> False
Advertisement
Add Comment
Please, Sign In to add comment