Functions getGADM(), getGaz(), and getWDPA() help download data from the GADM administrative units for the countries of the world link, DIVA-GIS gazetteers (link), and the IUCN Global Database for Protected Areas (link).

library(dplyr)
#library(Rocc)

Downloading the datasets

GADM

Function getGADM() will download the GADM shapefiles in sp or sf formats if they are not already in disk. Parameter "best = TRUE" will seek to download the best available resolution, it will not download coarser datasets if finer resolution datasets are available.

countries <- c("Colombia", "Brazil", "Ecuador", "Peru", "Venezuela", "Bolivia")

iso3 <- countrycode::countrycode(countries,
                                 "country.name",
                                 "iso3c")
# Check the downloading scheme: 
df <- data.frame(codigo = rep(iso3, each = 5), pais = rep(4:0, length(countries)))
# we try the downloads at the finest scale first and ask best = TRUE to avoid downloading coarser levels. this is not beautiful, maybe the user should use EITHER level OR best internally. hmmm. if best = TRUE then default level is 4, then 3 etc. with a while loop. 
purrr::walk2(.x = rep(iso3, each = 5),
             .y = rep(4:0, length(countries)),
             ~ getGADM(
               cod = .x,
               level = .y,
               best = TRUE
             ))

DIVA-GIS gazetteer

Function getGAZ() downloads the DIVA-GIS gazetteer files for each country. These are zipped .dbf files.

iso3 %>% purrr::map(.x = ., ~ getGAZ(cod = .x))

IUCN files

Function getWDPA() downloads the shapefiles from the IUCN Global Database for Protected Areas for each country. These are zipped .shp files. Read the documentation carefully, not every country has data, and it can be in different stages of completion, revision and approval.

iso3 %>% purrr::map(.x = ., ~getWDPA(cod = .x))

gadm_files <- list.files("GADM", pattern = paste(iso3, collapse = "|"), full.names = TRUE) 
lev <- stringr::str_extract(string = gadm_files, pattern = "\\d+")

gaz_files <- list.files("GAZ", pattern = ".dbf", full.names = TRUE) 

wdpa_files <- list.files("WDPA", full.names = TRUE, recursive = T, pattern = ".shp$") 
library(sf)
library(rgdal)

bol0_pts <- read_sf(wdpa_files[1])
bol0_pol <- read_sf(wdpa_files[2])
bol1_pts <- read_sf(wdpa_files[3])
bol1_pol <- read_sf(wdpa_files[4])
library(tmap)

mapbol <- tm_shape(bol) + 
  tm_polygons(col = "white") + 
  tm_shape(bol0_pol) +
  tm_fill(col = "darkgreen") + 
  tm_shape(bol1_pol) +
  tm_fill(col = "green") + 
  tm_shape(bol1_pts) + 
  tm_dots()

  
bol <- readRDS(gadm_files[1])

plot(bol[1])
maptools
library(dplyr)
bol_gaz <- read_sf(gaz_files[1]) #mk, sirve
bol_gaz <- mutate(bol_gaz, 
                  LONG = as.numeric(LONG),
                  LAT  = as.numeric(LAT)
                  ) 
coordinates(bol_gaz) <- ~LONG+LAT

ex <- head(bol_gaz)
apply(ex, 2, class)
library(sf)
st_sf(ex)

mapbol +
  tm_shape(bol_gaz) + 
  tm_dots()