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?

  • If you are seeking open data then the place to ask is the Open Data Stack Exchange.
    – PolyGeo
    Jul 19, 2017 at 20:58

2 Answers 2


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(rgdal) #used to create shapefile

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

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(
                        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))


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))

writeOGR(obj=combinedRDS_lev1, dsn=getwd(), layer="africalev1", driver="ESRI Shapefile")
file.remove(dir(path = tempdir(), pattern = ".RDS",full.names=TRUE))
  • @AndreJ, Thank you for fixing the formatting in the original answer- also wanted to note that the second round of downloads grabbed the lower level GADM files.
    – A_Lewis
    Jul 22, 2017 at 17:15

I found this Q&A while searching to make the same thing for myself. Two things made it difficult to follow the accepted answer for me: 1) I had already downloaded the whole-of-Earth GADM shapefile. The problem for me was that 'continent' was not a variable name in that dataset - country was the biggest defined area. 2) the countrycodes package didn't work according to how @A_Lewis described, so I had to find another list of country codes. Maybe the package has evolved since the answer described above.

This is the code which I used:


shp <- readOGR(dsn="[myprojectfolder]/GADM/gadm36.shp", layer="gadm36")
countryweb <-  "https://pkgstore.datahub.io/JohnSnowLabs/country-and-continent-codes-list/country-and-continent-codes-list-csv_csv/data/b7876b7f496677669644f3d1069d3121/country-and-continent-codes-list-csv_csv.csv"
country.csv <- read.csv(countryweb)
names(country.csv)[5] <- "GID_0"
africaCountries <- subset(country.csv, Continent_Code=="AF")
africa_shp <- subset(shp, GID_0 %in% africaCountries$GID_0 )
## Store the Africa shapefile so that you don't have to import the whole world next time:
writeOGR(africa_shp,".", "africa-rgdal", driver="ESRI Shapefile")

Hope it helps someone out there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.