获取两个多边形的相交区域的坐标(在python中)

时间:2019-09-11 08:46:53

标签: python geometry intersection

假设我有两个多边形,它们的名称和坐标为:

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

在我们的大脑中,很容易知道这两个多边形相交并计算了相交区域的坐标,但是我想让我们的机器知道如何计算相交区域的坐标。基本上,我想知道是否有一个简单明了的算法来完成这项工作,如果已经有一个python库可以做到这一点,那将是完美的。

2 个答案:

答案 0 :(得分:0)

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))

匀称的用户手册:https://shapely.readthedocs.io/en/stable/manual.html

答案 1 :(得分:0)

from turfpy.transformation import intersect
from turfpy.measurement import area
from geojson import Feature
f = Feature(geometry={"coordinates": [
[[-122.801742, 45.48565], [-122.801742, 45.60491],
[-122.584762, 45.60491], [-122.584762, 45.48565],
[-122.801742, 45.48565]]], "type": "Polygon"})
b = Feature(geometry={"coordinates": [
[[-122.520217, 45.535693], [-122.64038, 45.553967],
[-122.720031, 45.526554], [-122.669906, 45.507309],
[-122.723464, 45.446643], [-122.532577, 45.408574],
[-122.487258, 45.477466], [-122.520217, 45.535693]
]], "type": "Polygon"})
inter = intersect([f, b])
area(inter)

您可以使用Turfpy,它是一个包含各种功能的库 Turfpy

相关问题