I have a GeoDataFrame with a column of shapely.polygons. Some of them distinct, some - not:
In [1]: gdf
Out[2]:
geometry
1 POLYGON ((1 1, 1 2, 2 2, 2 1, 1 1))
2 POLYGON ((1 3, 1 4, 2 4, 2 3, 1 3))
3 POLYGON ((1 1, 1 2, 2 2, 2 1, 1 1))
4 POLYGON ((3 1, 3 2, 4 2, 4 1, 3 1))
5 POLYGON ((1 3, 1 4, 2 4, 2 3, 1 3))
I need to find only distinct (non-overlapping) polygons:
In [1]: gdf_distinct
Out[2]:
geometry
1 POLYGON ((1 1, 1 2, 2 2, 2 1, 1 1))
2 POLYGON ((1 3, 1 4, 2 4, 2 3, 1 3))
4 POLYGON ((3 1, 3 2, 4 2, 4 1, 3 1))
As polygons are not hashable, I can not use simple ways in Pandas:
In [1]: gdf_distinct = gdf['geometry'].unique()
TypeError: unhashable type: 'Polygon'
Are there any simple and efficient ways to have a new GeoDataFrame with only distinct polygons?
P.S.:
I found one way, but it works only with fully-duplicate polygons and, as I think, not very efficient:
In [1]: m = []
for index, row in gdf.iterrows():]
if row['geometry'] not in m:
m.append(row['geometry'])
gdf_distinct = GeoDataFrame(geometry=m)