[GIS] Python shapely difference between two polygons

geometrypythonpython-2.7shapefileshapely

I have a geometry question using shapely,Fiona and polygons shapefiles with big data(over 700000 features any polygon shapefile).

polygon1.difference(polygon2)

I want to find the difference between two polygons using shapely (or maybe geopandas?) and I have read this post, I want the difference calculate only in the polygons segments(lines) and not the polygon geometry.

for example if I use code from the post with add more lines the final export difference is a polygon layer,I want the line layer to show me the differences between two polygons.

task

with the light blue line is the difference.

that because I think is more fast the difference calculator to create lines from polygons.

Best Answer

Once you've calculated the difference between your polygons (let's call this delta), take the difference between the exterior of delta and the exterior of your brown polygon2: result = delta.exterior.difference(polygon2.exterior). Note that the sense of the difference will be significant and it may be tricky to generalize this to all possible polygons. Good luck!