Ray Casting Multiple Points

6 views
Skip to first unread message

Michael Friedman

unread,
Dec 5, 2017, 9:42:40 PM12/5/17
to Geospatial Python

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 


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))or one Point in Polygon:




Reply all
Reply to author
Forward
0 new messages