属性错误'function'对象没有属性'DataType'



我认为这是一个简单的解决方案,可能是因为我在python上。

我想做的是采用这个 4 条带 NAIP 并创建一个 432 假彩色图像。最终创建一个脚本,该脚本将运行数百个这样的图像来做到这一点,但这显然很遥远。

我不断收到错误AttributeError: 'function' object has no attribute 'DataType'如果我输入数据类型 ex:gdal.GDT_Byte(我不完全理解(,那么稍后我会收到一个错误AttributeError: 'function' object has no attribute 'shape'这让我相信这个栅格与我在类的类似函数中使用的栅格之间存在一些不同,或者我从根本上在栅格中读取错误。

这是我的完整代码:

import os
from osgeo import gdal
#change directory to data location
data_folder = r'my directory'
os.chdir(data_folder)
#open the .tif file and its bands, make sure they opened properly
ds = gdal.Open('example.tif') 
if ds is None: 
raise IOError('Cound not open raster ya n00b')
band1 = ds.GetRasterBand(1).ReadAsArray
if band1 is None:
raise IOError('bandz 1 didnt make her dance')
band2 = ds.GetRasterBand(2).ReadAsArray
if band2 is None:
raise IOError('bandz 2 didnt make her dance')
band3 = ds.GetRasterBand(3).ReadAsArray
if band3 is None:
raise IOError('bandz 3 didnt make her dance')
band4 = ds.GetRasterBand(4).ReadAsArray
if band4 is None:
raise IOError('bandz 4 didnt make her dance')
#Get the GeoTiff driver to create an output raster    
gtiff_driver = gdal.GetDriverByName('GTiff')
#Get the data type name
data_type = band1.DataType
data_type = gdal.GetDataTypeName(data_type)
test_1 = gtiff_driver.Create('test_v1.tif', ds.RasterXSize, ds.RasterYSize, 3, gdal.GDT_Byte)
if test_1 is None:
raise IOError('Could not create raster test_1')
test_1.SetProjection(ds.GetProjection())
test_1.SetGeoTransform(ds.GetGeoTransform())
t1_band1 = test_1.GetRasterBand(1)
t1_band1.WriteArray(band4)
t1_band2 = test_1.GetRasterBand(2)
t1_band2.WriteArray(band3)
t1_band3 = test_1.GetRasterBand(3)
t1_band3.WriteArray(band2)
del t1_band1, t1_band2, t1_band3, test_1, ds 

我觉得我在这里只是愚蠢,但真的很感激一些帮助。哦,我还在使用Python 2,我知道我需要尽快切换。谢谢!

我想试试

band1 = ds.GetRasterBand(1).ReadAsArray()

末尾有括号。

括号是 python 语法,用于调用函数并获取结果。 如果没有括号,您就band1成为ReadAsArray函数的同义词。 这在其他情况下可能会有所帮助,您可以这样做

band1 = ds.GetRasterBand(1).ReadAsArray    
x = band1() 

哪个叫ReadAsArray

但这不是你想做的

最新更新