forked from bleutner/RStoolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspectralIndices.cpp
More file actions
244 lines (228 loc) · 9.25 KB
/
spectralIndices.cpp
File metadata and controls
244 lines (228 loc) · 9.25 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include<Rcpp.h>
using namespace Rcpp;
//[[Rcpp::export]]
NumericMatrix spectralIndicesCpp(NumericMatrix x, CharacterVector indices,
const int redBand, const int blueBand, const int greenBand, const int nirBand,
const int redEdge1Band, const int redEdge2Band, const int redEdge3Band,
const int swir1Band, const int swir2Band, const int swir3Band,
int maskLayer, const int maskValue,
const double L, const double s, const double G, const double C1,
const double C2, double Levi, const double swir2ccc, const double swir2cdiff, const double sf) {
const int nind = indices.size();
const int nsamp = x.nrow();
const int nc = x.ncol();
NumericMatrix out(nsamp, nind);
NumericVector blue, green, red, redEdge1, redEdge2, redEdge3, nir, swir1, swir2, swir3;
// Apply mask layer
if(!IntegerVector::is_na(maskLayer)){
maskLayer-=1 ;
std::vector<int> m;
m.reserve(nsamp);
if(IntegerVector::is_na(maskValue)){
for(int i = 0; i < nsamp; i++) {
if (ISNAN(x(i, maskLayer))) m.push_back(i);
}
} else {
for(int i = 0; i < nsamp; i++) {
if (x(i, maskLayer) == maskValue) m.push_back(i);
}
}
for(int j = 0; j < nc; j++) {
if (j == maskLayer) continue;
for(int i = 0; i < (int)m.size(); i++) {
x(m[i],j) = NA_REAL;
}
}
}
if(blueBand != NA_INTEGER) blue = x(_,blueBand - 1) / sf;
if(greenBand != NA_INTEGER) green = x(_,greenBand - 1) / sf;
if(redBand != NA_INTEGER) red = x(_,redBand - 1) / sf;
if(redEdge1Band != NA_INTEGER) redEdge1 = x(_,redEdge1Band - 1) / sf;
if(redEdge2Band != NA_INTEGER) redEdge2 = x(_,redEdge2Band - 1) / sf;
if(redEdge3Band != NA_INTEGER) redEdge3 = x(_,redEdge3Band - 1) / sf;
if(nirBand != NA_INTEGER) nir = x(_,nirBand - 1) / sf;
if(swir1Band != NA_INTEGER) swir1 = x(_,swir1Band - 1) / sf;
if(swir2Band != NA_INTEGER) swir2 = x(_,swir2Band - 1) / sf;
if(swir3Band != NA_INTEGER) swir3 = x(_,swir3Band - 1) / sf;
for(int j = 0; j < nind; ++j) {
if(indices[j] == "DVI") {
// Difference vegetation index
out(_,j) = (s * nir - red);
out(_,j) = ifelse(is_na(out(_,j)), NA_REAL, out(_,j));
}
else if(indices[j] == "CTVI") {
// Corrected transformed vegetation index
// Perry and Lautenschlager 1984
NumericVector np = (nir-red)/(nir+red) + 0.5;
out(_,j) = np / sqrt(abs(np));
out(_,j) = ifelse(is_na(out(_,j)), NA_REAL, out(_,j));
}
else if(indices[j] == "CLG") {
// Green cholorphyl index
// Wu et al 2012
out(_,j) = (redEdge3)/(green)-1;
out(_,j) = ifelse(is_na(out(_,j)), NA_REAL, out(_,j));
}
else if(indices[j] == "CLRE") {
// RedEdge cholorphyl index
// Clevers and Gitelson 2013
out(_,j) = (redEdge3)/(redEdge1)-1;
out(_,j) = ifelse(is_na(out(_,j)), NA_REAL, out(_,j));
}
else if(indices[j] == "EVI") {
// Enhanced vegetation index
// Huete et al 1990
out(_,j) = G * ((nir - red) / (nir + C1 * red - C2 * blue + Levi));
out(_,j) = ifelse(is_na(out(_,j)) | (out(_,j) > 1.0) | (out(_,j) < -1.0) , NA_REAL, out(_,j));
}
else if(indices[j] == "EVI2") {
// Two-band Enhanced vegetation index
// Jiang et al 2008
out(_,j) = G * ((nir - red) / (nir + 2.4 * red ));
out(_,j) = ifelse(is_na(out(_,j)) | (out(_,j) > 1.0) | (out(_,j) < -1.0) , NA_REAL, out(_,j));
}
else if(indices[j] == "GEMI") {
out(_,j) = (((pow(nir, 2) - pow(red, 2)) * 2.0 + (nir * 1.5) + (red * 0.5) ) /
(nir + red + 0.5)) * (1 - ((((pow(nir,2) - pow(red,2)) * 2 + (nir * 1.5) + (red * 0.5) ) /
(nir + red + 0.5)) * 0.25)) - ((red - 0.125) / (1 - red));
out(_,j) = ifelse(is_na(out(_,j)), NA_REAL, out(_,j));
}
else if(indices[j] == "GNDVI") {
// green Normalized diff vegetation index: -> more sensitive to cholorphyll than ndvi
// Gitelson, A., and M. Merzlyak. "Remote Sensing of Chlorophyll Concentration in Higher Plant Leaves." Advances in Space Research 22 (1998): 689-692
out(_,j) = (nir - green)/( nir + green);
out(_,j) = ifelse(is_na(out(_,j)) | (out(_,j) > 1.0) | (out(_,j) < -1.0) , NA_REAL, out(_,j));
}
else if(indices[j] == "KNDVI") {
// kernel Normalized difference vegetation index
// Camps Valls (2021) A unified vegetation index forquantifying theterrestrial biosphere
// This implementation assumes sigma = mean(nir,red), which is the setting suggested by the authors
// Other values for sigma are not currently implemented.
out(_,j) = tanh(pow((nir - red) / (nir + red), 2));
out(_,j) = ifelse(is_na(out(_,j)) | (out(_,j) > 1.0) | (out(_,j) < 0), NA_REAL, out(_,j));
}
else if(indices[j] == "MNDWI") {
// Modified Normalised Difference Water Index
out(_,j) = (green-swir2) / (green+swir2);
out(_,j) = ifelse(is_na(out(_,j)) | (out(_,j) > 1.0) | (out(_,j) < -1.0) , NA_REAL, out(_,j));
}
else if(indices[j] == "MTCI") {
// Meris Terrestrial Chlorophyll Index
// Clevers and Gitelson 2013, Dash and Curran 2004
out(_,j) = (redEdge2-redEdge1) / (redEdge1-red);
out(_,j) = ifelse(is_na(out(_,j)), NA_REAL, out(_,j));
}
else if(indices[j] == "MCARI") {
// Modified Chlorophyll Absorption ratio index
// Daughtery et al. 2000
out(_,j) = (redEdge1 - red - redEdge1 + green) * (redEdge1 / red);
out(_,j) = ifelse(is_na(out(_,j)), NA_REAL, out(_,j));
}
else if(indices[j] == "MSAVI") {
// Modified soil adjusted vegetation index
out(_,j) = nir + 0.5 - (0.5 * sqrt(pow(2.0 * nir + 1.0, 2) - 8.0 * (nir - (2.0 * red))));
out(_,j) = ifelse(is_na(out(_,j)), NA_REAL, out(_,j));
}
else if(indices[j] == "MSAVI2") {
// Modified soil adjusted vegetation index 2
out(_,j) = (2.0 * nir + 1.0 - sqrt(pow(2.0 * nir + 1.0, 2) - 8.0 * (nir - red))) / 2.0;
out(_,j) = ifelse(is_na(out(_,j)), NA_REAL, out(_,j));
}
else if(indices[j] == "NDVIC") {
//Normalized difference vegetation index
out(_,j) = (nir - red) / (nir + red) * (1 - (swir2 - swir2ccc)/swir2cdiff);
out(_,j) = ifelse(is_na(out(_,j)) | (out(_,j) > 1.0) | (out(_,j) < -1.0), NA_REAL, out(_,j));
}
else if(indices[j] == "NBRI"){
// Normalised Burn Ratio Index
out(_,j) = (nir - swir3) / (nir + swir3);
out(_,j) = ifelse(is_na(out(_,j)) | (out(_,j) > 1.0) | (out(_,j) < -1.0), NA_REAL, out(_,j));
}
else if(indices[j] == "NDVI") {
//Normalized difference vegetation index
out(_,j) = (nir - red) / (nir + red);
out(_,j) = ifelse(is_na(out(_,j)) | (out(_,j) > 1.0) | (out(_,j) < -1.0), NA_REAL, out(_,j));
}
else if(indices[j] == "NDWI") {
// Normalized difference water index
// McFeeters 1996
out(_,j) = (green - nir)/(green + nir);
out(_,j) = ifelse(is_na(out(_,j)) | (out(_,j) > 1.0) | (out(_,j) < -1.0) , NA_REAL, out(_,j));
}
else if(indices[j] == "NDWI2") {
// Normalized difference water index (also known as Normalized Difference Moisture Index (NDBI)
// Gao 1996, Chen 2005
// a.k.a. LSWI
out(_,j) = (nir - swir2)/(nir + swir2);
out(_,j) = ifelse(is_na(out(_,j)) | (out(_,j) > 1.0) | (out(_,j) < -1.0) , NA_REAL, out(_,j));
}
else if(indices[j] == "NDREI1") {
// Normalized difference red edge index
// Gitelson and Merzlyak 1994
out(_,j) = (redEdge2 - redEdge1)/(redEdge2 + redEdge1);
out(_,j) = ifelse(is_na(out(_,j)) | (out(_,j) > 1.0) | (out(_,j) < -1.0) , NA_REAL, out(_,j));
}
else if(indices[j] == "NDREI2") {
// Normalized difference red edge index 2
// Barnes et al 2000
out(_,j) = (redEdge3 - redEdge1)/(redEdge3 + redEdge1);
out(_,j) = ifelse(is_na(out(_,j)) | (out(_,j) > 1.0) | (out(_,j) < -1.0) , NA_REAL, out(_,j));
}
else if(indices[j] == "NRVI") {
// Normalized Ratio Vegetation Index
// Baret and Guyot 1991
NumericVector rvi = red / nir;
out(_,j) = (rvi - 1.0)/(rvi + 1.0);
out(_,j) = ifelse(is_na(out(_,j)), NA_REAL, out(_,j));
}
else if(indices[j] == "REIP") {
// Ratio Vegetation Index
// Richardson and Wiegand 1977
out(_,j) = 0.705+0.35*((red+redEdge3)/(2-redEdge1))/(redEdge2-redEdge1) ;
out(_,j) = ifelse(is_na(out(_,j)), NA_REAL, out(_,j));
}
else if(indices[j] == "RVI") {
// Ratio Vegetation Index
// Richardson and Wiegand 1977
out(_,j) = red / nir;
out(_,j) = ifelse(is_na(out(_,j)), NA_REAL, out(_,j));
}
else if(indices[j] == "SATVI"){
// Soil adjusted total vegetation index
out(_,j) = ((swir2 - red) / (swir2 + red + L)) * (1.0 + L) - (swir3 / 2.0);
out(_,j) = ifelse(is_na(out(_,j)), NA_REAL, out(_,j));
}
else if(indices[j] == "SAVI") {
// Soil adjusted vegetation index
// Huete1988
out(_,j) = (nir - red) * (1.0 + L) / (nir + red + L);
out(_,j) = ifelse(is_na(out(_,j)), NA_REAL, out(_,j));
}
else if(indices[j] == "SLAVI") {
out(_,j) = nir / (red + swir2);
out(_,j) = ifelse(is_na(out(_,j)), NA_REAL, out(_,j));
}
else if(indices[j] == "SR") {
// Simple ratio index
out(_,j) = nir / red;
out(_,j) = ifelse(is_na(out(_,j)), NA_REAL, out(_,j));
}
else if(indices[j] == "TVI") {
// Transformed Vegetation Index
// Deering 1975
out(_,j) = sqrt((nir-red)/(nir+red) + 0.5);
out(_,j) = ifelse(is_na(out(_,j)) | (out(_,j) > sqrt(1.5)), NA_REAL, out(_,j));
}
else if(indices[j] == "TTVI") {
// Thiams Transformed Vegetation Index
// Thiam 1997
out(_,j) = sqrt(abs((nir-red)/(nir+red) + 0.5));
out(_,j) = ifelse(is_na(out(_,j)), NA_REAL, out(_,j));
}
else if(indices[j] == "WDVI") {
out(_,j) = nir - s * red;
out(_,j) = ifelse(is_na(out(_,j)), NA_REAL, out(_,j));
}
}
return out;
}