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

后端 未结 2 1353
眼角桃花
眼角桃花 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("<shapefile>")
    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

    0 讨论(0)
  • 2020-12-10 10:21

    Use Rtree (examples) as R-tree index to: (1) index the bounds of the 36k polygons (do this just after reading jsonfile), then (2) very quickly find the intersecting bounding boxes of each polygon to your point of interest. Then, (3) for the intersecting bounding boxes from Rtree, use shapely to use, e.g. point.within(p) to do the actual point-in-polygon analysis. You should see a massive leap of performance with this technique.

    0 讨论(0)
提交回复
热议问题