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
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
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.