如何使用python从点列表中计算多边形的面积? [英] How to calculate area of polygon from list of points with python?
问题描述
我有一个2D点列表/数组,这些点/数组形成非凸 非自相交形状. 我想计算该多边形包围的面积.
I have a list/array of 2D points which form a non-convex non-self-intersecting shape. I want to calculate the area enclosed by that polygon.
首先,我需要形成一个多边形,并以曲线行进"(非自相交)的方式提供点.如果我可以对该多边形进行三角测量,则可以计算出面积(我可以忍受很小的相对误差).
First I need to form a polygon with the points that I get provided as a 'curve-walk' (non self intersecting). If I can triangulate that polygon I can calculate the area ( I can tolerate a small relative error).
显然,使用scipy的scipy.spatial.ConvexHull()
会导致错误的形状和区域,但是我没有在大型数学软件包中找到可用的算法来做到这一点.
Using scipy's scipy.spatial.ConvexHull()
results in the wrong shape and area , obviously, but I have not found a available algorithm in the big math packages which does this.
谁能告诉我该怎么做?
推荐答案
使用适用于Python 2.7和3的整形模块
use the shapely module available for both Python 2.7 and 3
In [41]: from shapely.geometry import Polygon
In [48]: coords = ((-1, 0), (-1, 1), (0, 0.5), (1, 1), (1, 0), (-1, 0))
In [49]: polygon = Polygon(coords)
In [50]: polygon.area
Out[50]: 1.5
这篇关于如何使用python从点列表中计算多边形的面积?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!