【问题标题】:numpy meshgrid to Shapely polygonsnumpy meshgrid 到 Shapely 多边形
【发布时间】:2016-05-05 02:35:52
【问题描述】:

我正在尝试创建一个 numpy meshgrid 并将其转换为 Shapely 多边形。我可能会用一种非常蛮力的方法来解决这个问题,但感觉必须有一个很好的技巧来完成这个,但我还没有想出它。

这让我得到了点的网格(假设在 Jupyter 中运行)-

import numpy as np
from matplotlib import pyplot

fig = pyplot.figure(figsize=(10, 10))
ax = fig.add_subplot(111, aspect='equal')

x,y = np.mgrid[-5:-1:8j, 1:5:8j]
ax.plot(x,y, 'o', color='#000000')
pyplot.show()

现在需要将所有这些点水平和垂直连接起来,形成 Shapely 多边形。我的第一次尝试是生成一个 Shapely MultiLineString 来绘制垂直和水平线,然后对其执行多边形化操作。这导致只创建了主要的外部多边形 - 这是由于 MultiLineString 仅包含外部多边形上的顶点。

我知道使用栅格和 GDAL 可能更合理,但我的情况要求最终结果是 Shapely 多边形。

感谢任何帮助追踪解决方案!

【问题讨论】:

    标签: python numpy shapely


    【解决方案1】:

    您基本上必须在构造 MultiLineString 之前定义每一行。

    import numpy as np
    from shapely.geometry import MultiLineString
    from shapely.ops import polygonize
    
    x = np.linspace(-5, -1, 8)
    y = np.linspace(1, 5, 8)
    
    hlines = [((x1, yi), (x2, yi)) for x1, x2 in zip(x[:-1], x[1:]) for yi in y]
    vlines = [((xi, y1), (xi, y2)) for y1, y2 in zip(y[:-1], y[1:]) for xi in x]
    
    grids = list(polygonize(MultiLineString(hlines + vlines)))
    

    【讨论】:

    • 如何从网格中提取坐标?
    猜你喜欢
    • 1970-01-01
    • 2015-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-15
    • 2023-02-17
    相关资源
    最近更新 更多