计算给定(x,y)坐标的多边形的面积 [英] Calculate area of polygon given (x,y) coordinates

查看:631
本文介绍了计算给定(x,y)坐标的多边形的面积的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组点,想知道是否有一个函数(为了方便起见,可能是为了提高速度)可以计算出一组点所包围的面积.

I have a set of points and would like to know if there is a function (for the sake of convenience and probably speed) that can calculate the area enclosed by a set of points.

例如:

x = np.arange(0,1,0.001)
y = np.sqrt(1-x**2)

points = zip(x,y)

给定points的面积应近似等于(pi-2)/4.也许从scipy,matplotlib,numpy,shapely等中可以做到这一点?对于x或y坐标,我都不会遇到任何负值...它们将是没有任何定义函数的多边形.

given points the area should be approximately equal to (pi-2)/4. Maybe there is something from scipy, matplotlib, numpy, shapely, etc. to do this? I won't be encountering any negative values for either the x or y coordinates... and they will be polygons without any defined function.

点很可能没有指定的顺序(顺时针或逆时针),并且可能很复杂,因为它们是来自shapefile在一组边界下的一组utm坐标

points will most likely not be in any specified order (clockwise or counterclockwise) and may be quite complex as they are a set of utm coordinates from a shapefile under a set of boundaries

推荐答案

鞋类公式的实现可以在Numpy中完成.假设这些顶点:

Implementation of Shoelace formula could be done in Numpy. Assuming these vertices:

import numpy as np
x = np.arange(0,1,0.001)
y = np.sqrt(1-x**2)

我们可以在numpy中重新定义函数以找到区域:

We can redefine the function in numpy to find the area:

def PolyArea(x,y):
    return 0.5*np.abs(np.dot(x,np.roll(y,1))-np.dot(y,np.roll(x,1)))

并获得结果:

print PolyArea(x,y)
# 0.26353377782163534

避免for循环会使此功能比PolygonArea快50倍:

Avoiding for loop makes this function ~50X faster than PolygonArea:

%timeit PolyArea(x,y)
# 10000 loops, best of 3: 42 µs per loop
%timeit PolygonArea(zip(x,y))
# 100 loops, best of 3: 2.09 ms per loop.

在Jupyter笔记本中完成计时.

Timing is done in Jupyter notebook.

这篇关于计算给定(x,y)坐标的多边形的面积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆