Get the coordinates of two polygon's intersection area (in Python)

11,604
from shapely.geometry import Polygon

p = Polygon([(1,1),(2,2),(4,2),(3,1)])
q = Polygon([(1.5,2),(3,5),(5,4),(3.5,1)])
print(p.intersects(q))  # True
print(p.intersection(q).area)  # 1.0
x = p.intersection(q)
print(x) #POLYGON ((1.833333333333333 1.833333333333333, 2 2, 4 2, 3.166666666666667 1.166666666666667, 1.833333333333333 1.833333333333333))

shapely user manual: https://shapely.readthedocs.io/en/stable/manual.html

Share:
11,604
Zhou XF
Author by

Zhou XF

Just a investigator

Updated on August 01, 2022

Comments

  • Zhou XF
    Zhou XF over 1 year

    Say, if I have two polygons, their name and coordinates are (in Python):

    p:[(1,1),(2,2),(4,2),(3,1)]
    q:[(1.5,2),(3,5),(5,4),(3.5,1)]
    

    In our human brain, it is easy to know that these two polygons are intersected and calculate the intersection area coordinates, but I want to let our machine know how to calculate the intersection area's coordinates. Basically, I want to know if there is a simple and clear algorithm for this job, if there is already a Python library could do this, it will be perfect.