- 关于内部点的提取,基于网格形状,可以通过简单的数学来完成,如下所示:
from shapely.geometry import Polygon, MultiPoint
from math import ceil
# The setup
pol = Polygon([(0,0), (1,0), (1,1), (0,1)])
cell_size = 0.1
# Extract coordinates of the bounding box:
minx, miny, maxx, maxy = pol.bounds
# How many points in each dimension ?
rows = int(ceil((maxy - miny) / cell_size))
cols = int(ceil((maxx - minx) / cell_size))
# Actually generate the points:
pts = []
x, y = minx, miny
for countcols in range(cols):
for countrows in range(rows):
pts.append((x, y))
y += cell_size
x += cell_size
y = miny
# Create the MultiPoint from this list
result_pts_interior = MultiPoint(pts)
结果:
但请注意,如果多边形没有矩形形状,它也会生成位于多边形外部的点(您应该在将它们添加到列表之前测试它们是否与多边形相交pol.intersects(Point(x, y)))。
- 关于边界点的提取,可以使用shapely LineStrings的
interpolate方法,如下:
from shapely.geometry import Polygon, MultiPoint
import numpy as np
# The setup
pol = Polygon([(0,0), (1,0), (1,1), (0,1)])
distance_between_pts = 0.1
boundary = pol.boundary # Boundary of polygon as a linestring
boundary_length = boundary.length # Its length
# Build a list of points spaced by 0.1 along this linestring:
pts_boundary = [
boundary.interpolate(n, False) for n
in np.linspace(0, boundary_length, int(boundary_length / distance_between_pts) + 1)
]
# Create the MultiPoint from this list
result_pts_boundary = MultiPoint(pts_boundary)
结果: