问题描述
我有以下匀称的像素多边形
big_poly = Polygon([(94.5, 77.0),(92.5, 53.0),(87.5, 45.0),(66.0, 20.5),(9.0, 3.5),(5.5, 6.0),(4.5, 13.0),(7.5, 26.0),(6.5, 91.0),(8.0, 92.5),(16.0, 92.5),(44.0, 86.5)])
我需要用 4 x 6 像素多边形填充 big_poly
,让它们都落在大多边形的边界内并平行于其最小轴(相同的旋转).
使用以下代码:
b = building_poly.boundary点数 = []对于范围内的 x(int(np.floor(xmin)), int(np.ceil(xmax)), 4):对于范围内的 y(int(np.floor(ymin)), int(np.ceil(ymax)), 6):点附加((x,y))点数 = 多点(点数)结果 = points.intersection(building_poly)
并在 result
中绘制点,但现在我需要从这些点创建多边形.如何将这些点变成矩形多边形?
我也发现了
I have the following shapely pixel polygon
big_poly = Polygon([(94.5, 77.0),
(92.5, 53.0),
(87.5, 45.0),
(66.0, 20.5),
(9.0, 3.5),
(5.5, 6.0),
(4.5, 13.0),
(7.5, 26.0),
(6.5, 91.0),
(8.0, 92.5),
(16.0, 92.5),
(44.0, 86.5)])
I need to fill the big_poly
with 4 by 6, pixel polygons, have them all fall within the border of the big one and parallel to its min axis (same rotation).
The solutions here sure were helpful thanks to @Georgy.So I was able to do the following:
with the following code:
b = building_poly.boundary
points = []
for x in range(int(np.floor(xmin)), int(np.ceil(xmax)), 4):
for y in range(int(np.floor(ymin)), int(np.ceil(ymax)), 6):
points.append((x, y))
points = MultiPoint(points)
result = points.intersection(building_poly)
And plotting the points in result
, but now I need to create polygons from these points.How can I turn these points into rectangular polygons?
I also found this, but not sure how to make it fit my case.
Here is what we could do:
- Construct two arrays of X and Y coordinates based on the boundaries of the given polygon
- Construct a grid of rectangles covering completely the given polygon by iterating over the consecutive pairs of X and Y coordinates
- Filter out those rectangles that don't lie completely inside the polygon
This is, most probably, not the most efficient solution, but it performs well for your case:
dx = 4
dy = 6
xmin, ymin, xmax, ymax = big_poly.bounds
xs = np.arange(np.floor(xmin), np.ceil(xmax) + 1, dx)
ys = np.arange(np.floor(ymin), np.ceil(ymax) + 1, dy)
rectangles = (Polygon([(x_start, y_start), (x_end, y_start),
(x_end, y_end), (x_start, y_end)])
for x_start, x_end in zip(xs, xs[1:])
for y_start, y_end in zip(ys, ys[1:]))
rectangles = [rectangle for rectangle in rectangles
if big_poly.contains(rectangle)]
这篇关于将小多边形堆叠在另一个更大的多边形中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!