采用GDAL批量波段运算计算植被指数0基础教程

采用GDAL批量波段运算计算植被指数0基础教程

1. 引言

在传统的遥感数据处理方法中,通常使用ArcGis或ENVI软件进行波段运算。然而,这些软件在处理大量数据时往往效率低下。有没有一种方法可以批量进行波段运算,一下子计算几十个植被指数,并自动保存计算后的指标影像到相应目录下呢?答案是肯定的!本文将介绍如何使用GDAL库实现这一目标。

2. GDAL简介

GDAL(Geospatial Data Abstraction Library)是一个用于处理栅格数据和矢量数据的开源库,是地理信息系统(GIS)和遥感领域的重要工具之一。GDAL的详细介绍及其安装可以查看这篇文章:GDAL介绍及安装

3. 植被指数计算公式

这里列出了要计算的23种植被指数及其计算公式:

  • DVI 为差值植被指数;
  • EVI 为增强植被指数;
  • GCVI 为绿光归一化差值植被指数;
  • GNDVI 为绿光归一化差值植被指数;
  • ISR1, ISR2 为短波红外比值植被指数;
  • LSWI 为地表水分指数;
  • MSAVI 为修正型土壤调节植被指数;
  • NDPI 为归一化差异物候指数;
  • NDVI 为归一化植被指数;
  • NDWI1, NDWI2, NDWI3 为归一化水体指数;
  • RENDVI1, RENDVI2 为红边归一化差值植被指数;
  • RENDWI1, RENDWI2 为红边归一化水体指数;
  • SAVI 为土壤调节植被指数;
  • SR 为比值植被指数;
  • TR1, TR2 为比值生物学变量植被指数;
  • VRE1, VRE2 为红边植被指数。
植被指数计算公式
DVIDVI = nir - red
EVIEVI = 2.5 × (nir - red)/(nir + 6 × red - 7.5 × blue + 1)
GCVIGCVI = nir/green - 1
GNDVIGNDVI = (nir - green)/(nir + green)
ISR1ISR1 = nir/swir1
ISR2ISR2 = nir/swir2
LSWILSWI = (nir - swir1)/(nir + swir1)
MSAVIMSAVI = 1/2 × (2 × nir + 1 - ((2 × nir + 1)^2 - 8 × (nir - red))^(1/2))
NDPINDPI = (nir - (0.74 × red + 0.26 × swir1))/(nir + (0.74 × red + 0.26 × swir1))
NDVINDVI = (nir - red)/(nir + red)
NDWI1NDWI1 = (nir - swir1)/(nir + swir1)
NDWI2NDWI2 = (nir - swir2)/(nir + swir2)
NDWI3NDWI3 = (green - nir)/(green + nir)
RENDVI1RENDVI1 = (nir - red1)/(nir + red1)
RENDVI2RENDVI2 = (nir - red2)/(nir + red2)
RENDWI1RENDWI1 = (red1 - swir1)/(red1 + swir1)
RENDWI2RENDWI2 = (red2 - swir1)/(red2 + swir1)
SAVISAVI = (1 + L) × (nir - red)/(nir + red + 0.5)
SRSR = nir/red
TR1TR1 = (1/red1 - 1/red1) × nir
TR2TR2 = (1/red - 1/nir) × red2
VRE1VRE1 = nir/red1
VRE2VRE2 = nir/red2

4. 批量计算植被指数

使用 gdal_calc.py 工具可以批量计算表格中各植被指数。以下是计算这些植被指数的命令示例:

python gdal_calc.py -A ronghe.tif --A_band=5 -B ronghe.tif --B_band=4 --outfile=dvi.tif --calc="A-B"

python gdal_calc.py -A ronghe.tif --A_band=5 -B ronghe.tif --B_band=4 -C ronghe.tif --C_band=2 --outfile=evi.tif --calc="2.5*((A-B)/(A+6*B-7.5*C+1))"

python gdal_calc.py -A ronghe.tif --A_band=5 -B ronghe.tif --B_band=3 --outfile=gcvi.tif --calc="(A/B)-1"

python gdal_calc.py -A ronghe.tif --A_band=5 -B ronghe.tif --B_band=3 --outfile=gndvi.tif --calc="(A-B)/(A+B)"

python gdal_calc.py -A ronghe.tif --A_band=5 -B ronghe.tif --B_band=6 --outfile=isr1.tif --calc="A/B"

python gdal_calc.py -A ronghe.tif --A_band=5 -B ronghe.tif --B_band=7 --outfile=isr2.tif --calc="A/B"

python gdal_calc.py -A ronghe.tif --A_band=5 -B ronghe.tif --B_band=6 --outfile=lswi.tif --calc="(A-B)/(A+B)"

python gdal_calc.py -A ronghe.tif --A_band=5 -B ronghe.tif --B_band=4 --outfile=msavi.tif --calc="0.5*(2*A+1-((2*A+1)**2-8*(A-B))**0.5)"

python gdal_calc.py -A ronghe.tif --A_band=5 -B ronghe.tif --B_band=4 -C ronghe.tif --C_band=6 --outfile=ndpi.tif --calc="(A-(0.74*B+0.26*C))/(A+(0.74*B+0.26*C))"

python gdal_calc.py -A ronghe.tif --A_band=5 -B ronghe.tif --B_band=4 --outfile=ndvi.tif --calc="(A-B)/(A+B)"

python gdal_calc.py -A ronghe.tif --A_band=5 -B ronghe.tif --B_band=6 --outfile=ndwi1.tif --calc="(A-B)/(A+B)"

python gdal_calc.py -A ronghe.tif --A_band=5 -B ronghe.tif --B_band=7 --outfile=ndwi2.tif --calc="(A-B)/(A+B)"

python gdal_calc.py -A ronghe.tif --A_band=3 -B ronghe.tif --B_band=5 --outfile=ndwi3.tif --calc="(A-B)/(A+B)"

python gdal_calc.py -A ronghe.tif --A_band=5 -B ronghe.tif --B_band=4 --outfile=rendvi1.tif --calc="(A-B)/(A+B)"

python gdal_calc.py -A ronghe.tif --A_band=5 -B ronghe.tif --B_band=4 --outfile=rendvi2.tif --calc="(A-B)/(A+B)"

python gdal_calc.py -A ronghe.tif --A_band=4 -B ronghe.tif --B_band=6 --outfile=rendwi1.tif --calc="(A-B)/(A+B)"

python gdal_calc.py -A ronghe.tif --A_band=4 -B ronghe.tif --B_band=6 --outfile=rendwi2.tif --calc="(A-B)/(A+B)"

python gdal_calc.py -A ronghe.tif --A_band=5 -B ronghe.tif --B_band=4 --outfile=savi.tif --calc="(1+0.5)*(A-B)/(A+B+0.5)"

python gdal_calc.py -A ronghe.tif --A_band=5 -B ronghe.tif --B_band=4 --outfile=sr.tif --calc="A/B"

python gdal_calc.py -A ronghe.tif --A_band=4 -B ronghe.tif --B_band=4 --C ronghe.tif --C_band=5 --outfile=tr1.tif --calc="(1/A-1/B)*C"

python gdal_calc.py -A ronghe.tif --A_band=4 -B ronghe.tif --B_band=5 -C ronghe.tif --C_band=4 --outfile=tr2.tif --calc="(1/A-1/B)*C"

python gdal_calc.py -A ronghe.tif --A_band=5 -B ronghe.tif --B_band=4 --outfile=vre1.tif --calc="A/B"

python gdal_calc.py -A ronghe.tif --A_band=5 -B ronghe.tif --B_band=4 --outfile=vre2.tif --calc="A/B"

这里如何快速生成这些指令呢?我将公式截图直接发给给GPT, 然后给了它一个示范指令如何构建,让其构建所有公式的指令,只需要简单对一下即可,一般不会错,省去了手敲指令的过程。

这些命令使用 gdal_calc.py 工具来计算表格中的各个植被指数,其中 A_bandB_bandC_band 分别对应 Landsat 8多光谱,这里我们将其改名成ronghe.tif (你可以改为你的影像名字)的不同波段。请根据实际数据文件调整命令中的文件名和波段编号。

5. 批处理脚本自动计算

手动运行每条命令会非常麻烦,因此可以将这些命令写入一个批处理文件(.bat),实现自动计算。

创建批处理文件
  1. 打开记事本,将上述命令内容复制进去。

  2. 保存文件,将文件名命名为run.bat

  3. 将gdal_calc.py文件和遥感影像文件放入同一文件夹下。

    gdal.py文件已经上传到百度网盘,需要可搜索关注作者微信公众号:Python与遥感 获取
    在这里插入图片描述

运行批处理文件
  1. 打开Anaconda Prompt。
  2. 输入以下命令进入相应的驱动器和目录:
    F:
    cd path\to\your\folder
    
  3. 输入并运行批处理文件:
    run.bat
    

运行后,批处理文件将自动依次执行每个命令,计算所有植被指数,并将结果保存到相应的文件中。

6. 结果展示

每条命令运行后,对应的植被指数会保存在文件夹中,黑窗里的数字到99就代表计算完了一个植被指数。只需等待一小会儿,即可计算完所有的植被指数计算。

7. 总结

通过本文介绍的方法,使用GDAL库可以高效地批量计算植被指数,大大提高了工作效率。批处理文件的使用使得整个过程更加自动化和简便。希望这篇0基础教程能帮助大家更好地掌握GDAL的使用方法。如果有任何问题或需要进一步的帮助,请在评论区留言。
欢迎关注我的公众号:python与遥感,或许里面有你想要的免费教程。
在这里插入图片描述

  • 10
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Python与遥感

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值