I refer to this example notebook resource that shows how to identify intersection regions between polygons: https://nbviewer.org/gist/jorisvandenbossche/3a55a16fda9b3c37e0fb48b1d4019e65
Here in this example, with credit going to the author of that example notebook above, we start with 3 circles that happen to all overlap:
We then try to find all of the new intersected polygons made up by these overlapping polygons via this code:
all_intersections = [a.intersection(b) for a, b in list(itertools.combinations(s, 2))] s_all = pd.concat([s, geopandas.GeoSeries(all_intersections)]) polys = list(shapely.ops.polygonize(s_all.boundary.unary_union)) polys = geopandas.GeoSeries(polys) polys.plot(cmap='Set1')
to get this plot of our three overlapping circles with each new intersected polygon identified, and so we now have 7 polygons identified, just what I want:
And then just running
polys I see the geometries for each of these 7 newly intersected polygons printed, great:
Though what I am trying to figure out how to do is how to loop through each unique polygon in the initial GeoDataFrame and identify only the polygon intersections within that specific polygon.
So going with this new example:
For circle A would just get the geometry for circle A itself returned, and then for circle B, I would just get the intersections within circle B returned:
And for circle E, I would get just the intersections within circle E returned:
And so on, repeating this process for each unique circle.
How can I augment my above polygon intersection code to accommodate a
for loop to iterate through each unique circle and only identify the intersected polygons within that specific polygon?
I have tried this:
for index, row in polygons.iterrows(): all_intersections = [a.intersection(b) for a, b in list(itertools.combinations(row['geometry'], 2))] polygons_all = pd.concat([polygons, gpd.GeoSeries(all_intersections)]) polys = list(shapely.ops.polygonize(polygons_all.boundary.unary_union)) polys = gpd.GeoSeries(polys)
but am just getting errors, specifically:
TypeError: 'Polygon' object is not iterable