I have multiple files that essentially have anywhere from 2000-5000 points each.
I have a polygon file that has about 10000 polygon shapes.
For each file containing the points, I want to get all the polygons that the file intersected.
Checking each polygon for each point and using Contains() takes too long. 2000 x 10000 iterations for each file essentially.
My current approach is to create a polygon around the points then get the intersections of that one bounding polygon to the 10000. This is fast but leads to me picking up intersecting polygons that don't actually contain the points but intersect the bounding polygon I had made around the points. So I have to go back then and do another check for intersections that don't actually contain points.
#go through files containing points and create a bounding polygon around each
for file in point_files:
pts_ds = ogr.Open(file)
pts_lyr = pts_ds.GetLayer()
geom_collection = ogr.Geometry(ogr.wkbGeometryCollection)
for feat in pts_lyr:
geom_collection.AddGeometry(feature.GetGeometryRef())
bounding_polygon = geom_collection.ConvexHull()
#then save bounding polygon to file
#open up each of the bounding polygons I had made and intersect it against the comparison polygons
comparison_polygons_ds = ogr.Open(comparison_polygons.shp)
comparison_polygons_lyr = comparison_polygons.GetLayer()
matches = {}
for file in bounding_polygon_files:
b_polygon_ds = ogr.Open(file)
b_polygon_lyr = b_polygon_ds.GetLayer()
for b_polygon_feat in b_polygon_lyr:
b_polygon_geom = b_polygon_feat.GetGeometryRef()
for comparison_polygons_feat in comparison_polygons_lyr:
comparison_polygon_geom = comparison_polygons_feat.GetGeometryRef()
if b_polygon_geom.Intersects(comparison_polygon_geom):
matches.setdefault(comparison_polygon_geom.GetField(“ID”), []).append(file)
The end result I want is essentially: file1 intersects x,y,z polygons. file2 intersects a,b,c polygons.
How do I achieve that?
I am unfamiliar with GIS and using Python with the OGR package.