如何获取geotiff堆栈中的乐队名称?

发布于 2025-01-13 06:32:33 字数 486 浏览 3 评论 0原文

从像 'NDVI_TS.tif' 这样的文件中的 geotiff(NDVI 时间序列)堆栈中,我想获取单独的波段名称。例如:“频段 086:20190803T004719”。例如,当我将堆栈加载到 QGIS 中时,我可以看到这一点。我需要追溯名称中的日期,在上面的示例中为 2019-08-03。但是,我找不到从 Python 访问它的方法。我可以通过索引访问这些乐队,但这并不能帮助我找到它们来自哪个日期。

from osgeo import gdal

NDVI = gdal.Open('NDVI_TS.tif', gdal.GA_ReadOnly) 
#for example I can get data from band 86 as and array with:
band86 = NDVI.GetRasterBand(86).ReadAsArray()

我觉得应该有一些简单的解决方案,但未能找到。

From an stack of geotiff (time-series of NDVI) in a file like 'NDVI_TS.tif' I want to get individual band names. For example: 'Band 086: 20190803T004719'. I can see that when I load the stack into QGIS for example. I need to trace back the dates in the name, in the example above would be 2019-08-03. However, I can't find a way to access it from Python. I can access the bands by index but that doesn't help me finding from which date they are from.

from osgeo import gdal

NDVI = gdal.Open('NDVI_TS.tif', gdal.GA_ReadOnly) 
#for example I can get data from band 86 as and array with:
band86 = NDVI.GetRasterBand(86).ReadAsArray()

I feel there should be some easy solution for this but failed to find.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秋叶绚丽 2025-01-20 06:32:33

我不知道这是否有效,但这是我解决这个问题的方法(仅用小图像进行测试):

bands = {NDVI.GetRasterBand(i).GetDescription(): i for i in range(1, NDVI.RasterCount + 1)}

您会得到一本带有乐队名称(您在 QGIS 中看到的)及其各自索引的字典。

{ ...,
 'Band 086: 20190803T004719': 86,
 ...
}

您可以创建一个小函数来解析乐队名称并根据需要获取日期(没有正确测试):

import datetime
import re

def string2date(string):
   date_string = re.match('.* (.*)T', string).group(1)
   return datetime.datetime.strptime(date_string, '%Y%m%d').strftime('%Y-%m-%d')

然后您可以应用到之前的字典:

bands = {string2date(NDVI.GetRasterBand(i).GetDescription()): i for i in range(1, NDVI.RasterCount + 1)}

希望它有帮助

I don't know if this is efficient, but is the way I solve this (only tested with small images):

bands = {NDVI.GetRasterBand(i).GetDescription(): i for i in range(1, NDVI.RasterCount + 1)}

You get a dictionary with the band name (the one you see in QGIS) and their respective index.

{ ...,
 'Band 086: 20190803T004719': 86,
 ...
}

You can create a small function to parse the band name and get the dates as you want (didn't test it properly):

import datetime
import re

def string2date(string):
   date_string = re.match('.* (.*)T', string).group(1)
   return datetime.datetime.strptime(date_string, '%Y%m%d').strftime('%Y-%m-%d')

And then you can apply to the previous dict:

bands = {string2date(NDVI.GetRasterBand(i).GetDescription()): i for i in range(1, NDVI.RasterCount + 1)}

Hope it helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文