【问题标题】:How to use Shapely to plot squares/rectangles along a centre line如何使用 Shapely 沿中心线绘制正方形/矩形
【发布时间】:2018-04-30 06:27:37
【问题描述】:

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

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

【问题讨论】:

  • 你提出的问题没有完全指定。 ¿ 正方形有固定的等长吗? ¿应该将中心放置在哪里(每个固定步骤或变量)? ¿ 方格是否应该覆盖线? ¿ 正方形可以重叠吗? ¿ 方块应该相互接触吗?
  • 感谢您指出我的问题的差距。正方形具有固定的相等长度。中心应该放置在每个固定的步骤中。方格必须覆盖线。正方形不能重叠,但它们必须相互接触。希望这能澄清我的问题。
  • 根据线的形状和正方形的长度,不可能满足所有这些要求。

标签: python gis shapely


【解决方案1】:
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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-15
    • 2013-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多