0

I have an NDVI tif file that was generated from Sentinel 2A data using the gdal_calc method. I now want to add a red-green colour table to this file so that it matches the image below. I have managed to do this using QGIS and now want to replicate it using gdal.

I tried adding the --color-table=<file path.txt> parameter to the gdal_calc command, but this returned an error: ERROR 6: ndvi.tif, band 1: SetColorTable() only supported for Byte or UInt16 bands in TIFF format. My original command to create the ndvi is:

docker run --rm -v /Users:/Users osgeo/gdal:alpine-normal-latest gdal_calc.py --calc="((B.astype(float)-A.astype(float))/(B.astype(float)+A.astype(float)))" -A /Users/hyprstack/gdal-test/SAMPLE-DATA/GRANULE/L1C_T30UXD_A031022_20210531T111450/IMG_DATA/T30UXD_20210531T110621_B04.jp2 -B /Users/hyprstack/gdal-test/SAMPLE-DATA/GRANULE/L1C_T30UXD_A031022_20210531T111450/IMG_DATA/T30UXD_20210531T110621_B08.jp2 --outfile=/Users/hyprstack/gdal-test/ndvi.tif --type=Float32 --color-table=/Users/hyprstack/gdal-test/color_map.txt --overwrite

Output of the NDVI:

enter image description here

Desired output with color:

enter image description here

This is the exported colour table file from QGIS:

# QGIS Generated Color Map Export File
INTERPOLATION:INTERPOLATED
-1,215,25,28,255,-1.0000
-0.5,253,174,97,255,-0.5000
0,255,255,192,255,0.0000
0.5,166,217,106,255,0.5000
1,26,150,65,255,1.0000

How can I achieve this with gdal?

hyprstack
  • 111
  • 4

1 Answers1

0

Found an answer here too!

However I had to correct my colour table file to:

# QGIS Generated Color Map Export File
-1,215,25,28,255,-1.0000
-0.5,253,174,97,255,-0.5000
0,255,255,192,255,0.0000
0.5,166,217,106,255,0.5000
1,26,150,65,255,1.0000

And then ran the command:

docker run --rm -v /Users:/Users osgeo/gdal:alpine-normal-latest gdaldem color-relief /Users/hyprstack/gdal-test/ndvi.tif /Users/hyprstack/gdal-test/color_map.txt /Users/hyprstack/gdal-test/ndvi_color.tif

hyprstack
  • 111
  • 4