generated from worldbank/template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from datapartnership/ntl
Ntl
- Loading branch information
Showing
43 changed files
with
20,936 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Download Nighttime Lights | ||
|
||
## Bearer token | ||
bearer <- read.csv("/Users/rmarty/Library/CloudStorage/OneDrive-WBG/Webscraping API Keys/bearer_bm.csv") %>% | ||
pull(token) | ||
|
||
## Define ROI | ||
roi_sf <- read_sf(file.path(boundaries_dir, "ner_adm_ignn_20230720_ab_shp", | ||
"NER_admbnda_adm0_IGNN_20230720.shp")) | ||
|
||
## Download data | ||
annual_r <- bm_raster(roi_sf = roi_sf, | ||
product_id = "VNP46A4", | ||
date = 2012:2023, | ||
bearer = bearer, | ||
output_location_type = "file", | ||
file_dir = file.path(ntl_dir, | ||
"individual_rasters", | ||
"annually")) | ||
|
||
month_r <- bm_raster(roi_sf = roi_sf, | ||
product_id = "VNP46A3", | ||
date = seq.Date(from = ymd("2012-01-01"), to = Sys.Date(), by = "month"), | ||
bearer = bearer, | ||
output_location_type = "file", | ||
file_dir = file.path(ntl_dir, | ||
"individual_rasters", | ||
"monthly")) | ||
|
||
day_r <- bm_raster(roi_sf = roi_sf, | ||
product_id = "VNP46A2", | ||
date = seq.Date(from = ymd("2023-01-01"), to = Sys.Date(), by = "day"), | ||
bearer = bearer, | ||
output_location_type = "file", | ||
file_dir = file.path(ntl_dir, | ||
"individual_rasters", | ||
"daily")) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
# Extract data | ||
|
||
AGG_FUNS <- c("mean", "sum") | ||
|
||
# Load ROIs -------------------------------------------------------------------- | ||
adm0_sf <- read_sf(file.path(boundaries_dir, "ner_adm_ignn_20230720_ab_shp", | ||
"NER_admbnda_adm0_IGNN_20230720.shp")) | ||
adm1_sf <- read_sf(file.path(boundaries_dir, "ner_adm_ignn_20230720_ab_shp", | ||
"NER_admbnda_adm1_IGNN_20230720.shp")) | ||
adm2_sf <- read_sf(file.path(boundaries_dir, "ner_adm_ignn_20230720_ab_shp", | ||
"NER_admbnda_adm1_IGNN_20230720.shp")) | ||
adm3_sf <- read_sf(file.path(boundaries_dir, "ner_adm_ignn_20230720_ab_shp", | ||
"NER_admbnda_adm1_IGNN_20230720.shp")) | ||
|
||
city_df <- read.csv(file.path(city_dir, "niger_cities.csv")) | ||
city_sf <- st_as_sf(city_df, coords = c("longitude", "latitude"), crs = 4326) | ||
city_buff_sf <- city_sf %>% st_buffer(dist = 10*1000) | ||
|
||
# Extract individual files ----------------------------------------------------- | ||
|
||
#### Loop through unit | ||
for(unit in c("adm0", "adm1", "adm2", "adm3", "city")){ | ||
|
||
dir.create(file.path(ntl_dir, "aggregated_individual", unit)) | ||
|
||
if(unit == "adm0") unit_sf <- adm0_sf | ||
if(unit == "adm1") unit_sf <- adm1_sf | ||
if(unit == "adm2") unit_sf <- adm2_sf | ||
if(unit == "adm3") unit_sf <- adm3_sf | ||
if(unit == "city") unit_sf <- city_buff_sf | ||
|
||
#### Loop through time period | ||
for(time_period in c("annually", "monthly", "daily")){ | ||
|
||
dir.create(file.path(ntl_dir, "aggregated_individual", unit, time_period)) | ||
|
||
files_vec <- file.path(ntl_dir, | ||
"individual_rasters", | ||
time_period) %>% | ||
list.files(full.names = T) | ||
|
||
#### Loop through dates | ||
for(file_i in files_vec){ | ||
|
||
date_i <- file_i %>% | ||
str_replace_all(".*qflag_t", "") %>% | ||
str_replace_all(".tif", "") | ||
|
||
OUT_DIR <- file.path(ntl_dir, "aggregated_individual", unit, time_period, | ||
paste0(date_i, ".Rds")) | ||
|
||
#### Extract data | ||
if(!file.exists(OUT_DIR)){ | ||
|
||
r <- terra::rast(file_i) | ||
|
||
ntl_df <- exact_extract(r, unit_sf, fun = AGG_FUNS) | ||
names(ntl_df) <- paste0("ntl_", names(ntl_df)) | ||
ntl_df$date <- date_i | ||
|
||
unit_df <- unit_sf %>% | ||
st_drop_geometry() | ||
unit_df$date <- NULL | ||
|
||
ntl_df <- bind_cols(unit_df, ntl_df) | ||
|
||
#### Cleanup dates | ||
if(time_period == "annually"){ | ||
|
||
ntl_df$date <- ntl_df$date %>% as.numeric() | ||
|
||
} else if(time_period == "monthly"){ | ||
|
||
ntl_df$date <- paste0(ntl_df$date, "-01") %>% | ||
str_replace_all("_", "-") %>% | ||
ymd() | ||
|
||
} else if(time_period == "daily"){ | ||
|
||
ntl_df$date <- ntl_df$date %>% | ||
str_replace_all("_", "-") %>% | ||
ymd() | ||
|
||
} | ||
|
||
saveRDS(ntl_df, OUT_DIR) | ||
|
||
} | ||
} | ||
} | ||
} | ||
|
||
# Append files ----------------------------------------------------------------- | ||
for(unit in c("adm0", "adm1", "adm2", "adm3", "city")){ | ||
for(time_period in c("annually", "monthly", "daily")){ | ||
|
||
ntl_df <- file.path(ntl_dir, "aggregated_individual", unit, time_period) %>% | ||
list.files(full.names = T) %>% | ||
map_df(readRDS) | ||
|
||
saveRDS(ntl_df, file.path(ntl_dir, "aggregated_appended", | ||
paste0(unit, "_", time_period, ".Rds"))) | ||
|
||
write_dta(ntl_df, file.path(ntl_dir, "aggregated_appended", | ||
paste0(unit, "_", time_period, ".dta"))) | ||
|
||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Nighttime Lights |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Nighttime Lights: Main Script | ||
|
||
# Filepaths -------------------------------------------------------------------- | ||
git_dir <- file.path("/Users", "rmarty", "Library", "CloudStorage", "OneDrive-WBG", "Documents", | ||
"GitHub", "niger-economic-monitoring", "notebooks", "nighttime-lights") | ||
|
||
data_dir <- file.path("/Users", "rmarty", "Library", "CloudStorage", "OneDrive-SharedLibraries-WBG", | ||
"Development Data Partnership - Niger Economic Monitor", "Data") | ||
|
||
boundaries_dir <- file.path(data_dir, "Boundaries") | ||
ntl_dir <- file.path(data_dir, "Nighttime Lights") | ||
city_dir <- file.path(data_dir, "Cities") | ||
|
||
# Packages --------------------------------------------------------------------- | ||
library(dplyr) | ||
library(lubridate) | ||
library(sf) | ||
library(ggplot2) | ||
library(leaflet) | ||
library(haven) | ||
library(terra) | ||
library(purrr) | ||
library(stringr) | ||
library(exactextractr) | ||
library(blackmarbler) |
Oops, something went wrong.