I have a geodataframe with overlapping polygon features. I want to mutate this to create a non-overlapping polygon geodataframe. I know I can merge the geometries by using unary_union as shown here: How to self union a GeoDataFrame?.

However this removes all other columns except geometry. I want to retain the columns (in the example below, id and weight). I have to compare each row to all other rows and see if they intersect, if so, use the ‘difference‘ operation to erase the overlapping part. The priority is not important, I just want to delete the overlapping part from one or the other geometry feature. The result should contain no overlapping polygons.

How do I do this somewhat comptutationally efficiently?

See picture. In this case, the squares (1, 2) and the buffered point (4) overlap each other. I would like to apply this for a geodataframe with ~5000 features.


Code sample of the geodataframe used above:

import geopandas
from shapely.geometry import Polygon, Point

my_gdf = geopandas.GeoDataFrame({
    'id': [1, 2, 3, 4],
    'weight': [100, 120, 50, 75],
    'geometry': [Polygon([(0,0), (2,0), (2,2), (0,2)]),
                 Polygon([(2,2), (4,2), (4,4), (2,4)]),
                 Polygon([(4.5,4.5), (6.5,4.5), (6.5,6.5), (4.5,6.5)]),

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.