-
Notifications
You must be signed in to change notification settings - Fork 0
/
R_Code.R
181 lines (151 loc) · 5.58 KB
/
R_Code.R
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
library(openintro) # for data
library(tidyverse) # for data wrangling and visualization
library(knitr) # for tables
library(broom) # for model summary
library(imputeTS)
library(dplyr)
library(kableExtra)
library(forecast)
library(psych)
library(viridis)
library(ggridges)
library('sf')
library(tibble)
library(lubridate)
# Downloading the EVI and NDVI
library(rgee)
library(reticulate)
#ee_install()
ee_check()
ee_Initialize("kalong",drive = TRUE) # initialize GEE,
#this will have you log in to Google Drive
#Load shape file
aoi <- read_sf('Ghana shp file/ROI/new_roi.shp')
aoi <- st_transform(aoi, st_crs(4326))
aoi.ee <- st_bbox(aoi) %>%
st_as_sfc() %>%
sf_as_ee() #Converts it to an Earth Engine Object
################################################################################
# #NDVI DATA
#
# getQABits <- function(image, qa) {
# # Convert binary (character) to decimal (little endian)
# qa <- sum(2^(which(rev(unlist(strsplit(as.character(qa), "")) == 1))-1))
# # Return a mask band image, giving the qa value.
# image$bitwiseAnd(qa)$lt(1)
# }
#
# ndvi_mod.clean <- function(img) {
# # Extract the NDVI band
# ndvi_values <- img$select("NDVI")
# # Extract the quality band
# ndvi_qa <- img$select("SummaryQA")
# # Select pixels to mask
# quality_mask <- getQABits(ndvi_qa, "11")
# # Mask pixels with value zero.
#
# #0.0001 is the MODIS Scale Factor
# ndvi_values$updateMask(quality_mask)$divide(ee$Image$constant(10000))
# }
#
# modis.ndvi <- ee$ImageCollection("MODIS/006/MOD13Q1"
# )$filter(ee$Filter$date('2000-01-01','2022-01-01'))$map(ndvi_mod.clean)
################################################################################
# EVI DATA
getQABits <- function(image, qa) {
# Convert binary (character) to decimal (little endian)
qa <- sum(2^(which(rev(unlist(strsplit(as.character(qa), "")) == 1))-1))
# Return a mask band image, giving the qa value.
image$bitwiseAnd(qa)$lt(1)
}
evi_mod.clean <- function(img) {
# Extract the NDVI band
evi_values <- img$select("EVI")
# Extract the quality band
evi_qa <- img$select("SummaryQA")
# Select pixels to mask
evi_quality_mask <- getQABits(evi_qa, "11")
# Mask pixels with value zero.
#0.0001 is the MODIS Scale Factor
evi_values$updateMask(evi_quality_mask)$divide(ee$Image$constant(10000))
}
modis.evi <- ee$ImageCollection("MODIS/006/MOD13Q1"
)$filter(ee$Filter$date('2000-01-01','2022-01-01'))$map(evi_mod.clean)
################################################################################
aoi.proj <- st_transform(aoi, st_crs(2392))
hex <- st_make_grid(x = aoi.proj, cellsize = 17280, square = FALSE) %>%
st_sf() %>%
rowid_to_column('hex_id')
hex <- hex[aoi.proj,]
{#This will take about 30 minutes #
# aoi.ndvi <- ee_extract(x = modis.ndvi, y = hex["hex_id"], sf = FALSE, scale = 250, fun = ee$Reducer$mean(), via = "drive", quiet = T)
# colnames(aoi.ndvi) <- c('hex_id', stringr::str_replace_all(substr(colnames(aoi.ndvi[, 2:ncol(aoi.ndvi)]), 2, 11), "_", "-"))
}
{ aoi.evi <- ee_extract(x = modis.evi, y = hex["hex_id"], sf = FALSE, scale = 250, fun = ee$Reducer$mean(), via = "drive", quiet = T)
colnames(aoi.evi) <- c('hex_id', stringr::str_replace_all(substr(colnames(aoi.evi [, 2:ncol(aoi.evi )]), 2, 11), "_", "-"))
}
write.csv(x = aoi.evi, file = "Data/aoi_evi.csv")
{
# EVI <- aoi.evi%>%
# pivot_longer(-hex_id,names_to = "Date",values_to = "EVI")%>%
# separate(Date,into = c("year","month","day"),sep = "-")%>%
# select(hex_id,year,month,EVI)}
# {Vegetation <- NDVI %>% full_join(EVI,by ="hex_id")
# Vegetation%>%
# mutate(month = month(as.Date(rownames(Vegetation))), year = year(as.Date(rownames(Vegetation)))) %>%
# group_by(year, month) %>%
# summarise(mean_evi = mean(evi, na.rm = T), .groups = "keep") %>%
# as.data.frame()
}
{
Dates <- data.frame(date = seq(as.Date('2000-01-01'), as.Date('2022-01-01'),"month"))
Dates$month <- month(Dates$date)
Dates$year <- year(Dates$date)
i = 1
}
{
#converting the data to a transposed data frame
NDVI <- data.frame(ndvi = t(aoi.ndvi[i, 2:ncol(aoi.ndvi)]))
EVI <- data.frame(ndvi = t(aoi.evi[i, 2:ncol(aoi.evi)]))
}
colnames(NDVI) <- c("NDVI")
colnames(EVI) <- c("EVI")
{
# write.csv(NDVI,"Data/NDVI.csv")
# write.csv(EVI,"Data/EVI.csv")
}
{
ndvi.trend <- data.frame(hex_id = aoi.ndvi$hex_id, na.NDVI = NA, NA_Values = NA, Trend = NA, P_value = NA, R_Squared = NA, Standard_Error = NA, Trend_Strength = NA, Seasonal_Strength = NA)
evi.trend <- data.frame(hex_id = aoi.evi$hex_id, na.EVI = NA, NA_Values = NA, Trend = NA, P_value = NA, R_Squared = NA, Standard_Error = NA, Trend_Strength = NA, Seasonal_Strength = NA)
na.NDVI <- length(NDVI[is.na(NDVI)])
ndvi.trend$na.NDVI[i] <- na.NDVI
na.EVI <- length(EVI[is.na(EVI)])
evi.trend$na.EVI[i] <- na.EVI
}
{
# NDVI <- NDVI %>%
# mutate(month =month(as.Date(rownames(NDVI))),year =year(as.Date(rownames(NDVI))))%>%
# group_by(year, month) %>%
# summarise(NDVI = mean(NDVI, na.rm = T), .groups = "keep") %>%
# as.data.frame()
#
# NDVI$date <- as.Date(paste0(NDVI$year, "-", NDVI$month, "-01"))
# dx <- Dates[!(Dates$date %in% NDVI$date),]
#
#
# dx$NDVI <- NA
# NDVI <- rbind(NDVI, dx) %>%
# arrange(date)
}
{
EVI <- EVI %>%
mutate(month =month(as.Date(rownames(EVI))),year =year(as.Date(rownames(EVI))))%>%
group_by(year, month) %>%
summarise(EVI = mean(EVI, na.rm = T), .groups = "keep") %>%
as.data.frame()
EVI$date <- as.Date(paste0(EVI$year, "-", EVI$month, "-01"))
dx <- Dates[!(Dates$date %in% EVI$date),]
dx$EVI <- NA
EVI <- rbind(EVI, dx) %>%
arrange(date)
}