Hi
I am trying to run the following code please
but getting this error Error in .local(x, y, ...) : extents do not overlap
how to resolve this issue
Thanks
# Extract Black Marble Data
# Extract Black Marble Data
# Download Monthly Black Marble
rm(list = ls())
renv::restore()
pacman::p_load(tidyverse,
rgdal,
viridis,
readstata13,
dplyr,
data.table,
raster,
stargazer,
stringdist,
tmaptools,
stringr,
geosphere,
rgeos,
haven,
ggmap,
sf,
sp,
glmnet,
rgeos,
caret,
mltest,
RANN,
lubridate,
jsonlite,
httr,
curl,
ggpmisc,
haven,
sjmisc,
dbscan,
ggplot2,
spatialEco,
geosphere,
radiant.data,
readxl,
mclust,
missMDA,
DescTools,
furrr,
countrycode,
FactoMineR,
progressr,
ggmap,
ggridges,
ggpubr,
xgboost,
WDI,
scales,
ggExtra,
ggrepel,
ggcorrplot,
rnaturalearth,
ggthemes,
gghalves,
ggtext,
ggsignif,
LiblineaR,
caret,
exactextractr)
github_dir = "E:/Big Data Poverty Estimation/"
source(file.path(github_dir, "Functions", "functions.R"))
source("https://raw.githubusercontent.com/ramarty/download_blackmarble/main/R/download_blackmarble.R")
source("https://raw.githubusercontent.com/ramarty/fast-functions/master/R/functions_in_chunks.R")
source("https://raw.githubusercontent.com/ramarty/rSocialWatcher/52eede6cf561a74584503846eb78ee8bc8fa780b/R/main.R")
BEARER <- "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJlbWFpbF9hZGRyZXNzIjoibXJhbXphbkBjbGFya3UuZWR1IiwiaXNzIjoiQVBTIE9BdXRoMiBBdXRoZW50aWNhdG9yIiwiaWF0IjoxNzEzMTA3MDk1LCJuYmYiOjE3MTMxMDcwOTUsImV4cCI6MTg3MDc4NzA5NSwidWlkIjoicmFtemFuIiwidG9rZW5DcmVhdG9yIjoicmFtemFuIn0.-dPqpjfSYzUT27FOjjSkXBziFvqAncus8Y8E_2GXLtA"
data_dir <- "E:/Big Data Poverty Estimation/Data"
ntl_bm_dir <- file.path(data_dir, "NTL Black Marble")
# Parameters -------------------------------------------------------------------
BUFFER_OSM <- 5000
BUFFER_SATELLITE <- 2500
# Options:
# -- DHS
# -- DHS_nga_policy_experiment
# -- LSMS
SURVEY_NAME <- "DHS"
REPLACE_IF_EXTRACTED <- F
# Load data --------------------------------------------------------------------
df <- readRDS(file.path(data_dir, SURVEY_NAME, "FinalData", "Individual Datasets",
"survey_socioeconomic.Rds"))
# Delete existing files --------------------------------------------------------
if(F){
to_rm <- file.path(data_dir, SURVEY_NAME, "FinalData", "Individual Datasets",
"blackmarble") %>%
list.files(full.names = T) %>%
str_subset("bm_")
for(to_rm_i in to_rm) file.remove(to_rm_i)
}
# Function to Extract Black Marble Data ----------------------------------------
#country_code_i <- "IA"
#buffer_m <- 5000
extract_bm <- function(df_country,
year_i,
buffer_m){
## Project, buffer, then back to WGS
# Go back to WGS so don't have to project larger raster
coordinates(df_country) <- ~longitude+latitude
crs(df_country) <- CRS("+init=epsg:4326")
df_country <- geo.buffer_chunks(df_country, r = buffer_m, chunk_size = 100)
## Load globcover
if(year_i < 2012) year_i <- 2012
if(year_i > 2021) year_i <- 2021
r <- raster(file.path(ntl_bm_dir, "FinalData", "annual_rasters", paste0("bm_vnp46A4_",
year_i,
".tif")))
## Crop globcover
r_crop <- crop(r, bbox(df_country))
df_country$viirs_bm_mean <- exact_extract(r_crop, df_country, 'mean')
df_out <- df_country@data %>%
dplyr::select(uid, year, viirs_bm_mean)
return(df_out)
}
# Implement Function and Export ------------------------------------------------
for(buffer_i in c(BUFFER_SATELLITE, 1120, 3360)){
for(country_i in unique(df$country_code)){
df_country <- df[df$country_code %in% country_i,]
for(year_i in unique(df_country$year)){
print(paste0(country_i, " - ", buffer_i, " - ", year_i))
OUT_PATH <- file.path(data_dir, SURVEY_NAME, "FinalData", "Individual Datasets",
"blackmarble",
paste0("bm_", country_i, "_", buffer_i, "m_",year_i,".Rds"))
if(REPLACE_IF_EXTRACTED | !file.exists(OUT_PATH)){
df_bm_i <- extract_bm(df_country[df_country$year %in% year_i,],
year_i,
buffer_i)
saveRDS(df_bm_i, OUT_PATH)
}
}
}
}