GTiff格式中,可以在Metadata域中可以写入自定义的元数据
关键函数:
SetMetadata(self, *args)
列表中的函数功能进行实验:
>>>import gdal
>>>path = r"F:\Wujd\0411贝叶斯融合\writeMetadata\HJ1A-CCD1-451-63-20171202-L20003285750-1.TIF"
>>>a = gdal.Open(path)
>>><osgeo.gdal.Dataset; proxy of <Swig Object of type 'GDALDatasetShadow *' at 0x000001CC5AD46F60> >
>>>b =gdal.Info(a)
>>>Driver: GTiff/GeoTIFF
Files: F:\Wujd\0411贝叶斯融合\writeMetadata\HJ1A-CCD1-451-63-20171202-L20003285750-1.TIFF:\Wujd\0411贝叶斯融合\writeMetadata\HJ1A-CCD1-451-63-20171202-L20003285750-1.TIF.aux.xml
Size is 16295, 14602
Coordinate System is:
PROJCS["WGS 84 / UTM zone 51N",GEOGCS["Ellipse_WGS_84",DATUM["unknown",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]]],PRIMEM["Greenwich",0],UNIT[,0.0174532925199433]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",123],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AUTHORITY["EPSG","32651"]]
Origin = (261071.946778000012273,4950917.280701999552548)
Pixel Size = (30.000000000000000,-30.000000000000000)
Metadata:AREA_OR_POINT=PointBandNames=0Bands=1BottomRightCoorX=0
...
>>>c=a.GetMetadataDomainList()
>>><class 'list'>: ['', 'IMAGE_STRUCTURE', '', 'DERIVED_SUBDATASETS']
>>>d=a.GetMetadata_Dict()
>>>{
'AREA_OR_POINT': 'Point', 'BandNames': '0', 'Bands': '1', 'BottomRightCoorX': '0', 'BottomRightCoorY': '0', 'CloudPercent': '0', 'DataRangeMax': '0', 'DataRangeMin': '0', 'DataType': '2', 'FilePathName': '0', 'FWHM': '0', 'GeodeticDatum': '0', 'HeightInPixels': '14602', 'IgoneValue': '0', 'PixelResolution': '30', 'ProduceTime': '0', 'ProductFormat': '0', 'ProductLevel': '0', 'ProjectionParameters': '0', 'RadianceScaleFactor': '0', 'ReflectanceScaleFactor': '0', 'SatelliteAzimuth': '0', 'SatelliteElevation': '0', 'SatelliteID': '0', 'Scale': '0', 'SensorID': '0', 'StorageFormat': '0', 'SunAzimuth': '100', 'SunElevation': '0', 'TIFFTAG_DATETIME': '2017-12-03 16:56:03', 'TopLeftCoorX': '0', 'TopLeftCoorY': '0', 'Wavelengths': '0', 'WidthInPixels': '16295'}
# -*- coding: utf-8 -*-
""" /*************************************************************************** CreateMetadata.py创建元数据字典,使用SetMetadata写入数据 ***************************************************************************/ """
from osgeo import gdalclass Createmetadata:def __init__(self,infilename):self.filename = infilenamedef createMetadata(self):ds = gdal.Open(self.filename)mList = {
"SatelliteID":'0',"SensorID":'0',"ProduceTime":'0',"ProductLevel":'0',"ProductFormat":'0',"Bands":'0',"WidthInPixels":'0',"HeightInPixels":'0',"CloudPercent":'0',"StorageFormat":'0',"DataType":'0',"IgoneValue":'0',"Scale":'0',"TopLeftCoorY":'0',"TopLeftCoorX":'0',"BottomRightCoorY":'0',"BottomRightCoorX":'0',"FilePathName":'0',"PixelResolution":'0',"ProjectionParameters":'0',"GeodeticDatum":'0',"BandNames":'0',"ReflectanceScaleFactor":'0',"RadianceScaleFactor":'0',"FWHM":'0',"Wavelengths":'0',"DataRangeMin":'0',"DataRangeMax":'0',"SunElevation":'0',"SunAzimuth":'100',"SatelliteElevation":'0',"SatelliteAzimuth":'0'}ds.SetMetadata(mList,"")print(gdal.Info(ds))print(ds.GetMetadataItem("SunAzimuth"))return Trueif __name__=="__main__":path = r"F:\Wujd\0411贝叶斯融合\writeMetadata\HJ1A-CCD1-451-63-20171202-L20003285750-1.TIF"obj = Createmetadata(path)obj.createMetadata()