Created
February 24, 2016 16:23
-
-
Save jgomezdans/6c11fef62eced9e131eb to your computer and use it in GitHub Desktop.
Simple script to calculate NDVI using pure GDAL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from osgeo import gdal | |
import numpy | |
g = gdal.Open ("baikal_subset.tif") | |
if g is None: | |
raise IOError, "Couldn't open baikal_subset.tif" | |
b3 = g.GetRasterBand(3).ReadAsArray().astype(np.float32) | |
b4 = g.GetRasterBand(4).ReadAsArray().astype(np.float32) | |
ndvi = (b4 - b3)/(b4 + b3) | |
drv = gdal.GetDriverByName ( "GTiff" ) | |
dst_ds = drv.Create ( "output_file.tif", g.RasterXSize, g.RasterYSize, 1, | |
gdal.GDT_Float32, options=["COMPRESS=LZW"] ) | |
dst_ds.GetRasterBand(1).WriteArray ( dst_ds.astype (np.float32) ) | |
dst_ds = None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Quick and easy thanks!