Looking for a fast way to find the polygon a point belongs to using Shapely

后端 未结 2 1357
眼角桃花
眼角桃花 2020-12-10 09:25

I have a set of ~36,000 polygons which represent a partition (~counties) of the country. My python script receives a lot of points: pointId, longitude, latitude.

For

2条回答
  •  一向
    一向 (楼主)
    2020-12-10 10:20

    It's great,

    Here is sample code :

    polygons_sf = shapefile.Reader("")
    polygon_shapes = polygons_sf.shapes()
    polygon_points = [q.points for q in polygon_shapes ]
    polygons = [Polygon(q) for q in polygon_points]
    idx = index.Index()
    count = -1
    for q in polygon_shapes:
        count +=1
        idx.insert(count, q.bbox)
    [...]
    for j in idx.intersection([point.x, point.y]):
        if(point.within(polygons[j])):
            geo1, geo2 = polygons_sf.record(j)[0], polygons_sf.record(j)[13]
            break
    

    Thanks

提交回复
热议问题