-
Notifications
You must be signed in to change notification settings - Fork 2
/
ProcessingEvaluationMonitoringCube.Rmd
154 lines (94 loc) · 4.26 KB
/
ProcessingEvaluationMonitoringCube.Rmd
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
---
title: "Processing and Evaluation of Monitoring Cube"
author: "Nick Jakuschona – [email protected], Tom Niers – [email protected]"
date: "24.3.2021"
output: html_document
---
```{r, out.width=600, echo=FALSE}
#Detect deforestation on a added month
# set variables
percentage= 0.1 # the quantile of reference data which should be used to detect deforestation
newMonth= "2019-09" # the month which should be proofed. Month must be deaseasonalized and saved. This can be ideally done by the PreprocessingMonitoringCube.Rmd file.
load("data/NDVIchanges.Rdata")
library("lubridate")
library("raster")
#calculate the NDVI decrease value at which we predict a deforestation
criticalValue=quantile(changes, percentage)
#Select NDVI values from 2 month ago, to detect changes
dat = as.Date(paste(newMonth,"-01",sep=""))
d1= dat%m-% months(2)
oldMonth = format(d1, "%Y-%m")
newFilename= paste0("data/MonitoringDeseasonalized/Des_", newMonth ,".tif")
rasterNew= raster(newFilename)
oldFilename= paste0("data/MonitoringDeseasonalized/Des_", oldMonth ,".tif")
rasterOld= raster(oldFilename)
# calculate the NDVI change
NDVIchange= rasterNew-rasterOld
#check where the NDVI decrease is over the critical value
overCritical= NDVIchange<=criticalValue
```
```{r, echo=FALSE}
#show NDVI map with detected deforestation
#divide by zero to create NA
criticalNA = overCritical/overCritical
plotFigure1= function(rasterNew, criticalNA){
plot(rasterNew)
plot(criticalNA, add=TRUE, legend=FALSE,col="blue")
}
plotFigure1(rasterNew, criticalNA)
```
```{r, echo=FALSE}
# Evaluation
#date, where deforestation monitoring data is available
# http://terrabrasilis.dpi.inpe.br/geonetwork/srv/eng/catalog.search#/metadata/b75b83db-8026-43f9-9537-ee1dfa308158
dateToEvaluate= "2019-08"
filenameDEF =paste0("data/deforestation/def",dateToEvaluate ,"RasterClipped.tif")
#inspect NDVI rasters one month before and after the deforestation was monitored
dat = as.Date(paste(dateToEvaluate,"-01",sep=""))
d1= dat%m+% months(1)
dat1String = format(d1, "%Y-%m")
d2= dat%m-% months(1)
dat2String = format(d2, "%Y-%m")
print(dat2String)
filenameNDVI1 = paste0("data/MonitoringDeseasonalized/Des_", dat1String ,".tif")
filenameNDVI2 = paste0("data/MonitoringDeseasonalized/Des_", dat2String ,".tif")
defRaster= raster(filenameDEF)
NDVI1Raster=raster(filenameNDVI1)
NDVI2Raster=raster(filenameNDVI2)
ext= raster::extent(NDVI1Raster)
defRaster = setExtent(defRaster, ext, keepres = TRUE)
# calculate the NDVI change
diff= NDVI1Raster - NDVI2Raster
#check where the NDVI decrease is over the critical value
overCritical= diff<=criticalValue
```
```{r, echo=FALSE}
#Proof our detection against reference data
#change NA to 0
defRaster[is.na(defRaster[])] <- 0
#calculate correct and incorrect detections
correctTrue= ((overCritical==1) + (defRaster==1)) == 2
correctFalse= (overCritical==0) + (defRaster==0) ==2
incorrectTrue= (overCritical==1) + (defRaster==0)==2
incorrectFalse= (overCritical==0) + (defRaster==1)==2
correctTrueCount = cellStats(correctTrue, "sum")
correctTruePercentage = correctTrueCount / cellStats(defRaster==1, "sum")
correctFalseCount = cellStats(correctFalse, "sum")
correctFalsePercentage = correctFalseCount / cellStats(defRaster==0, "sum")
incorrectTrueCount = cellStats(incorrectTrue, "sum")
incorrectTruePercentage = cellStats(incorrectTrue, "sum") / cellStats(defRaster==0, "sum")
incorrectFalseCount = cellStats(incorrectFalse, "sum")
incorrectFalsePercentage = cellStats(incorrectFalse, "sum") / cellStats(defRaster==1, "sum")
```
```{r, echo=FALSE}
library(gridExtra)
x <- data.frame(row.names=c("Detected True", "Detected False"))
x[,1] <- c(paste0(floor(correctTruePercentage*10000)/100 ," %"), paste0(floor(incorrectFalsePercentage*10000)/100 ," %"))
x[,2] <- c(paste0(floor(incorrectTruePercentage*10000)/100 ," %"), paste0(floor(correctFalsePercentage*10000)/100 ," %"))
colnames(x) <- c("Actual True", "Actual False")
table <- tableGrob(x)
plotFigure2= function(table){
plot(table)
}
plotFigure2(table)
```