x = self.x_origin + (j + 0.5) * self.pixel_width y = self.y_origin + (i + 0.5) * self.pixel_height z = val self.iteSize += 1 return x, y, z dsm_path = 'C:\sanwei\jianmo\Productions\Production_2\Production_2_DSM_part_2_2.tif' dsm_data = gdal.Open(dsm_path) dsm_array = dsm_data.ReadAsArray() spiral_iterator = SpiralIterator(dsm_array,x=810,y=500) while spiral_iterator.hasNext(): x, y, z = spiral_iterator.get() print(f'Value at ({x},{y}):{z}')这段代码怎么改可以将打印的坐标保存为文档

时间: 2023-12-15 22:04:41 浏览: 31
你可以将打印出来的坐标以及对应的值写入到一个文本文件中,方法和上面类似。以下是修改后的代码示例: ```python import gdal class SpiralIterator: def __init__(self, array, x=0, y=0): self.array = array self.x_origin = x self.y_origin = y self.pixel_width = 1 self.pixel_height = 1 self.iteSize = 0 self.i = 0 self.j = 0 self.di = 0 self.dj = 1 self.width = array.shape[1] self.height = array.shape[0] def hasNext(self): return self.iteSize < self.width * self.height def get(self): val = self.array[self.i, self.j] x = self.x_origin + (self.j + 0.5) * self.pixel_width y = self.y_origin + (self.i + 0.5) * self.pixel_height self.iteSize += 1 self.i, self.j, self.di, self.dj = self.getNextIndex() return x, y, val def getNextIndex(self): if (self.i + self.di < 0 or self.i + self.di == self.height or self.j + self.dj < 0 or self.j + self.dj == self.width or self.array[self.i + self.di, self.j + self.dj] is None): self.di, self.dj = self.dj, -self.di return self.i + self.di, self.j + self.dj, self.di, self.dj dsm_path = r'C:\sanwei\jianmo\Productions\Production_2\Production_2_DSM_part_2_2.tif' dsm_data = gdal.Open(dsm_path) dsm_array = dsm_data.ReadAsArray() spiral_iterator = SpiralIterator(dsm_array, x=810, y=500) # 打开文件 with open('coordinates.txt', 'w') as f: while spiral_iterator.hasNext(): x, y, z = spiral_iterator.get() print(f'Value at ({x},{y}): {z}') # 将坐标和对应的值写入文件 f.write(f'({x},{y}): {z}\n') print('Done!') ``` 在这个例子中,我们使用了Python的with语句来打开文件,它可以自动管理文件的打开和关闭,避免了手动关闭文件时可能出现的错误。在while循环中,我们将打印的坐标和对应的值写入到了文件中。文件的名字可以根据你的需要自行修改。运行结束后,你可以在相应的目录下找到保存好的coordinates.txt文件。

相关推荐

class SpiralIterator: def init(self, source, x=810, y=500, length=None): self.source = source self.row = np.shape(self.source)[0]#第一个元素是行数 self.col = np.shape(self.source)[1]#第二个元素是列数 if length: self.length = min(length, np.size(self.source)) else: self.length = np.size(self.source) if x: self.x = x else: self.x = self.row // 2 if y: self.y = y else: self.y = self.col // 2 self.i = self.x self.j = self.y self.iteSize = 0 geo_transform = dsm_data.GetGeoTransform() self.x_origin = geo_transform[0] self.y_origin = geo_transform[3] self.pixel_width = geo_transform[1] self.pixel_height = geo_transform[5] def hasNext(self): return self.iteSize < self.length # 不能取更多值了 def get(self): if self.hasNext(): # 还能再取一个值 # 先记录当前坐标的值 —— 准备返回 i = self.i j = self.j val = self.source[i][j] # 计算下一个值的坐标 relI = self.i - self.x # 相对坐标 relJ = self.j - self.y # 相对坐标 if relJ > 0 and abs(relI) < relJ: self.i -= 1 # 上 elif relI < 0 and relJ > relI: self.j -= 1 # 左 elif relJ < 0 and abs(relJ) > relI: self.i += 1 # 下 elif relI >= 0 and relI >= relJ: self.j += 1 # 右 #判断索引是否在矩阵内 x = self.x_origin + (j + 0.5) * self.pixel_width y = self.y_origin + (i + 0.5) * self.pixel_height z = val self.iteSize += 1 return x, y, z dsm_path = 'C:\sanwei\jianmo\Productions\Production_2\Production_2_DSM_part_2_2.tif' dsm_data = gdal.Open(dsm_path) dsm_array = dsm_data.ReadAsArray() spiral_iterator = SpiralIterator(dsm_array,x=810,y=500) while spiral_iterator.hasNext(): x, y, z = spiral_iterator.get() print(f'Value at ({x},{y}):{z}')这段代码怎么改能依据共线方程将地面点(X,Y,Z)反算其在原始航 片中的像素值行列号( r,c),

from osgeo import gdal import numpy as np class SpiralIterator: def init(self, source, x=810, y=500, length=None): self.source = source self.row = np.shape(self.source)[0]#第一个元素是行数 self.col = np.shape(self.source)[1]#第二个元素是列数 if length: self.length = min(length, np.size(self.source)) else: self.length = np.size(self.source) if x: self.x = x else: self.x = self.row // 2 if y: self.y = y else: self.y = self.col // 2 self.i = self.x self.j = self.y self.iteSize = 0 geo_transform = dsm_data.GetGeoTransform() self.x_origin = geo_transform[0] self.y_origin = geo_transform[3] self.pixel_width = geo_transform[1] self.pixel_height = geo_transform[5] def hasNext(self): return self.iteSize < self.length # 不能取更多值了 def get(self): if self.hasNext(): # 还能再取一个值 # 先记录当前坐标的值 —— 准备返回 i = self.i j = self.j val = self.source[i][j] # 计算下一个值的坐标 relI = self.i - self.x # 相对坐标 relJ = self.j - self.y # 相对坐标 if relJ > 0 and abs(relI) < relJ: self.i -= 1 # 上 elif relI < 0 and relJ > relI: self.j -= 1 # 左 elif relJ < 0 and abs(relJ) > relI: self.i += 1 # 下 elif relI >= 0 and relI >= relJ: self.j += 1 # 右 #判断索引是否在矩阵内 x = self.x_origin + (j + 0.5) * self.pixel_width y = self.y_origin + (i + 0.5) * self.pixel_height z = val self.iteSize += 1 return x, y, z dsm_path = 'C:\sanwei\jianmo\Productions\Production_2\Production_2_DSM_part_2_2.tif' dsm_data = gdal.Open(dsm_path) dsm_array = dsm_data.ReadAsArray() spiral_iterator = SpiralIterator(dsm_array,x=810,y=500) while spiral_iterator.hasNext(): x, y, z = spiral_iterator.get() print(f'Value at ({x},{y}):{z}')这段代码怎么改可以)依据共线方程将地面点(X,Y,Z)反算其在原始航 片中的像素值行列号( r,c),当 img1 该位置像素值为 0 值,修改其像素值为 255,当 img1 该( r,c) 位置像素值为 255 时,说明此点已被占用,则对地面点(X,Y,Z)标记此点位被遮蔽。

class SpiralIterator: def init(self, source, x=810, y=500, length=None): self.source = source self.row = np.shape(self.source)[0] #第一个元素是行数 self.col = np.shape(self.source)[1] #第二个元素是列数 if length: self.length = min(length, np.size(self.source)) else: self.length = np.size(self.source) if x: self.x = x else: self.x = self.row // 2 if y: self.y = y else: self.y = self.col // 2 self.i = self.x self.j = self.y self.iteSize = 0 geo_transform = dsm_data.GetGeoTransform() self.x_origin = geo_transform[0] self.y_origin = geo_transform[3] self.pixel_width = geo_transform[1] self.pixel_height = geo_transform def hasNext(self): return self.iteSize < self.length # 不能取更多值了 def get(self): if self.hasNext(): # 还能再取一个值 # 先记录当前坐标的值 —— 准备返回 i = self.i j = self.j val = self.source[i][j] # 计算下一个值的坐标 relI = self.i - self.x # 相对坐标 relJ = self.j - self.y # 相对坐标 if relJ > 0 and abs(relI) < relJ: self.i -= 1 # 上 elif relI < 0 and relJ > relI: self.j -= 1 # 左 elif relJ < 0 and abs(relJ) > relI: self.i += 1 # 下 elif relI >= 0 and relI >= relJ: self.j += 1 # 右 #判断索引是否在矩阵内 x = self.x_origin + (j + 0.5) * self.pixel_width y = self.y_origin + (i + 0.5) * self.pixel_height col = int((x - dsm_data.GetGeoTransform()[0]) / dsm_data.GetGeoTransform()[1]) row = int((y - dsm_data.GetGeoTransform()[3]) / dsm_data.GetGeoTransform()[5]) z = val self.iteSize += 1 return x, y, z, row, col dsm_path = 'C:\sanwei\jianmo\Productions\Production_2\Production_2_DSM_part_2_2.tif' dsm_data = gdal.Open(dsm_path) dsm_array = dsm_data.ReadAsArray() spiral_iterator = SpiralIterator(dsm_array,x=810,y=500) while spiral_iterator.hasNext(): x, y, z, row, col = spiral_iterator.get() print(f'Value at ({x},{y}) ({row},{col}):{z}')这段代码怎么改可以当原始航片该位置像素值为 0 值,修改其像素值为 255,当 原始航片该位置像素值为 255 时,说明此点已被占用,则对地面点(X,Y,Z)标记此点位被遮蔽

最新推荐

recommend-type

Python源码-数学美之樱花.py

Python源码-数学美之樱花
recommend-type

蚁群算法(ACO)求解TSP问题,MATLAB源码,代码注释详细,可根据自身需求拓展应用

蚁群算法(ACO)求解TSP问题,MATLAB源码,代码注释详细,可根据自身需求拓展应用
recommend-type

2024年5月最新采集大众点评全国(内地)-学习培训大类-店铺基础信息,93余万家

2024年5月最新采集大众点评全国(内地)-学习培训大类-店铺基础信息,93余万家。此处仅展示1万家,全量也有。 2024年5月最新大众点评店铺基础信息采集。含美食、休闲娱乐、结婚、电影演出赛事、丽人、酒店、亲子、周边游、运动健身、购物、家装、学习培训、医疗健康、爱车、宠物等十几大类共几千万家店铺信息。
recommend-type

My-Graduation-Project-demo

服务器
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

用matlab绘制高斯色噪声情况下的频率估计CRLB,其中w(n)是零均值高斯色噪声,w(n)=0.8*w(n-1)+e(n),e(n)服从零均值方差为se的高斯分布

以下是用matlab绘制高斯色噪声情况下频率估计CRLB的代码: ```matlab % 参数设置 N = 100; % 信号长度 se = 0.5; % 噪声方差 w = zeros(N,1); % 高斯色噪声 w(1) = randn(1)*sqrt(se); for n = 2:N w(n) = 0.8*w(n-1) + randn(1)*sqrt(se); end % 计算频率估计CRLB fs = 1; % 采样频率 df = 0.01; % 频率分辨率 f = 0:df:fs/2; % 频率范围 M = length(f); CRLB = zeros(M,1); for
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依