TransWikia.com

Self union a geodataframe whilst retaining columns of overlapping features

Geographic Information Systems Asked by Web_GIS on February 26, 2021

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.

gdf_overlap

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)]),
                 Point([3.3,1]).buffer(1.5)]})

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP