Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calculate NDVI
#1
Hello
I am new to python.
I am trying to calculate the NDVI of two LANDSAT images with the following code :
from osgeo import gdal
import numpy as np
from numpy import *
data = gdal.Open("E:\\T29SPT_20181223T111451_B04_10m.tif")
red = data.ReadAsArray()
data = gdal.Open("E:\\T29SPT_20181223T111451_B08_10m.tif")
nir = data.ReadAsArray()

red = np.array(red, dtype = float)
nir = np.array(nir, dtype = float)

check = np.logical_and ( red > 1, nir > 1 )
ndvi = np.where ( check,  (nir - red ) / ( nir + red ) * 100, -999 ) 

geo = data.GetGeoTransform()
proj = data.GetProjection()
shape = red.shape        
driver = gdal.GetDriverByName("GTiff")
dst_ds = driver.Create( "E:\SIG GT\Python\GrosFichiers - taji\ndvi.tif", shape[1], shape[0], 1, gdal.GDT_Float32)
dst_ds.SetGeoTransform(geo)
dst_ds.SetProjection(proj)

dst_ds.GetRasterBand(1).WriteArray(ndvi)
dst_ds.FlushCache()
dst_ds = None  
but unfortunately it does not work
I have the following errors that appears
1) ndvi = np.where ( check, (nir - red ) / ( nir + red ) * 100, -999 )
RuntimeWarning: invalid value encountered in divide
2) AttributeError: 'NoneType' object has no attribute 'SetGeoTransform'

What do you think?
thanks
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020