Skip to content

Instantly share code, notes, and snippets.

@glw
Last active October 11, 2023 21:42
Show Gist options
  • Save glw/f8c26a08b8a8cd7cc0f55e25a1283028 to your computer and use it in GitHub Desktop.
Save glw/f8c26a08b8a8cd7cc0f55e25a1283028 to your computer and use it in GitHub Desktop.
NDVI calculation
# gets read from a database, hardwired here
NDVI_COLORMAP="006600 008800 00BB00 00FF00 CCFF00 FFFF00 FFCC00 FF8800 FF0000 EE0000 DD0000 CC0000 BB0000 AA0000 990000 880000 770000"
NIR_INPUT=nir_input.tif
NIR_OUTPUT=nir_output.tif
RED_INPUT=red_input.tif
RED_OUTPUT=red_output.tif
NDVI_IMAGE=new_ndvi.tif
NDVI_COLOR=ndvi_color.tif
python write_colormap_file.py "$NDVI_COLORMAP" custom.txt
echo "Creating NIR part of the NDVI color image for red filtered image..."
gdal_calc.py --calc="A" --A_band=1 --type=Float32 -A $NIR_INPUT --outfile $NIR_OUTPUT --NoDataValue=1.001 --overwrite
echo "Creating VIS part of the NDVI color image for red filtered image..."
gdal_calc.py --calc="A" --A_band=1 --type=Float32 -A $RED_INPUT --outfile $RED_OUTPUT --NoDataValue=1.001 --overwrite
echo "Doing the image math to create the NDVI image."
gdal_calc.py --calc="((A-B)/(A+B))" --type=Float32 -A $NIR_OUTPUT -B $RED_OUTPUT --outfile $NDVI_IMAGE --overwrite --NoDataValue=1.001
echo "Color mapping the NDVI image."
# this one creates the version with the alpha channel. It gets up uploaded.
gdaldem color-relief $NDVI_IMAGE custom.txt $NDVI_COLOR -alpha
# calculate the scaling percentage to make the full res output fit inside 65000
import argparse
if __name__ == '__main__':
p = argparse.ArgumentParser (description = 'Given a colormap string, this script will generate a file to use as the colormap for gdaldem.')
p.add_argument("colormap", type = str, help = 'Colormap String')
p.add_argument("file_location", type = str, help = 'Target location for the generated colormap file.')
args = p.parse_args()
f = open(args.file_location, 'w+')
vals = [-.941, -.824, -.706, -.588, -.471, -.353, -.235, -.118, 0, .118, .235, .353, .471, .588, .706, .824, .941]
# parse colormap string into list of int color values
colors = []
hexs = args.colormap.split(" ")
for h in reversed(hexs): # hexes come in positive to negative
red = int(h[0:2], 16)
green = int(h[2:4], 16)
blue = int(h[4:6], 16)
colors.append(str(red) + ' ' + str(green) + ' ' + str(blue))
f.write('-1 ' + colors[0] + ' 255\n')
for i in range(len(colors)):
f.write(str(vals[i]) + ' ' + colors[i] + ' 255\n')
f.write('1 ' + colors[len(colors)-1] + ' 255\n')
f.write('nv 0 0 0 0\n')
f.close()
f = open(args.file_location+"x", 'w+')
vals = [-.941, -.824, -.706, -.588, -.471, -.353, -.235, -.118, 0, .118, .235, .353, .471, .588, .706, .824, .941]
# parse colormap string into list of int color values
colors = []
hexs = args.colormap.split(" ")
for h in reversed(hexs): # hexes come in positive to negative
red = int(h[0:2], 16)
green = int(h[2:4], 16)
blue = int(h[4:6], 16)
colors.append(str(red) + ' ' + str(green) + ' ' + str(blue))
f.write('-1 ' + colors[0] + ' 255\n')
for i in range(len(colors)):
f.write(str(vals[i]) + ' ' + colors[i] + ' 255\n')
f.write('1 ' + colors[len(colors)-1] + ' 255\n')
f.write('nv 22 33 44 0\n')
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment