-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathndvi_calculator.py
58 lines (48 loc) · 1.63 KB
/
ndvi_calculator.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
import rasterio
from rasterio import plot
import matplotlib.pyplot as plt
import numpy as np
from osgeo import gdal
import os
infn = './input/ayuso_rededge.tif'
outfn = './output/ndvi.tif'
img = rasterio.open(infn)
plot.show(img)
def openRaster(fn, access=0):
ds = gdal.Open(fn, access)
if ds is None:
print("Error opening Raster dataset")
return ds
def getRasterBand(fn, band=1, access=0):
ds = openRaster(fn, access)
band = ds.GetRasterBand(band).ReadAsArray()
return band
def createRasterFromTemplate(fn, ds, data, ndv=-9999.0, driverFmt="GTiff"):
driver = gdal.GetDriverByName(driverFmt)
outds = driver.Create(fn, xsize=ds.RasterXSize, ysize=ds.RasterYSize, bands=1, eType=gdal.GDT_Float32)
outds.SetGeoTransform(ds.GetGeoTransform())
outds.SetProjection(ds.GetProjection())
outds.GetRasterBand(1).SetNoDataValue(ndv)
outds.GetRasterBand(1).WriteArray(data)
outds = None
ds = None
def ndvi(nirband, redband, ndv=-9999.0):
nirband[nirband < 0] = np.nan
nirband[nirband > 10000] = np.nan
nirband[redband < 0] = np.nan
nirband[redband > 10000] = np.nan
ndviband = (nirband-redband)/(nirband+redband)
ndviband[np.isnan(ndviband)] = ndv
return ndviband
# Plot redband vs nirband
redband = getRasterBand(infn, 3)
nirband = getRasterBand(infn, 5)
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12,6))
plot.show(redband, ax=ax1, cmap='Blues')
plot.show(nirband, ax=ax2, cmap='Blues')
fig.tight_layout()
plt.show()
ndviband = ndvi(nirband, redband)
createRasterFromTemplate(outfn, gdal.Open(infn), ndviband)
img = rasterio.open('./output/ndvi.tif')
plot.show(img)