如何使用GDAL设置GeoTIFF文件的“波段描述”选项/标签(gdalwarp / gdal_translate)

时间:2015-09-16 13:10:20

标签: gdal geotiff

有人知道如何使用GDAL更改或设置GeoTIFF文件的“描述”选项/标签吗?

要指定我的意思,这是gdalinfo从GeoTIFF文件返回并设置“描述”的示例:

 Band 1 Block=64x64 Type=UInt16, ColorInterp=Undefined
 Description = AVHRR Channel 1:  0.58  micrometers -- 0.68 micrometers
 Min=0.000 Max=814.000 
 Minimum=0.000, Maximum=814.000, Mean=113.177, StdDev=152.897
 Metadata:
    LAYER_TYPE=athematic
    STATISTICS_MAXIMUM=814
    STATISTICS_MEAN=113.17657236931
    STATISTICS_MINIMUM=0
    STATISTICS_STDDEV=152.89720574652

在示例中,您可以看到:描述= AVHRR通道1:0.58微米 - 0.68微米

如何使用GDAL设置此参数?

4 个答案:

答案 0 :(得分:6)

在Python中,您可以像这样设置乐队描述:

from osgeo import gdal, osr
import numpy

# Define output image name, size and projection info:
OutputImage = 'test.tif'
SizeX = 20
SizeY = 20
CellSize = 1
X_Min = 563220.0
Y_Max = 699110.0
N_Bands = 10
srs = osr.SpatialReference()
srs.ImportFromEPSG(2157)
srs = srs.ExportToWkt()
GeoTransform = (X_Min, CellSize, 0, Y_Max, 0, -CellSize)

# Create the output image:
Driver = gdal.GetDriverByName('GTiff')
Raster = Driver.Create(OutputImage, SizeX, SizeY, N_Bands, 2) # Datatype = 2 same as gdal.GDT_UInt16
Raster.SetProjection(srs)
Raster.SetGeoTransform(GeoTransform)

# Iterate over each band
for band in range(N_Bands):
    BandNumber = band + 1
    BandName = 'SomeBandName '+ str(BandNumber).zfill(3)
    RasterBand = Raster.GetRasterBand(BandNumber)
    RasterBand.SetNoDataValue(0)
    RasterBand.SetDescription(BandName) # This sets the band name!
    RasterBand.WriteArray(numpy.ones((SizeX, SizeY)))

# close the output image
Raster = None
print("Done.")

不幸的是,我不确定ArcGIS或QGIS是否能够读取波段描述。但是,乐队名称在Tuiview中清晰可见: enter image description here

答案 1 :(得分:1)

GDAL包含一个名为gdal_edit.py的python应用程序,可用于修改文件的元数据。我不熟悉您所指的“描述”字段,但此工具应该是要使用的工具。

以下是手册页:gdal_edit.py

以下是使用从USGS Earth-Explorer下载的正射影像的示例脚本。

#!/bin/sh

#  Image to modify
IMAGE_PATH='11skd505395.tif'

#  Field to modify
IMAGE_FIELD='TIFFTAG_IMAGEDESCRIPTION'

# Print the tiff image description tag
gdalinfo $IMAGE_PATH | grep $IMAGE_FIELD

#  Change the Field
CMD="gdal_edit.py -mo ${IMAGE_FIELD}='Lake-Tahoe' $IMAGE_PATH"
echo $CMD
$CMD

#  Print the new field value
gdalinfo $IMAGE_PATH | grep $IMAGE_FIELD

输出

$ ./gdal-script.py 
TIFFTAG_IMAGEDESCRIPTION=OrthoVista
gdal_edit.py -mo TIFFTAG_IMAGEDESCRIPTION='Lake-Tahoe' 11skd505395.tif
TIFFTAG_IMAGEDESCRIPTION='Lake-Tahoe'

这是另一个应该提供有用信息的链接。

https://gis.stackexchange.com/questions/111610/how-to-overwrite-metadata-in-a-tif-file-with-gdal

答案 2 :(得分:0)

带有-mo标志的

gdal_edit.py可用于编辑乐队描述,乐队的编号从1开始:

gdal_edit.py -mo BAND_1=AVHRR_Channel_1_p58_p68_um -mo BAND_2=AVHRR_Channel_2 avhrr.tif

我没有尝试使用特殊字符,但是如果使用正确的引号,那可能会起作用。

答案 3 :(得分:0)

这是一个单一用途的 Python 命令行脚本,用于就地编辑波段描述。

''' Set image band description to specified text'''
import os
import sys
from osgeo import gdal

gdal.UseExceptions()

if len(sys.argv) < 4:
    print(f"Usage: {sys.argv[0]} [in_file] [band#] [text]")
    sys.exit(1)

infile = sys.argv[1]        # source filename and path
inband = int(sys.argv[2])   # source band number
descrip = sys.argv[3]        # description text

data_in = gdal.Open(infile, gdal.GA_Update)
band_in = data_in.GetRasterBand(inband)
old_descrip = band_in.GetDescription()
band_in.SetDescription(descrip)
new_descrip = band_in.GetDescription()

# de-reference the datasets, which triggers gdal to save
data_in = None
data_out = None

print(f"Description was: {old_descrip}")
print(f"Description now: {new_descrip}")

正在使用中:

$ python scripts\gdal-edit-band-desc.py test-edit.tif 1 "Red please"
Description was:
Description now: Red please

$ gdal-edit-band-desc test-edit.tif 1 "Red please also"

$ python t:\ENV.558\scripts\gdal-edit-band-desc.py test-edit.tif 1 "Red please also"
Description was: Red please
Description now: Red please also

正确地应该将它添加到 gdal_edit.py 中,但我不知道是否可以直接添加它。