属性错误“功能”对象没有属性“ DataType”

时间:2019-12-18 20:54:11

标签: python gis python-2.x raster

我认为这是一个简单的修复程序,可能是由于我在python方面的能力。

我要尝试的是采用这4个带状的NAIP并创建432个伪彩色图像。最终创建一个脚本来运行数百个此类图像,但这显然还差得很远。

我一直收到错误消息AttributeError: 'function' object has no attribute 'DataType' 如果我输入一个数据类型,例如: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,我知道我需要尽快进行切换。谢谢!

1 个答案:

答案 0 :(得分:0)

我想尝试

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

末尾带有括号。

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

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

会调用ReadAsArray

但这不是您想要做的

相关问题