- Regarding to the extraction of the interior points, based on a grid shape, it can be done with simple math, like so :
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)
Result:
But note that if the polygon does not have a rectangular shape, it will also generate points which will be outside the polygon (you should then test if they intersect the polygon with pol.intersects(Point(x, y))
before adding them to the list).
- Regarding to the extraction of boundary points, it can be done using the
interpolate
method of shapely LineStrings, like so :
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)
Result:
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…