无法在Python中使用Gdal编写光栅。错误:dict'对象没有属性'shape'

问题描述 投票:0回答:1

[我正在使用Python中的gdal填充一些数据后没有尝试创建栅格文件。

我有一个获取栅格数组的函数。

def raster2array(rasterfn):
try:
    bndNum_Val_Dic={}
    raster = gdal.Open(rasterfn)
    for bandNum in range(raster.RasterCount):
        bandNum += 1
        band=raster.GetRasterBand(bandNum)
        bandVal=band.ReadAsArray()
        bndNum_Val_Dic[bandNum]=bandVal
    raster=None
    return bndNum_Val_Dic
except Exception as e:
    print(e)

使用从此函数生成的数组,我试图编写栅格,该栅格在dict'对象没有属性'shape'的“ outband.WriteArray(array)”处引发错误。

import numpy as np
import gdal
from osgeo import osr
rasterfn ="MAH_20.tif"
newRasterfn ="MAH_FND.tif"
array= raster2array(rasterfn)
newValue = 100
Driver= 'GTiff'
bandNumber=1
raster = gdal.Open(rasterfn)
geotransform = raster.GetGeoTransform()
originX = geotransform[0]
originY = geotransform[3]
pixelWidth = geotransform[1]
pixelHeight = geotransform[5]
cols = raster.RasterXSize
rows = raster.RasterYSize
bandCount=raster.RasterCount
rasterDataType=raster.GetRasterBand(bandNumber).DataType
global Flag
if(Flag):
    driver = gdal.GetDriverByName(Driver)
    global outRaster
    outRaster = driver.Create(newRasterfn, cols, rows, bandCount, rasterDataType)
    Flag=False

outband = outRaster.GetRasterBand(bandNumber)
outRaster.SetGeoTransform((originX, pixelWidth, 0, originY, 0, pixelHeight))
outband = outRaster.GetRasterBand(bandNumber)
outband.WriteArray(array)
outRasterSRS = osr.SpatialReference()
outRasterSRS.ImportFromWkt(raster.GetProjectionRef())
outRaster.SetProjection(outRasterSRS.ExportToWkt())
outRaster.GetRasterBand(bandNumber).SetNoDataValue(newValue)
raster=None
if(bandNumber==bandCount):
    outRaster=None
    outband=None
    raster=None

我正在使用python 3.5和GDAL 3.0.2。有没有什么办法解决这一问题?任何帮助将不胜感激

python gdal
1个回答
0
投票

[您正在尝试两个人编写字典,而GDAL需要一个Numpy数组。尚不清楚您要写入哪些数据,但是将您的write语句更改为如下所示的内容至少应该消除错误消息。但是请确保您输入正确的频段。

outband.WriteArray(array[bandNumber])
© www.soinside.com 2019 - 2024. All rights reserved.