[GIS] One shapefile for African continent containing counties/regions

africadatagadm

I use R to do my geo stuff with two limitations. First, I'm pretty new to all the geo stuff, and second I got lost while searching for a shapefile. I'm looking for a shapefile that contains all the gadm level 3 (possibly level 2 would suffice too) for all African countries. I could theoretically, download all the shapefiles from Global Administrative Areas and combine them into one masterfile (that would be another question). However, I was wondering if such a shapefile already exists… And if so, where?

Best Answer

So the answer is probably going to be no, there is no shapefile that is gadm data level 3 or 2 available for all of Africa because some countries only have data up to level 1 (Libya, West Sahara, etc. ).

That being said you can download the data and combine it into a shapefile – I was able to get the whole continent at level 1 downloading the rds files using an adjusted version of the script below which downloads all of Africa available at level 2 and grabs the rest at level one and makes shapefile of both subsets. You can merge them together using the union command In qgis if you use that or look into the union() command in the raster R package

I wrote a script that does the following:

  • Grabs the ISO3 country code from the countrycode package
  • Makes a list of the urls to download the level polygon info from http://www.gadm.org/
  • Downloads the files to your temp folder
  • Deletes the temp files that were created but did not download
  • Merges the polygons using the rbind command from the sp package
  • Makes a shapefile and saves it in your working directory
  • Creates a list of and downloads the remaining country files, binds and makes the second shapefile.

There is definitely a more elegant way to do this but this seems to work.

library(countrycode) #has list of ISO3 country codes built into package data
library(dplyr) #data filtering and manipulation
library(httr) 
library(tibble) 

library(rgdal) #used to create shapefile



list1 = unlist(countrycode_data %>%  
                 filter(continent == "Africa")  %>% 
                 select(iso3c))

for (i in list1){
  urls <- lapply(list1
                 , function(i) paste0("https://biogeo.ucdavis.edu/data/gadm2.8/rds/", i, "_adm2.rds" )
  )
}


for (u in urls){
  urls <- lapply(urls, function(u) GET(u,write_disk(tempfile(fileext = ".RDS")))
  )
}

infolist1 =rownames_to_column(
                  data.frame(
                    as.matrix(
                      unlist(
                        lapply(urls, `[`, c('url', 'status_code', 'content')
#explained at https://stackoverflow.com/questions/23758858/how-can-i-extract-elements-from-lists-of-lists-in-r
)
                        )
                      )
                    )
                  , var = "rowname")


infolist1$rowname= gsub(".*\\.","",infolist1$rowname) 
colnames(infolist1) = c("rowname", "data1")


c1= infolist1 %>% filter(rowname == 'url') %>%  select(data1)
c2 = infolist1 %>%  filter(rowname == 'status_code') %>%  select(data1)
c3= infolist1 %>%   filter(rowname == 'content') %>%   select(data1)

infolist1 = cbind(c1,c2,c3)
rm(list=c("c1", 'c2', 'c3'))
colnames(infolist1) = c("urlx", "filestatus", "filepath")



deletelist =  as.character(unlist(infolist1%>% filter(filestatus == '404') %>%  select(filepath)))

for (p in deletelist){
  paths <-  lapply(deletelist, function(p) file.remove(p)
  )
}


combinedRDS_lev2 <- do.call('rbind', lapply(list.files(pattern = ".RDS",tempdir(),full.names = T), readRDS))

plot(combinedRDS_lev2)

#setwd(...)
writeOGR(obj=combinedRDS_lev2, dsn=getwd(), layer="africalev2", driver="ESRI Shapefile")
# getwd() #

file.remove(dir(path = tempdir(), pattern = ".RDS",full.names=TRUE))

new_urls = gsub("_adm2","_adm1",unlist(infolist1%>% filter(filestatus == '404') %>%  select(urlx)))

for (u in new_urls){
  new_urls <- lapply(new_urls, function(u) GET(u,write_disk(tempfile(fileext = ".RDS")))
  )
}

    combinedRDS_lev1<- do.call('rbind', lapply(list.files(pattern = ".RDS",tempdir(),full.names = T), readRDS))
plot(combinedRDS_lev1)

writeOGR(obj=combinedRDS_lev1, dsn=getwd(), layer="africalev1", driver="ESRI Shapefile")
file.remove(dir(path = tempdir(), pattern = ".RDS",full.names=TRUE))