I have a geopandas GeoDataFrame
containing hundreds of shapely Polygon
and MultiPolygon
geometries. The polygons overlap in lots of places. I'd like to make a new geometry that contains a count of how many of them overlap. Something like this:
Does anyone have any ideas for how to approach this? I can't even see a way in.
Eventually I'd especially like to be able to weight the polygons, so that some polygons might be worth 2 on their own. Doing this with shapely
's Z field might be nice.
Aside: I'm not particularly tied to any of these libraries, it's just where I've ended up. The coordinates in these geometries are actually pixel coordinates — I'm stumbling towards making a raster to overlay on another image. I'd prefer to keep my footprint as small as possible, since I'd like to be able to deploy this stuff to cloud servers etc, where I might not be able to install random stuff.
Best Answer
May be off topic because it is a postgresql/postgis solution:
In postgres/postgis it is a simple O(N^2) query that may/could be adopted to geopanda.
and defines 5 rectangles:
The intersection request with the table itself:
shows which areas intersecting each other:
Using this basis you can aggregate the counts for each ID object via the group by clausel:
The result shows the wanted pattern.