-
Notifications
You must be signed in to change notification settings - Fork 32
/
NDVI.py
64 lines (36 loc) · 1.02 KB
/
NDVI.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# coding: utf-8
# In[ ]:
from osgeo import gdal, gdal_array
file1 = gdal.Open(str(path of green band))
file2 = gdal.Open(str(path of NIR band))
file1 = file1.ReadAsArray()
file2 = file2.ReadAsArray()
a, b = file1.shape
a,b
# In[56]:
import numpy as np
final_arr = np.zeros((a, b))
# print(final_arr)
# print(final_arr)
for i in range(a):
for j in range(b):
final_arr[i][j] = (file2[i][j] - file1[i][j])*100/(file1[i][j] + file2[i][j])#-100 to +100
# In[58]:
inRaster = str(path of green band)
inDS=gdal.Open(inRaster,1)
geoTransform = inDS.GetGeoTransform()
band=inDS.GetRasterBand(1)
datatype=band.DataType
proj = inDS.GetProjection()
# In[59]:
outRaster = str('path to store the NDVI file')
driver=inDS.GetDriver()
outDS = driver.Create(outRaster, b,a, 1,datatype)
geoTransform = inDS.GetGeoTransform()
# In[60]:
outDS.SetGeoTransform(geoTransform)
proj = inDS.GetProjection()
outDS.SetProjection(proj)
outBand = outDS.GetRasterBand(1)
outBand.WriteArray(final_arr,0,0)
outDS=None