【问题标题】:Multipolygon triangular mesh/grid with shapely in python多多边形三角形网格/网格与python中的形状
【发布时间】:2018-11-29 21:17:50
【问题描述】:

我想做一个三角形的网格,这些三角形是多边形,使用匀称。

我有一个坐标点列表(每个点 2 个坐标)和一个连接列表。

import numpy as np
import shapely.geometry as geometry


xlen = 20
ylen = 20
x0=0
y0=0
xPoints = np.arange(x0,xlen+1,1)
yPoints = np.arange(y0,ylen+1,1)

GridPoints = np.array([[[x,y] for x in xPoints] for y in yPoints])

triangles = [[i+j*(ylen+1),
      (i+1)+j*(ylen+1),
      i+(j+1)*(ylen+1)] for i in range(ylen) for j in range(xlen)]

需要多边形,因为我稍后需要针对 x 和 y 优化该网格,以用尽可能多的三角形填充另一个多边形。

【问题讨论】:

    标签: python graphics polygon triangulation shapely


    【解决方案1】:

    我希望您想用三角形完全填充该区域。你的“三角形”只是其中的一半。如果你只想要你的一半,只需注释掉 for 循环的第二部分。

    import numpy as np
    from shapely.geometry import Polygon
    from geopandas import GeoSeries
    
    xlen = 20
    ylen = 20
    x0 = 0
    y0 = 0
    xPoints = np.arange(x0, xlen + 1, 1)
    yPoints = np.arange(y0, ylen + 1, 1)
    
    GridPoints = list((x, y) for x in xPoints for y in yPoints)
    
    triangles = []  # list of triangles to be populated
    for i in range(ylen):
        for j in range(xlen):
            # triangles with perpendicular angle on the bottom left
            triangles.append([i + j * (ylen + 1), (i + 1) + j * (ylen + 1), i + (j + 1) * (ylen + 1)])
            # triangles with perpendicular angle on the top right
            triangles.append([(i + 1) + j * (ylen + 1), i + (j + 1) * (ylen + 1), (i + 1) + (j + 1) * (ylen + 1)])
    
    polygons = []  # list of polygons to be populated
    for triangle in triangles:
        polygon = Polygon([GridPoints[triangle[0]], GridPoints[triangle[1]], GridPoints[triangle[2]]])
        polygons.append(polygon)
    gs = GeoSeries(polygons)  # save polygons to geopandas GeoSeries
    

    我正在将你的形状多边形保存到 GeoPandas GeoSeries 中。结果如下所示: plotted GeoSeries

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-25
      • 2021-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多