R/readCountries.R

Defines functions readCountries

## function to get the boundaries of countries from different sources in a standardized way

readCountries <- function(countries="DIVA GIS", cache=T) {
  
  if (countries=="DIVA GIS"){
    if (file.exists(paste0(geodata$config$mainfolder, "cache/DIVA_GIS_boundaries.rda")) & cache==T) {
      load(paste0(geodata$config$mainfolder, "cache/DIVA_GIS_boundaries.rda"))
    } else {
      
      boundaries <- rgdal::readOGR(paste0(geodata$config$mainfolder, "DIVA GIS/countries_shp/countries.shp"), layer="countries")
      ISO3 <- as.character(boundaries$ISO3)
      boundaries <- maptools::unionSpatialPolygons(boundaries, ISO3)
      # save(boundaries, file=paste0(geodata$config$mainfolder, "cache/DIVA_GIS_boundaries.rda"))
    }
  }
  
  return(boundaries)
}
pik-piam/geodata documentation built on Nov. 5, 2019, 12:21 a.m.