def DSM_grid_sorting_masking_check(DSM,grid_size,threshold_angle): ''' 进行基于DSM格网排序的遮蔽检测方法 :param DSM: 输入的数字高程模型 :param grid_size: 格网大小 :param threshold_angle: 实现遮蔽的最大角度 :return: 遮蔽检测结果。True表示不遮蔽,False表示遮蔽 ''' width = DSM.RasterXSize height = DSM.RasterYSize #计算网格数量 grid_num_y =int(np.ceil(height/grid_size)) grid_num_x =int(np.ceil(width/grid_size)) #初始化遮蔽检测结果矩阵 result = np.ones((grid_num_y,grid_num_x),dtype=bool) #计算每个格网进行遮蔽检测 for i in range(grid_num_y): for j in range(grid_num_x): #当前格网内的点坐标 y_min = i*grid_size y_max = min((i+1)*grid_size,height) x_min = j * grid_size x_max = min((j+1)*grid_size,width) coords = np.argwhere(DSM.ReadAsArray(x_min, y_min, x_max - x_min, y_max - y_min) > 0) coords[:, 0] += y_min coords[:, 1] += x_min # 构建KD树 tree = cKDTree(coords) # 查询每个点的最邻近点 k = 2 dist, ind = tree.query(coords, k=k) # 计算每个点的法向量 normals = np.zeros(coords.shape) for l in range(coords.shape[0]): if k == 2: p1 = coords[l, :] p2 = coords[ind[l, 1], :] else: p1 = coords[l, :] p2 = coords[ind[l, 1], :] normals[l, :] = np.cross(p1 - p2, p1 - DSM.ReadAsArray(p1[1], p1[0], 1, 1)) # 计算每个点的可见性 visibilities = np.zeros(coords.shape[0]) for l in range(coords.shape[0]): if k == 2: p1 = coords[l, :] p2 = coords[ind[l, 1], :] else: p1 = coords[l, :] p2 = coords[ind[l, 1], :] angle = np.cross(np.dot(normals[l, :], (p2 - p1) / dist[l, 1])) * 180 / np.pi if angle <= threshold_angle: visibilities[l] = 1 # 判断当前格网是否遮蔽 if np.sum(visibilities) == 0: result[i, j] = False else: result[i, j] = True return result dsm_path = 'C:/yingxiang/output.tif' DSM = gdal.Open(dsm_path) result = DSM_grid_sorting_masking_check(DSM,grid_size=10,threshold_angle=10) print(result.shape)这段代码in method 'BandRasterIONumPy', argument 3 of type 'double'为什么会报这个错误
时间: 2024-04-27 20:21:45 浏览: 35
这段代码中并没有直接调用BandRasterIONumPy函数,因此很难确定是哪行代码导致了这个错误。但是,由于GDAL库中的一些函数需要传入特定的参数类型,而且在不同的操作系统或版本中,这些参数类型可能会有所不同,因此建议您检查一下代码中与GDAL库相关的函数调用,确保传入的参数类型正确。另外,您可以尝试更新GDAL库的版本,或者查看GDAL库的文档,了解特定函数所需的正确参数类型。
相关问题
def DSM_grid_sorting_masking_check(DSM,grid_size,threshold_angle): ''' 进行基于DSM格网排序的遮蔽检测方法 :param DSM: 输入的数字高程模型 :param grid_size: 格网大小 :param threshold_angle: 实现遮蔽的最大角度 :return: 遮蔽检测结果。True表示不遮蔽,False表示遮蔽 ''' width = DSM.RasterXSize height = DSM.RasterYSize #计算网格数量 grid_num_y =int(np.ceil(height/grid_size)) grid_num_x =int(np.ceil(width/grid_size)) #初始化遮蔽检测结果矩阵 result = np.ones((grid_num_y,grid_num_x),dtype=bool) #计算每个格网进行遮蔽检测 for i in range(grid_num_y): for j in range(grid_num_x): #当前格网内的点坐标 y_min = i*grid_size y_max = min((i+1)*grid_size,height) x_min = j * grid_size x_max = min((j+1)*grid_size,width) coords = np.argwhere(DSM.ReadAsArray(x_min, y_min, x_max - x_min, y_max - y_min) > 0) coords[:, 0] += y_min coords[:, 1] += x_min # 构建KD树 tree = cKDTree(coords) # 查询每个点的最邻近点 k = 2 dist, ind = tree.query(coords, k=k) # 计算每个点的法向量 normals = np.zeros(coords.shape) for l in range(coords.shape[0]): if k == 2: p1 = coords[l, :] p2 = coords[ind[l, 1], :] else: p1 = coords[l, :] p2 = coords[ind[l, 1], :] normals[l, :] = np.cross(p1 - p2, p1 - DSM.ReadAsArray(p1[1], p1[0], 1, 1)) # 计算每个点的可见性 visibilities = np.zeros(coords.shape[0]) for l in range(coords.shape[0]): if k == 2: p1 = coords[l, :] p2 = coords[ind[l, 1], :] else: p1 = coords[l, :] p2 = coords[ind[l, 1], :] angle = np.cross(np.dot(normals[l, :], (p2 - p1) / dist[l, 1])) * 180 / np.pi if angle <= threshold_angle: visibilities[l] = 1 # 判断当前格网是否遮蔽 if np.sum(visibilities) == 0: result[i, j] = False else: result[i, j] = True return result dsm_path = 'C:/yingxiang/output.tif' DSM = gdal.Open(dsm_path) result = DSM_grid_sorting_masking_check(DSM,grid_size=10,threshold_angle=10) print(result.shape)这段代码怎么改可以输出每个点是否被遮蔽
可以在函数中添加一个返回值,记录每个点是否被遮蔽。可以按照以下方式修改函数:
```python
def DSM_grid_sorting_masking_check(DSM,grid_size,threshold_angle):
''' 进行基于DSM格网排序的遮蔽检测方法
:param DSM: 输入的数字高程模型
:param grid_size: 格网大小
:param threshold_angle: 实现遮蔽的最大角度
:return: 遮蔽检测结果。True表示不遮蔽,False表示遮蔽,以及每个点是否被遮蔽的矩阵,True表示不遮蔽,False表示遮蔽
'''
width = DSM.RasterXSize
height = DSM.RasterYSize
#计算网格数量
grid_num_y =int(np.ceil(height/grid_size))
grid_num_x =int(np.ceil(width/grid_size))
#初始化遮蔽检测结果矩阵
result = np.ones((grid_num_y,grid_num_x),dtype=bool)
mask = np.zeros((height, width), dtype=bool)
#计算每个格网进行遮蔽检测
for i in range(grid_num_y):
for j in range(grid_num_x):
#当前格网内的点坐标
y_min = i*grid_size
y_max = min((i+1)*grid_size,height)
x_min = j * grid_size
x_max = min((j+1)*grid_size,width)
coords = np.argwhere(DSM.ReadAsArray(x_min, y_min, x_max - x_min, y_max - y_min) > 0)
coords[:, 0] += y_min
coords[:, 1] += x_min
# 构建KD树
tree = cKDTree(coords)
# 查询每个点的最邻近点
k = 2
dist, ind = tree.query(coords, k=k)
# 计算每个点的法向量
normals = np.zeros(coords.shape)
for l in range(coords.shape[0]):
if k == 2:
p1 = coords[l, :]
p2 = coords[ind[l, 1], :]
else:
p1 = coords[l, :]
p2 = coords[ind[l, 1], :]
normals[l, :] = np.cross(p1 - p2, p1 - DSM.ReadAsArray(p1[1], p1[0], 1, 1))
# 计算每个点的可见性
visibilities = np.zeros(coords.shape[0])
for l in range(coords.shape[0]):
if k == 2:
p1 = coords[l, :]
p2 = coords[ind[l, 1], :]
else:
p1 = coords[l, :]
p2 = coords[ind[l, 1], :]
angle = np.cross(np.dot(normals[l, :], (p2 - p1) / dist[l, 1])) * 180 / np.pi
if angle <= threshold_angle:
visibilities[l] = 1
# 判断当前格网是否遮蔽
if np.sum(visibilities) == 0:
result[i, j] = False
mask[y_min:y_max, x_min:x_max] = True
else:
result[i, j] = True
return result, mask
dsm_path = 'C:/yingxiang/output.tif'
DSM = gdal.Open(dsm_path)
result, mask = DSM_grid_sorting_masking_check(DSM,grid_size=10,threshold_angle=10)
print(result.shape)
```
使用该函数后,`mask` 矩阵的值即表示每个点是否被遮蔽,True 表示被遮蔽,False 表示没有被遮蔽。
def DSM_grid_sorting_masking_check(DSM,grid_size,threshold_angle): width = DSM.RasterXSize height = DSM.RasterYSize #计算网格数量 grid_num_y =int(np.ceil(height/grid_size)) grid_num_x =int(np.ceil(width/grid_size)) #初始化遮蔽检测结果矩阵 result = np.ones((grid_num_y,grid_num_x),dtype=bool) #计算每个格网进行遮蔽检测 for i in range(grid_num_y): for j in range(grid_num_x): #当前格网内的点坐标 y_min = i*grid_size y_max = min((i+1)*grid_size,height) x_min = j * grid_size x_max = min((j+1)*grid_size,width) coords = np.argwhere(DSM.ReadAsArray(x_min, y_min, x_max - x_min, y_max - y_min) > 0) coords[:, 0] += y_min coords[:, 1] += x_min # 构建KD树 tree = cKDTree(coords) # 查询每个点的最邻近点 k = 2 dist, ind = tree.query(coords, k=k) # 计算每个点的法向量 normals = np.zeros(coords.shape) for l in range(coords.shape[0]): if k == 2: p1 = coords[l, :] p2 = coords[ind[l, 1], :] else: p1 = coords[l, :] p2 = coords[ind[l, 1], :] normals[l, :] = np.cross(p1 - p2, p1 - DSM.ReadAsArray(p1[1], p1[0], 1, 1)) # 计算每个点的可见性 visibilities = np.zeros(coords.shape[0]) for l in range(coords.shape[0]): if k == 2: p1 = coords[l, :] p2 = coords[ind[l, 1], :] else: p1 = coords[l, :] p2 = coords[ind[l, 1], :] angle = np.cross(np.dot(normals[l, :], (p2 - p1) / dist[l, 1])) * 180 / np.pi if angle <= threshold_angle: visibilities[l] = 1 # 判断当前格网是否遮蔽 if np.sum(visibilities) == 0: result[i, j] = False else: result[i, j] = True return result dsm_path = 'C:/yingxiang/output.tif' DSM = gdal.Open(dsm_path) result = DSM_grid_sorting_masking_check(DSM,grid_size=10,threshold_angle=40) print(result)这段代码怎么改可以没有以下错误in method 'BandRasterIONumPy', argument 3 of type 'double'
这个错误通常是因为传入的参数类型和函数参数类型不匹配导致的。在这段代码中,可能是以下这一行代码引起的问题:
```
coords = np.argwhere(DSM.ReadAsArray(x_min, y_min, x_max - x_min, y_max - y_min) > 0)
```
这里传入到ReadAsArray函数中的参数是x_min, y_min, x_max - x_min, y_max - y_min,这些参数的类型需要是int型的像素坐标。如果这些参数的类型不正确,就会导致函数调用出错,进而导致BandRasterIONumPy函数出错。
要解决这个问题,可以先将输入的像素坐标转换为int类型,代码如下:
```
x_min = int(x_min)
y_min = int(y_min)
```
需要在x_min和y_min的计算后立即进行类型转换。
同样地,还需要检查其他传入函数的参数类型是否正确,确保类型匹配。
阅读全文