Commit 35d84ec8 authored by Facundo Muñoz's avatar Facundo Muñoz ®️
Browse files

WCAfrica cartography: move from workflow based on script and saved files to...

WCAfrica cartography: move from workflow based on script and saved files to drake targets and functions.
parent a544eab0
Pipeline #14883 passed with stage
in 8 minutes and 34 seconds
library(rgdal)
source("src/functions.R")
source("src/packages.R")
ctr <- c("Cameroon", "Central African Republic", "Chad", "Congo DR",
"Congo", "Equatorial Guinea", "Gabon", "Sao Tome and Principe",
"Benin", "Burkina Faso", "Cape Verde", "The Gambia", "Ghana",
"Guinea", "Guinea Bissau", "Cote d'Ivoire", "Liberia",
"Mali", "Mauritania", "Niger", "Nigeria", "Senegal",
"Sierra Leone", "Togo")
ctry <- standardise_country_names(ctr)
codes <- ccodes()
Tcorresp <- subset(codes,
subset = NAME %in% ctry,
select = c("NAME", "ISO3"))
world <- readOGR(dsn = "d:/gis/gadm/gadm36_levels.gpkg",
layer = "level0")
wcAfrica1 <- world[world$GID_0 %in% Tcorresp$ISO3, ]
wcAfrica2 <- gSimplify(wcAfrica1,
tol=.025,
topologyPreserve=T)
wcAfrica3 <- as(wcAfrica2,
"SpatialPolygonsDataFrame")
wcAfrica3@data <- wcAfrica1@data
e <- extend(extent(wcAfrica3), 1)
E <- as(e, "SpatialPolygons")
projection(E) <- projection(wcAfrica1)
roi1 <- raster::intersect(world, E)
roi2 <- gSimplify(roi1,
tol=.025,
topologyPreserve=T)
roi3 <- as(roi2,
"SpatialPolygonsDataFrame")
roi3@data <- roi1@data
writeOGR(wcAfrica1,
dsn = "data/geoWCAfrica.gpkg",
layer = "geoWCAfrica",
driver = "GPKG", overwrite = T)
writeOGR(wcAfrica3,
dsn = "data/geoWCAFricaLight.gpkg",
layer = "geoWCAfricaLight",
driver = "GPKG", overwrite = T)
writeOGR(roi1,
dsn = "data/roiWCAfrica.gpkg",
layer = "roiWCAfrica",
driver = "GPKG",
overwrite = T)
writeOGR(roi3,
dsn = "data/roiWCAfricaLight.gpkg",
layer = "roiWCAfricaLight",
driver = "GPKG",
overwrite = T)
#' Simplify a vector layer
geosimplify <- function(x) {
sx <- gSimplify(x, tol=.025, topologyPreserve=T)
ans <- as(sx, "SpatialPolygonsDataFrame")
ans@data <- x@data
return(ans)
}
#' Extract the region of interest from a context
#'
#' Get a map of the extent of x from a context
region_of_interest <- function(x, context) {
e <- extend(extent(x), 1)
E <- as(e, "SpatialPolygons")
projection(E) <- projection(x)
return(raster::intersect(context, E))
}
#' ECDC Covid19 data worldwide
#'
#' Returns the published file at a given date.
......@@ -77,6 +97,7 @@ standardise_country_names <- function(x) {
"Congo, DRC", "Congo DR",
"Congo DRC", "Congo DR",
"Congo, DR", "Congo DR",
"Republic of Congo", "Congo",
"Gambia", "The Gambia",
"Cote dIvoire", "Cote d'Ivoire",
"Côte d'Ivoire", "Cote d'Ivoire",
......@@ -100,6 +121,14 @@ standardise_country_names <- function(x) {
)
}
#' Get ISO3 codes for a list of countries
get_iso3 <- function(x) {
codes <- ccodes()
codes$ISO3[standardise_country_names(codes$NAME) %in%
standardise_country_names(x)]
}
#' Total population by country for a given year
#'
#' Population counts in thousands of people.
......
......@@ -40,7 +40,7 @@ plan <- drake_plan(
"Georgia", "Israel", "Jordan", "Lebanon", "Libya",
"Moldova", "Morocco", "Syria", "Tunisia", "Ukraine"),
wcAfrica = c("Cameroon", "Central African Republic", "Chad",
wca = c("Cameroon", "Central African Republic", "Chad",
"Congo DR", "Congo", "Equatorial Guinea", "Gabon",
"Sao Tome and Principe", "Benin", "Burkina Faso",
"Cape Verde", "The Gambia", "Ghana", "Guinea",
......@@ -80,6 +80,11 @@ plan <- drake_plan(
lockdown = as.Date(lockdown)
),
# CARTOGRAPHY ---------------------------------------------------
world_map = getData("countries"),
## GIS vector layer for European countries (EU 27 + UK + EFTA +
## candidate countries + non-EU countries). Source GADM.org
## version 3.6
......@@ -92,11 +97,11 @@ plan <- drake_plan(
layer = "roi_europe_light"),
## GIS vector layer for West and Central African countries
geoWCAfrica = readOGR(dsn = "data/geoWCAfricaLight.gpkg",
layer = "geoWCAfricaLight"),
geoWCAfrica = world_map[world_map$ISO %in% get_iso3(wca),] %>%
geosimplify(),
roiWCAfrica = readOGR(dsn = "data/roiWCAfricaLight.gpkg",
layer = "roiWCAfricaLight"),
roiWCAfrica = region_of_interest(geoWCAfrica, world_map) %>%
geosimplify(),
## DERIVED DATA ----------------------------------------
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment