GTiff格式文件:
A.修改数据类型(单字节,双字节,浮点型等)
B.BIP,BIL,BSQ数据组织格式转换
关键函数
Translate(destName, srcDS, **kwargs)
destName — Output dataset name
srcDS — a Dataset object or a filename
options — return of gdal.InfoOptions(), string or array of strings
# -*- coding: utf-8 -*-
""" /*************************************************************************** Translate.py:A:数据类型转换(单字节、双字节、浮点等)读取配置文件,获取对应的数据类型gdal.Translate(newfilename,self.fileName,outputType=list2[list1.index(parameter)])B: BIP、BIL、BSQ格式相互转换GTiff格式数据中,设置INTERLEAVE=BAND or PIXEL ;只能设置BSQ和BIPENVI格式的数据集,可设置数据组织类型,INTERLEAVE为"BIL,BIP,BSQ"@version <1.1> 2018-04-13 Wujd : Created. ***************************************************************************/ """
from osgeo import gdalclass ChangeOrganization:def __init__(self, infilename):"""初始化变量:param infilename:"""self.fileName = infilenameself._get_rasterinfo()def _get_rasterinfo(self):"""获取栅格数据的行列数,波段数,放射矩阵,投影信息:return:@version <1.1> 2018-04-2 Wujd : Created."""self.dataset = gdal.Open(self.fileName)self.im_width = self.dataset.RasterXSizeself.im_height = self.dataset.RasterYSizeself.im_bands = self.dataset.RasterCountself.im_geotrans = self.dataset.GetGeoTransform()self.im_proj = self.dataset.GetProjection()self.im_data = self.dataset.ReadAsArray()def Translate_org(self,newfilename,orgparameter):"""Translate方法设置INTERLEAVE=BAND or PIXEL ;只能设置BSQ和BIP:param newfilename::param orgparameter::return:"""if orgparameter =="BSQ":gdal.Translate(newfilename, self.fileName, creationOptions=["INTERLEAVE=BAND"])else:gdal.Translate(newfilename, self.fileName, creationOptions=["INTERLEAVE=PIXEL"])return Truedef Translate_org_envi(self,newfilename,orgparameter):"""转换成ENVI格式,可以在INTERLEAVE= BSQ,BIL,BIP:param newfilename::param orgparameter::return:"""#判断数据类型if "int8" in self.im_data.dtype.name:datetype = gdal.GDT_Byteelif "int16" in self.im_data.dtype.name:datetype = gdal.GDT_UInt16else :datetype = gdal.GDT_Float32#创建ENVI格式的数据集,可直接设置INTERLEAVE为"BIL,BIP,BSQ"driver = gdal.GetDriverByName("ENVI")if orgparameter =="BSQ":dataset = driver.Create(newfilename, self.im_width, self.im_height, self.im_bands,datetype,options=["INTERLEAVE=BSQ"])elif orgparameter =="BIL":dataset = driver.Create(newfilename, self.im_width, self.im_height, self.im_bands, datetype,options=["INTERLEAVE=BIL"])elif orgparameter =="BIP":dataset = driver.Create(newfilename, self.im_width, self.im_height, self.im_bands, datetype,options=["INTERLEAVE=BIP"])dataset.SetGeoTransform(self.im_geotrans)dataset.SetProjection(self.im_proj)try:for i in range(self.im_bands):dataset.GetRasterBand(i+1).WriteArray(self.im_data[i])except BaseException as e:print("数据写入错误!"+str(e))return Trueclass ChangeDataType:def __init__(self, infilename):"""初始化变量:param infilename:"""self.fileName = infilenameself._get_rasterinfo()def _get_rasterinfo(self):"""获取栅格数据的行列数,波段数,放射矩阵,投影信息:return:@version <1.1> 2018-04-2 Wujd : Created."""self.dataset = gdal.Open(self.fileName)self.im_width = self.dataset.RasterXSizeself.im_height = self.dataset.RasterYSizeself.im_bands = self.dataset.RasterCountself.im_geotrans = self.dataset.GetGeoTransform()self.im_proj = self.dataset.GetProjection()self.im_data = self.dataset.ReadAsArray()def getTranslateDtype(self,newfilename,parameter="float32"):"""修改数据类型:param newfilename::param parameter::return:"""list1 = ["byte","uint8","uint16","int16","uint32","int32","float32","float64","cint16","cint32","cfloat32","cfloat64"]list2 = [gdal.GDT_Byte,gdal.GDT_Byte,gdal.GDT_UInt16,gdal.GDT_Int16,gdal.GDT_UInt32,gdal.GDT_Int32,gdal.GDT_Float32,gdal.GDT_Float64,gdal.GDT_CInt16,gdal.GDT_CInt32,gdal.GDT_CFloat32,gdal.GDT_CFloat64]#调用公用的配置文件方法# obj = rwconfig.rwconfig()# try:# datatype = obj.readconfig(parameter)# except ImportError:# pass## if datatype!=None:# gdal.Translate(newfilename,self.fileName,outputType=datatype)if parameter.lower() in list1:gdal.Translate(newfilename,self.fileName,outputType=list2[list1.index(parameter)])return True# if __name__=="__main__":
# path = r"F:\Wujd\0319Test\translate\GF2_clip_2000.tif"
# obj1 = ChangeOrganization(path)
# newpath = r"F:\Wujd\0319Test\translate\OutBand79"
# outResult=obj1.Translate_org_envi(newpath,"BIL")
# obj = ChangeDataType(path)
# obj.getTranslateDtype(newpath,"int16")
gdal 读取GTiff格式时,默认INTERLEAVE为PIXEL,(BIP),而且INTERLEAVE must be PIXEL or BAND
在转换使用envi格式进行转换,可以在INTERLEAVE转换成BIL,BIP,BSQ
http://www.gdal.org/frmt_gtiff.html
http://www.gdal.org/python/