当前位置: 首页 > news >正文

点在多边形内_空间分析: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
from shapely.geometry import Polygon, LineString, Point, MultiPoint
from shapely.ops import polygonize
# 要生成德劳内三角网和泰森多边形的点文件
f = open(r'point.txt','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)
# 取凸包,确保所有点都能获取一个多边形
# radius单位为度,0.00001约等于1米
radius = 1.0
convexbuffer = MultiPoint(points).convex_hull.buffer(radius)
points.extend(convexbuffer.exterior.coords)
array = np.array(points)
f.close()
# 沃罗诺伊图
vor = Voronoi(array,furthest_site=False, incremental=True, qhull_options=None)
# 泰森多边形的顶点
vertices = vor.vertices
fvertices = open('voronoi_vertices.txt','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 = vor.regions
fregions = open('voronoi_regions.txt','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'voronoi_delaunays.txt','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的效果如下:

9b85f0cfc920e7a886b165811e364245.png

把生成的数据放到QGIS里:

9cedf2e60d7e9443d8de002e40b77730.png

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • struts2的s:param标签使用
  • leetcode 打印_剑指 Offer 32 - I. 从上到下打印二叉树
  • 使用spring的MBeanExporter暴露hibernate的StatisticsService对象方法
  • springboot分页插件_Spring boot整合Mybatis Generator以及PageHelper源码分析
  • ext2.0不能与prototype1.6一起使用
  • c语言scanf一次不定_初学C语言编程时最容易犯的错误,你踩坑了吗?
  • 知识可以触类旁通。。。
  • cmd bat删除文件命令_ftp下载文件命令地址,用cmd命令访问ftp下载文件地址
  • 洪昭光:21世纪的健康新标准
  • python做图片美化_Python实现简单的照片磨皮(照片智能磨皮) 最新免费版
  • excel公式大全详解_财务常用的Excel函数公式大全(共484个),帮你整理好了!...
  • 2007 - 2008
  • python读二进制文件遍历_读取二进制文件并遍历每个字节
  • 怎么让页面刷新不白屏_win10电脑出现DNS异常上不了网怎么办?
  • Windows Embedded Webcast 2008年1月预告
  • [译]如何构建服务器端web组件,为何要构建?
  • Bytom交易说明(账户管理模式)
  • IIS 10 PHP CGI 设置 PHP_INI_SCAN_DIR
  • Rancher-k8s加速安装文档
  • 百度贴吧爬虫node+vue baidu_tieba_crawler
  • 前端自动化解决方案
  • 区块链分支循环
  • 一个项目push到多个远程Git仓库
  • 移动端解决方案学习记录
  • 远离DoS攻击 Windows Server 2016发布DNS政策
  • 没有任何编程基础可以直接学习python语言吗?学会后能够做什么? ...
  • 你学不懂C语言,是因为不懂编写C程序的7个步骤 ...
  • ​​​​​​​GitLab 之 GitLab-Runner 安装,配置与问题汇总
  • ​低代码平台的核心价值与优势
  • ​水经微图Web1.5.0版即将上线
  • !! 2.对十份论文和报告中的关于OpenCV和Android NDK开发的总结
  • #单片机(TB6600驱动42步进电机)
  • (2)从源码角度聊聊Jetpack Navigator的工作流程
  • (C++17) std算法之执行策略 execution
  • (C语言)逆序输出字符串
  • (pytorch进阶之路)扩散概率模型
  • (附源码)springboot电竞专题网站 毕业设计 641314
  • (附源码)ssm基于jsp的在线点餐系统 毕业设计 111016
  • (附源码)ssm基于web技术的医务志愿者管理系统 毕业设计 100910
  • (每日持续更新)信息系统项目管理(第四版)(高级项目管理)考试重点整理 第13章 项目资源管理(七)
  • (一)VirtualBox安装增强功能
  • (转)iOS字体
  • (转)全文检索技术学习(三)——Lucene支持中文分词
  • (转)微软牛津计划介绍——屌爆了的自然数据处理解决方案(人脸/语音识别,计算机视觉与语言理解)...
  • ***检测工具之RKHunter AIDE
  • .net 4.0发布后不能正常显示图片问题
  • .NET 8 中引入新的 IHostedLifecycleService 接口 实现定时任务
  • .NET Core SkiaSharp 替代 System.Drawing.Common 的一些用法
  • .NET 线程 Thread 进程 Process、线程池 pool、Invoke、begininvoke、异步回调
  • .NET/C# 避免调试器不小心提前计算本应延迟计算的值
  • .NET6使用MiniExcel根据数据源横向导出头部标题及数据
  • [ 网络通信基础 ]——网络的传输介质(双绞线,光纤,标准,线序)
  • [2009][note]构成理想导体超材料的有源THz欺骗表面等离子激元开关——
  • [BSidesCF 2019]Kookie1
  • [BZOJ 4598][Sdoi2016]模式字符串