6

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

为了说明我的意思,这是一个从带有“描述”的 GeoTIFF 文件返回的 gdalinfo 示例:

 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

4 回答 4

7

在 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中清晰可见: 在此处输入图像描述

于 2017-11-20T20:54:44.460 回答
2

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

这是手册页: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

于 2015-10-18T20:14:25.980 回答
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

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

于 2020-01-30T00:39:16.680 回答
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但我不知道直接添加它是否安全。

于 2021-07-21T23:44:48.670 回答