I want do make a mesh of triangles which are polygons, using shapely.
I have a list of coordinate points (2 coordinates for each point) and a list of connections.
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)]
Polygons are needed because I will later need to optimize that mesh with respect to x and y,to fill out another polygon with as many triangles as possible.
I expect you want to fill the area completely with triangles. Your "triangles" are only half of them. If you want only your half, just comment-out the second part of the for loop.
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
I am saving your shapely polygons into GeoPandas GeoSeries. Result looks like this: plotted GeoSeries
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.