1

Lets go with the example at the geopandas Documentation. Say we have:

from shapely.geometry import Polygon

polys1 = gpd.GeoSeries([Polygon([(0,0), (2,0), (2,2), (0,2)]),Polygon([(2,2), (4,2), (4,4), (2,4)])])

polys2 = gpd.GeoSeries([Polygon([(1,1), (3,1), (3,3), (1,3)]),Polygon([(3,3), (5,3), (5,5), (3,5)])])

df1 = gpd.GeoDataFrame([[1, 'A'], [2, 'B']], columns=['df1', 'name'], geometry = polys1)
df2 = gpd.GeoDataFrame([[1, 'C'], [2, 'B']], columns=['df2', 'name'], geometry = polys2)

ax = df1.plot(color='red', alpha=0.5);
df1.loc[[1],'geometry'].plot(ax=ax, facecolor='none', edgecolor='black');
df2.plot(ax=ax, color='green', alpha=0.5);
df2.loc[[1],'geometry'].plot(ax=ax, facecolor='none', edgecolor='blue');
ax.annotate('B', xy=(3.4,3.4), size=15, c='yellow');

enter image description here

How would I execute res_symdiff = df1.overlay(df2, how='symmetric_difference') to only perform the operation on the matching name (B) rows? [i.e.: where the yellow B is; only that block would be gone].

1 Answer 1

1

A simple solution is to select the rows of the GeoDataFrames before overlay:

df1 = df1[df1['name'] == 'B']
df2 = df2[df2['name'] == 'B']
result = df1.overlay(df2,how='symmetric_difference')
ax = result.plot(color='red', alpha=0.5)

enter image description here

2
  • Thank you @gene. How would I replace result back into the original df1 at the index of the (originally) selected rows?
    – arkriger
    Jun 29 at 15:09
  • Sorry, but it is another question
    – gene
    Jun 29 at 16:25

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.