首先,我很抱歉发布这个简单的问题。我有一个多边形
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)])
我的目标是按照图示例计算此多边形的次要轴和长轴:
我在scikit-image中找到了这个例子,但在使用第二个模块之前,我想问一下,在形状模块中是否存在计算这些索引的方法。
提前致谢
答案 0 :(得分:2)
这个问题有点老,但是我最近遇到了这个问题,这是我做的:
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使得通过minimum_rotated_rectangle
来计算mbr很容易,但是似乎相对的两边的长度并不相等。因此,上面的方法计算出每边的长度,然后取最小值/最大值。
答案 1 :(得分:0)
首先计算多边形的最小边界矩形 - 参见How to find the minimum-area-rectangle for given points?中描述的过程,除了你将从凸包开始。在Shapely中,使用.convex_hull()
方法计算多边形的凸包。
然后,一旦你有MBR,你就可以找到主轴/副轴。