-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNDWI_RasterArea.R
More file actions
225 lines (206 loc) · 8.13 KB
/
NDWI_RasterArea.R
File metadata and controls
225 lines (206 loc) · 8.13 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
# Limpopo Resilience Lab
# Sophia Bakar
# Remote Sensing Image Analysis with GDAL (Part 2)
# Measure area of water based on NDWI values within pre-programmed areas
#This work was supported by the United States Agency for International
# Development, Southern Africa Regional Mission, Fixed Amount Award
# 72067419FA00001. This work reflects the work of the authors and does not
# necessarily reflect the views of USAID or the United States Government.
# Further information is available at:
# www.duq.edu/limpopo
# https://github.com/LimpopoLab
library(rgdal) # must change to GDAL and PROJ: sf/stars/terra, by 2023
library(PROJ)
library(raster)
library(rgeos)
library(XML)
library(methods)
library(sp)
library(parallel)
library(MASS)
library(doParallel)
library(foreach)
library(stringr)
library(dplyr)
library(lubridate)
library(readr)
library(ggplot2)
im <- list.files(path = ".",
pattern = "harmonized_clip.tif$",
full.names = TRUE,
recursive = TRUE,
ignore.case=TRUE,
include.dirs = TRUE)
di <- array(NA, dim = length(im))
for (i in 1:length(im)) {
a <- str_split(im[i],"/")
b <- str_split(a[[1]][length(a[[1]])],"_")
c <- as.character(b[[1]][1])
d <- as.character(b[[1]][2])
f <- paste0(c,"T",d)
di[i] <- ymd_hms(f)
}
# Metadata List
md <- list.files("./",
pattern = "metadata_clip.xml$",
full.names = TRUE,
recursive = TRUE,
ignore.case=TRUE,
include.dirs = TRUE)
dm <- array(NA, dim = length(md))
for (i in 1:length(md)) {
a <- strsplit(md[i],"/")
b <- strsplit(a[[1]][length(a[[1]])],"_")
c <- as.character(b[[1]][1])
d <- as.character(b[[1]][2])
f <- paste0(c,"T",d)
dm[i] <- ymd_hms(f)
}
rm(a,b,c,d,f)
id <- array(NA, dim = length(im)) # will match metadata filenames to image filenames and dates
for (i in 1:length(im)) {
for (j in 1:length(md)) {
if (di[i]==dm[j]) { # if image date matches metadata date,
id[i] <- md[j] # store metadata filename matched to image filename and date
}
}
}
imagebank <- data.frame(di,im,id)
rm(di,dm,im,md,id,i,j)
imagebank <- imagebank %>%
rename(dt=di,md=id) %>%
filter(is.na(md)==FALSE) # will contain imagebank data frame with date (dt), image (im), and metadata (md)
output <- array(NA, dim = 5) # output array - will be filled in if data are valid
output[1] <- date(as_datetime(imagebank$dt[q]))
#Import raw Planet metadata to get the reflectance coefficients
fn <- imagebank$md[q]
fl <- xmlParse(fn)
rc <- setNames(xmlToDataFrame(node=getNodeSet(fl, "//ps:EarthObservation/gml:resultOf/ps:EarthObservationResult/ps:bandSpecificMetadata/ps:reflectanceCoefficient")),"reflectanceCoefficient")
dm <- as.matrix(rc)
# 1 Red
# 2 Green
# 3 Blue
# 4 Near infrared
rc2 <- as.numeric(dm[2]) # Green
rc4 <- as.numeric(dm[4]) # NIR
#rc <- c(1,1,1,1);
# Import raster image, crops to chosen extent
fn <- imagebank$im[q]
pic <- stack(fn)
# set extent from QGIS analysis:
# extent format (xmin,xmax,ymin,ymax)
## Sand River
e <- as(extent( 759337.75181152, 769953.81802531, 7456555.52600303, 7465089.435583732), 'SpatialPolygons')
crs(e) <- "+proj=utm +zone=35 +datum=WGS84" # may need negative y values
test <- as(extent(pic), 'SpatialPolygons') # Extent of image
crs(test) <- "+proj=utm +zone=35 +datum=WGS84"
if (gOverlaps(test,e)) { # returns TRUE if no point in spgeom2 (e, needed) is outside spgeom1 (test, image extent) # used to be (gWithin(e, test, byid = FALSE))
r <- crop(pic, e)
rm(pic) # remove rest of image from RAM
rbrick <- brick(r)
# calculate NDWI using the green (band 2) and nir (band 4) bands
ndwi <- ((rc2*r[[2]]) - (rc4*r[[4]])) / ((rc2*r[[2]]) + (rc4*r[[4]]))
# plot(ndwi) # for viewing during development
# To export cropped NDWI as a new file and create filename root
p <- strsplit(imagebank$im[q], "_3B_AnalyticMS.tif")
r <- strsplit(p[[1]], "/")
lr <- tolower(r[[1]])
len <- length(lr)
root <- lr[[len]]
rm(p,r,lr,len)
writeRaster(x = ndwi, ## this does not need to be done, just a nice record.
filename= paste(root, "cndwi.tif", sep="."),
format = "GTiff", # save as a tif, save as a FLOAT if not default, not integer
overwrite = TRUE) # OPTIONAL - be careful. This will OVERWRITE previous files.
output[2] <- root #for output file: root name of image
h = hist(ndwi, # built-in histogram function. To find values only. Plotting is at the end of this loop.
breaks=seq(-1,1,by=0.01),
plot=FALSE)
bins <- h$mids # positions number
v <- h$counts # counts integer
# Allocate arrays used in analysis
avg <- array(0, dim = c(200,10))
peaks <- array(0, dim = c(200,10))
nop <- array(0, dim = c(1,10))
for (w in 1:10){
# filter values (v=h$counts) with the averaging window size 2*w+1
for (k in (w+1):(200-w)){
avg[k,w] <- ((sum(v[(k-w):(k+w)]))/((2*w)+1))
}
# identify and number peaks
cnt <- 0
for (j in (w+1):(200-w)){
if ((avg[j-1,w])<(avg[j,w])){
if ((avg[j+1,w])<(avg[j,w])){
cnt <- (cnt+1)
peaks[j,w] <- cnt
nop[1,w] <- cnt
}
}
}
}
# set error values for the result vectors in case neither two nor three peaks are found:
threepeak <- -1 # revised error values so the histogram visualization is acceptable; however, after debugging, should go back to -9999
twopeak <- -1
for (w in 1:10){
# testing in three peaks
# due to the order of the w variable, only the 'smoothest' result will be kept
if ((nop[w])==3){
# finds the second and third peak
for (j in 1:200){
if ((peaks[j,w])==2){
sec <- j # stores the index of the second peak
}
if ((peaks[j,w])==3){
thr <- j # stores the index of the third peak
}
}
# finds minimum between second and third peak
m <- max(v) # create variable for minimum, initially set higher than any value
for (j in (sec):(thr)){
if ((avg[j,w])<m){
goal <- j
m <- avg[j,w]
}
}
threepeak <- (bins[(goal)])
}
# test in case exactly three peaks were not found
if ((nop[w])==2){
# find the position of the first and second (the only) peaks
for (j in 1:200){
if ((peaks[j,w])==1){
fst <- j # stores the index of the second peak
}
if ((peaks[j,w])==2){
sec <- j # stores the index of the third peak
}
}
# finds minimum between first and second peak
m <- max(v) # create variable for minimum, initially set higher than any value
for (j in (fst):(sec)){
if ((avg[j,w])<m){
goal <- j
m <- avg[j,w]
}
}
twopeak <- (bins[(goal)])
}
}
# Used in issue #1: Recheck histogram values. Will comment out after diagnostics
ndwi_values <- data.frame(ndwi@data@values)
ndwi_values <- rename(ndwi_values, data=ndwi.data.values)
output[3] <-threepeak #for output file: value for the edge of water (3 peak)
output[4] <-twopeak #for output file: value for the edge of water (2 peak)
# Testing three-peak and two-peak water threshold
# Three-peak is theoretically superior; however, is not always found or there are problems (e.g., =-1)
# when it is found. Test to determine if three-peak threshold is acceptable, otherwise, use two-peak.
if ((threepeak > -0.65) & (threepeak < 0.4)) {
ndwi_threshold <- threepeak
} else {
ndwi_threshold <- twopeak # consider QC on two-peaks and a default value with QC flag
}
output[5] <- ndwi_threshold
print(output)
}
no_of_pixels <- sum(ndwi[] >= ndwi_threshold, na.rm = TRUE)