I was using the difference set operation for a different problem, but I would get an empty geodataframe or None type when replacing a polygon with the difference in my geodataframe. I found it to be a result of having non-intersecting polygons and doing the difference set-operation. I was wondering is this normal behavior or should it still be keeping the geometry for df1? This behavior is different between version 0.10 and version 0.9.
For example:
import geopandas polys1 = geopandas.GeoSeries([Polygon([(0,0), (2,0), (2,2), (0,2)])]) polys2 = geopandas.GeoSeries([Polygon([(3,3), (5,3), (5,5), (3,5)])]) df1 = geopandas.GeoDataFrame({'geometry': polys1, 'df1':[1]}) df2 = geopandas.GeoDataFrame({'geometry': polys2, 'df2':[1]}) ax = df1.plot(color='red'); df2.plot(ax=ax, color='green', alpha=0.5);
Displaying two non intersecting polygons
As you can see these polygons are not intersecting. So when I do the difference set operation,
res_difference = df1.overlay(df2, how='difference') print(res_difference) ax = res_difference.plot(cmap='tab10') df1.plot(ax=ax, facecolor='none', edgecolor='k'); df2.plot(ax=ax, facecolor='none', edgecolor='k');
the output is then this,
where I would expect to see df1 geometry show up in the res_difference variable. Is this how difference is supposed to work?
The reason I would expect df1 geometry to show up in res_difference is based on the behavior in geopandas 0.9. When I run the following code using geopandas 0.9 instead of geopandas 0.10
# I use the function vs method since it seems the method isn't # implemented in geopandas 0.9 res_difference = geopandas.overlay(df1, df2, how='difference') print(res_difference) ax = res_difference.plot(cmap='tab10') df1.plot(ax=ax, facecolor='none', edgecolor='k'); df2.plot(ax=ax, facecolor='none', edgecolor='k');
You get this output
This is the behavior I would expect to see in geopandas 0.10 but if the polygons don’t intersect then you get an empty geodataframe. Is this supposed to happen or is this a result of implementing the spatial indexing?
Advertisement
Answer
This is a regression in a 0.10 release. Sorry about that! We’ll try to get it fixed soon. In the meantime, I would suggest sticking to 0.9.