python中具有形状的Multipolygon三角形网格/网格

时间:2018-11-29 21:17:50

标签: python graphics polygon triangulation shapely

我想用匀整的形状制作一个三角形的网格,这些三角形是多边形。

Here is picture:

我有一个坐标点列表(每个点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优化该网格,以用尽可能多的三角形填充另一个多边形。

1 个答案:

答案 0 :(得分: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