I am wondering what the correct syntax is for doing a point in polygon search using Shapely and RTree. I'm really interested in speeding the process up using the RTree index but am not having any luck getting it working with the "contains" function. See below.

First. I build my index.

idx = rtree.index.Index()
for feature in world:
    fid = int(feature['properties']['UN'])
    geom = shapely.geometry.asShape(feature['geometry'])
    idx.insert(fid, list(geom.bounds))

Then I check to see if the point is in the polygon using the contains function in Shapely/GEOS.

def point_inside_polygon_world(x, y):
        for feature in world:
            #only work on features that match the country name
            shape = shapely.geometry.asShape(feature['geometry'])
            point = shapely.geometry.Point(float(x),float(y))
            check = shape.contains(point)

How would I check to see if the polygon from the index I created contains the point?

The contains function is not in the RTree docs so I was thinking it could potentially be something that's in there just not documented. Again, I am really looking to speed up the point in polygon lookup.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.