-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIndicesCalculatorClass.py
More file actions
121 lines (117 loc) · 4.63 KB
/
Copy pathIndicesCalculatorClass.py
File metadata and controls
121 lines (117 loc) · 4.63 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Jun 17 11:42:40 2020
@author: julia
"""
import os
import gdal
import numpy as np
from primary_functions import save_array_as_gtiff, percentile_to_range
class IndicesCalculator:
def __init__(self, images_collection_dir):
self.images_collection_dir=images_collection_dir
files_names=os.listdir(self.images_collection_dir)
for file in files_names:
if 'grn' in file.lower().split('.')[0].split('_'):
self.green=gdal.Open(os.path.join(self.images_collection_dir, file))
print('GREEN: '+file)
if 'red' in file.lower().split('.')[0].split('_'):
self.red=gdal.Open(os.path.join(self.images_collection_dir, file))
print('RED: '+file)
if 'nir' in file.lower().split('.')[0].split('_'):
self.nir=gdal.Open(os.path.join(self.images_collection_dir, file))
print('NIR: '+file)
if 'mir' in file.lower().split('.')[0].split('_'):
self.mir=gdal.Open(os.path.join(self.images_collection_dir, file))
print('MIR: '+file)
if 'swir' in file.lower().split('.')[0].split('_'):
self.swir=gdal.Open(os.path.join(self.images_collection_dir, file))
print('SWIR: '+file)
def get_NDVI_as_array(self):
nodataValue=self.nir.GetRasterBand(1).GetNoDataValue()
RED=self.red.GetRasterBand(1).ReadAsArray().astype('float')
NIR=self.nir.GetRasterBand(1).ReadAsArray().astype('float')
a=NIR-RED
b=RED+NIR
NDVI_array=np.divide(a,b)
NDVI_array[NIR==nodataValue]=np.nan
RED=None
NIR=None
a=None
b=None
return percentile_to_range(NDVI_array)
def get_NDWI_as_array(self):
nodataValue=self.nir.GetRasterBand(1).GetNoDataValue()
GREEN=self.green.GetRasterBand(1).ReadAsArray().astype('float')
NIR=self.nir.GetRasterBand(1).ReadAsArray().astype('float')
a=GREEN-NIR
b=GREEN+NIR
NDWI_array=np.divide(a,b)
NDWI_array[NIR==nodataValue]=np.nan
GREEN=None
NIR=None
a=None
b=None
return percentile_to_range(NDWI_array)
def get_MNDWI_as_array(self):
nodataValue=self.green.GetRasterBand(1).GetNoDataValue()
GREEN=self.green.GetRasterBand(1).ReadAsArray().astype('float')
MIR=self.mir.GetRasterBand(1).ReadAsArray().astype('float')
a=GREEN-MIR
b=GREEN+MIR
MNDWI_array=np.divide(a,b)
MNDWI_array[GREEN==nodataValue]=np.nan
return percentile_to_range(MNDWI_array)
def get_WRI_as_array(self):
nodataValue=self.nir.GetRasterBand(1).GetNoDataValue()
GREEN=self.green.GetRasterBand(1).ReadAsArray().astype('float')
RED=self.red.GetRasterBand(1).ReadAsArray().astype('float')
NIR=self.nir.GetRasterBand(1).ReadAsArray().astype('float')
MIR=self.mir.GetRasterBand(1).ReadAsArray().astype('float')
a=GREEN+RED
GREEN=None
RED=None
b=NIR+MIR
NIR=None
MIR=None
WRI_array=np.divide(a,b)
WRI_array[NIR==nodataValue]=np.nan
a=None
b=None
return percentile_to_range(WRI_array)
def get_AWEI_as_array(self):
nodataValue=self.nir.GetRasterBand(1).GetNoDataValue()
GREEN=self.green.GetRasterBand(1).ReadAsArray().astype('float')
MIR=self.mir.GetRasterBand(1).ReadAsArray().astype('float')
NIR=self.nir.GetRasterBand(1).ReadAsArray().astype('float')
SWIR=self.swir.GetRasterBand(1).ReadAsArray().astype('float')
a=GREEN-MIR
GREEN=None
MIR=None
b=NIR*0.25
NIR=None
c=SWIR*2.75
SWIR=None
d=b+c
e=a*4
AWEI_array=e-d
AWEI_array[NIR==nodataValue]=np.nan
a=None
b=None
c=None
d=None
e=None
return percentile_to_range(AWEI_array)
def save_indices(self, output_folder):
array=self.get_NDVI_as_array()
save_array_as_gtiff(array, output_folder+'/NDVI.tif', dataset=self.nir)
array=self.get_NDWI_as_array()
save_array_as_gtiff(array, output_folder+'/NDWI.tif', dataset=self.nir)
#array=self.get_MNDWI_as_array()
#save_array_as_gtiff(array, output_folder+'/MNDWI.tif', dataset=self.nir)
array=self.get_WRI_as_array()
save_array_as_gtiff(array, output_folder+'/WRI.tif', dataset=self.nir)
array=self.get_AWEI_as_array()
save_array_as_gtiff(array, output_folder+'/AWEI.tif', dataset=self.nir)
array=None