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),当原始航片该位置像素值为 0 值,修改其像素值为 255,当原始航片该( r,c) 位置像素值为 255 时,说明此点已被占用,则对地面点(X,Y,Z)标记此点位被遮蔽
时间: 2023-12-01 18:04:11 浏览: 80
对Python中class和instance以及self的用法详解
以下是修改后的代码:
```python
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 # 右
# 判断索引是否在矩阵内
if self.i >= 0 and self.i < self.row and self.j >= 0 and self.j < self.col:
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, i, j
else:
return None
else:
return None
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 True:
result = spiral_iterator.get()
if result is not None:
x, y, z, i, j = result
if dsm_array[i][j] == 0:
dsm_array[i][j] = 255
elif dsm_array[i][j] == 255:
# 对地面点(X,Y,Z)标记此点位被遮蔽
pass
else:
break
# 保存修改后的数组为 TIFF 文件
driver = gdal.GetDriverByName("GTiff")
new_dataset = driver.Create("modified_dsm.tif", dsm_data.RasterXSize, dsm_data.RasterYSize, 1, gdal.GDT_Float32)
new_dataset.SetGeoTransform(dsm_data.GetGeoTransform())
new_dataset.SetProjection(dsm_data.GetProjection())
new_dataset.GetRasterBand(1).WriteArray(dsm_array)
new_dataset.FlushCache()
```
这里添加了两个参数 `i` 和 `j` 分别表示当前像素在原始航片中的行列号。在 `while` 循环中,对于每个像素,判断其值是否为 0 或 255,进行相应的修改。最后将修改后的数组保存为 TIFF 文件。
阅读全文