[英]Python: efficient way to measure region properties using shapely
First of all, I apologize to post this easy question. 首先,我很抱歉张贴这个简单的问题。 I have a polygon
我有一个多边形
from shapely.geometry import Polygon
polygon = Polygon([(560023.4495758876400000 6362057.3904932579000000),(560023.4495758876400000 6362060.3904932579000000),(560024.4495758876400000 6362063.3904932579000000),(560026.9495758876400000 6362068.3904932579000000),(560028.4495758876400000 6362069.8904932579000000),(560034.9495758876400000 6362071.8904932579000000),(560036.4495758876400000 6362071.8904932579000000),(560037.4495758876400000 6362070.3904932579000000),(560037.4495758876400000 6362064.8904932579000000),(560036.4495758876400000 6362063.3904932579000000),(560034.9495758876400000 6362061.3904932579000000),(560026.9495758876400000 6362057.8904932579000000),(560025.4495758876400000 6362057.3904932579000000),(560023.4495758876400000 6362057.3904932579000000)])
My goal is compute the minor and the major axis of this polygon, following the Figure example: 我的目标是按照下图示例计算该多边形的短轴和长轴 :
I find this example in scikit-image but before to use a second module I wish to ask if there is in shapely module a method to calculate these indices. 我在scikit-image中找到了这个示例,但是在使用第二个模块之前,我想问一下匀称模块中是否存在一种计算这些指数的方法。
thanks in advance 提前致谢
This question is a bit old but I ran into this myself recently, here's what I did: 这个问题有点老了,但我最近遇到了这个问题,这是我做的:
from shapely.geometry import Polygon, LineString
polygon = Polygon([(560023.4495758876400000, 6362057.3904932579000000),(560023.4495758876400000, 6362060.3904932579000000),(560024.4495758876400000, 6362063.3904932579000000),(560026.9495758876400000, 6362068.3904932579000000),(560028.4495758876400000, 6362069.8904932579000000),(560034.9495758876400000, 6362071.8904932579000000),(560036.4495758876400000, 6362071.8904932579000000),(560037.4495758876400000, 6362070.3904932579000000),(560037.4495758876400000, 6362064.8904932579000000),(560036.4495758876400000, 6362063.3904932579000000),(560034.9495758876400000, 6362061.3904932579000000),(560026.9495758876400000, 6362057.8904932579000000),(560025.4495758876400000, 6362057.3904932579000000),(560023.4495758876400000, 6362057.3904932579000000)])
# get the minimum bounding rectangle and zip coordinates into a list of point-tuples
mbr_points = list(zip(*polygon.minimum_rotated_rectangle.exterior.coords.xy))
# calculate the length of each side of the minimum bounding rectangle
mbr_lengths = [LineString((mbr_points[i], mbr_points[i+1])).length for i in range(len(mbr_points) - 1)]
# get major/minor axis measurements
minor_axis = min(mbr_lengths)
major_axis = max(mbr_lengths)
Shapely makes it easy to compute the mbr via minimum_rotated_rectangle
, but it doesn't appear that the opposite sides are of exact equal length. Shapely使通过
minimum_rotated_rectangle
轻松计算mbr变得容易,但是似乎相对的边没有完全相等的长度。 Because of this, the above calculates the length of each side, then takes the min/max. 因此,上面的方法计算了每侧的长度,然后取最小值/最大值。
First calculate the Minimum Bounding Rectangle of the polygon - see the process described in How to find the minimum-area-rectangle for given points? 首先计算多边形的最小边界矩形-参见如何找到给定点的最小面积矩形中描述的过程? , except you will start with the convex hull.
,除了您将从凸包开始。 In Shapely, use the
.convex_hull()
method to calculate the convex hull of your polygon. 在Shapely中,使用
.convex_hull()
方法来计算多边形的凸包。
Then once you have the MBR, you can find the major/minor axes. 然后,一旦有了MBR,就可以找到主轴/副轴。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.