如何使用Shapely沿中心线绘制正方形/矩形

时间:2018-04-30 06:27:37

标签: python gis shapely

如何使用Shapely在中心线上绘制正方形/矩形?

A sketch of what I am trying to achieve

在上面的草图中,我想绘制沿黑线的红色方块。黑线是(x,y)点的集合。

1 个答案:

答案 0 :(得分:0)

from shapely.geometry import LineString, Point, box
import matplotlib.pyplot as plt
import numpy as np

# let's use a sine curve as our example line
x = np.arange(0, 3 * np.pi, 0.5)
y = np.sin(x)
xy = list(zip(x, y))
line = LineString(xy)
plt.plot(*line.xy)

# let's sample every 3 points along the line
x_samp = x[::3]
y_samp = y[::3]
plt.scatter(x_samp, y_samp)

# generate boxes along sampled points on the line
radius = (x_samp[1] - x_samp[0]) / 2
for i in range(len(x_samp)):
    box_loc = box(*Point(x_samp[i], y_samp[i]).buffer(radius).bounds)
    plt.plot(*box_loc.exterior.xy)

plt.axes().set_aspect('equal')
plt.show()

enter image description here