BaBtis
BaBtis

Reputation: 11

Missing result of GeoPandas.overlay() for polygons that overlap AND touch (how='intersection')

I noticed a weird behaviour of GeoPandas.overlay(): If you create GeoDataFrames from two polygons that overlap AND touch each other at the same time, overlay() returns an empty GeoDataFrame.

from shapely.geometry import Polygon
import geopandas as gpd

foo = Polygon([[0, 0], [0, 3], [2, 3], [2, 1], [1, 2]])
bar = Polygon([[0, 0], [0, 1], [2, 1], [2, 0]])
gdf_foo = gpd.GeoDataFrame([foo], columns=['geometry'], geometry='geometry')
gdf_bar = gpd.GeoDataFrame([bar], columns=['geometry'], geometry='geometry')
overlay_result = gpd.overlay(gdf_foo, gdf_bar)

import matplotlib.pyplot as plt
ax = gdf_foo.plot(color='None', edgecolor='red')
gdf_bar.plot(ax=ax, color='None', edgecolor='blue')
overlay_result.plot(ax=ax, color='green')
plt.show()

As a result overlay_result is empty, even though the polygons overlap: enter image description here

When adjusting the starting polygon foo slightly, gpd.overlay() works as expected:

foo = Polygon([[0, 0], [0, 3], [2, 3], [2, 0], [1, 2]])
# alt.: Polygon([[0, 0], [0, 3], [2, 3], [2, 2], [1, 2]])

Result: enter image description here

Is this behaviour as expected from overlay?

Upvotes: 1

Views: 2422

Answers (1)

martinfleis
martinfleis

Reputation: 7804

geopandas.overlay has a keyword keep_geom_type, which controls which results should be returned. It is by default set to True, which means that it returns only the same geometry type as the original is. In this particular case, the result of the intersection is geometry collection containing Polygon and Point, hence the result is dropped. Setting keep_geom_type=False will return the collection. So this is intended behaviour.

overlay_result = gpd.overlay(gdf_foo, gdf_bar, keep_geom_type=False)

                                            geometry
0  GEOMETRYCOLLECTION (POINT (2.00000 1.00000), P...

Upvotes: 2

Related Questions