空间分析:2-4.Python生成泰森多边形

空间分析:2-4.Python⽣成泰森多边形
代码注释很详细,如何⽣成泰森多边形和德洛内三⾓⽹。
总抗氧化能力⽤的是Python3,使⽤了scipy和matplotlib、numpy、shapely,引⽤matplotlib主要是为了看⼀下效果。
注释很详细,看代码就⾏。
from scipy.spatial import Voronoi,voronoi_plot_2d
import matplotlib.pyplot as plt
import numpy as np
ry import Polygon, LineString, Point, MultiPoint
from shapely.ops import polygonize
# 要⽣成德劳内三⾓⽹和泰森多边形的点⽂件
f = open(','r',encoding='utf-8')
flines = f.readlines()
# 所有的点
points = []
for line in flines:
# line是这样的:1 Point (116.32600952681723072 39.87965125233392882)
linelist = line.strip('\n').split('\t')
pointstr = linelist[1].replace('Point (','').replace(')','').split(' ')
points.append([float(pointstr[0]),float(pointstr[1])])
# 记录点个数,德劳内三⾓形⽤
pointlength = len(points)
# 取凸包,确保所有点都能获取⼀个多边形
钴60
# radius单位为度,0.00001约等于1⽶
radius = 1.0
convexbuffer = MultiPoint(points).convex_hull.buffer(radius)
莫麟传奇之四大古国全集array = np.array(points)
f.close()
# 沃罗诺伊图
vor = Voronoi(array,furthest_site=False, incremental=True, qhull_options=None)
# 泰森多边形的顶点
vertices = vor.vertices
fvertices = open('','a',encoding='utf-8')
for index,v in enumerate(vertices):
fvertices.write(str(index)+'\t'+'POINT('+str(v[0])+' '+str(v[1])+')'+'\n')
fvertices.close()
# 泰森多边形的⾯,-1代表⽆穷索引
regions = ions
fregions = open('','a',encoding='utf-8')
for index,r in enumerate(regions):
if len(r) == 0:
continue
if -1 in r:
continue
angulars = []
for id in r:
angulars.append(vertices[id])
angulars.append(vertices[r[0]])
当代教育科学polygon = Polygon(angulars)
fregions.write(str(index)+'\t'+str(polygon.wkt)+'\n')
fregions.close()
# 德劳内三⾓形的边,⽤原始的点数量
vorOriginal = Voronoi(array[0:pointlength],furthest_site=False, incremental=True, qhull_options=None)
ridge_points = vorOriginal.ridge_points
polylines = []
for ridge in ridge_points:
polyline = LineString([points[ridge[0]],points[ridge[1]]])
polylines.append(polyline)上海新型建材矿棉厂
# 德劳内三⾓形构⾯
# 德劳内三⾓形构⾯
delaunays = polygonize(polylines)
fdelaunay = open(r'','a',encoding='utf-8') for index,p in enumerate(delaunays):
fdelaunay.write(str(index)+'\t'+str(p.wkt)+'\n') fdelaunay.close()
# 泰森多边形的边的索引数组,-1代表⽆穷索引
ridge_vertices = vor.ridge_vertices
# 打印到控制台上看看结果
print('vertices')
print(vertices)
print('region')
print(regions)
print('ridge_points')
print(ridge_points)
print('ridge_vertices')
print(ridge_vertices)
# 打开界⾯,看看效果
fig = voronoi_plot_2d(vor)
plt.show()
matplotlib的效果如下:教育叙事研究的一般步骤
把⽣成的数据放到QGIS⾥:

本文发布于:2024-09-24 20:30:17,感谢您对本站的认可!

本文链接:https://www.17tex.com/xueshu/402639.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:多边形   看看   德劳内三   教育
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议