0

I am trying write a Ray Casting python code, that will return a list of True or False, that will show if points are in polygons. I have a List of that contains Lat/Lons of multiple polygons, and two additional Lists, one being the Latitude, and the other being the Longitude of points. So essentially what I to do is use the Lists, to see if multiple points are in multiple polygons. Any advice would be very helpful, since I am new Python, and learning as I go!.

Below is the function that creates the Ray Casting to see if one point is one polygon, so what would be needed to see if multiple points are in multiple polygons returning a True or False for each point, so returning [True, False, True, False]

Ray Casting for one Point in Polygon:

def point_in_poly(x,y,poly):


    n = len(poly)
    inside = False

    p1x,p1y = poly[0]
    for i in range(n+1):
        p2x,p2y = poly[i % n]
        if y > min(p1y,p2y):
            if y <= max(p1y,p2y):
                if x <= max(p1x,p2x):
                    if p1y != p2y:
                        xints = (y-p1y)*(p2x-p1x)/float(p2y-p1y)+p1x
                    if p1x == p2x or x <= xints:
                        inside = not inside
        p1x,p1y = p2x,p2y

    return inside

## Test

polygon = [(0,11),(11,10),(11,0),(0,1)]


point_x = 12
point_y = 12

## Call the function with the points and the polygon
print (point_in_poly(point_x,point_y,polygon))
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
friedmmj
  • 1
  • 3
  • next time use button `{}` to correctly format code. – furas Dec 06 '17 at 01:23
  • (1) Please describe your algorithm for determining whether the point is inside the polygon. (2) You ask for multiple points and multiple polygons; please show appropriate examples of the results you want. (3) Where are you having trouble expanding this to multiples? You know how to use loops and lists, so I'm not sure where you're stuck. – Prune Dec 06 '17 at 01:28
  • I'm asking about the algorithm, because your use of many single-letter abbreviations for your variables, makes the code difficult to read as a whole. – Prune Dec 06 '17 at 01:30

0 Answers0