Skip to content

Instantly share code, notes, and snippets.

@ashander
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashander/e7da5e58676656eb6a69 to your computer and use it in GitHub Desktop.
Save ashander/e7da5e58676656eb6a69 to your computer and use it in GitHub Desktop.
Looking at mapping from gadm using gadm2 urls
## urls were updated for gdam2 -- should go to gadm.org to update for each run of this
## http://gadm.org/country
## choose a country and R spdf from dropdown
## eg for
##for AFG sends POST req http://gadm.org/download?OK=OK&_submit_check=1&cnt=AFG_Afghanistan&thm=R%23R%20data
##returns page with list of download links (likely based on your location and data availability)
## part of the page looks like
##
## download
## level 0
## level 1
## etc
##
## COPY and paste the url from one of these into your rscript
##
## EG for me this gave (EXAMPLE)
## http://somedomain.edu/data/gadm2/R/AFG_adm0.RData
## which I use below as mybaseurl
source('so_spdf_gadm_functions.R') ## modified versions of SO to use baseurl argument
## new base
mybaseurl <- "http://somedomain.edu/data/gadm2/R/" ## NOTE this will not work, follow steps above to get teh real url
tmp <- getCountries('ARG', mybaseurl)
## code below from SO answer http://stackoverflow.com/questions/5126745/gadm-maps-cross-country-comparision-graphics
## modified to use old gadm base url as default
## you will need the sp-package
library('sp')
## old version gadm
baseurl <- "http://gadm.org/data/rda/"
## load a file from GADM (you just have to specify the countries "special part" of the file name, like "ARG" for Argentina. Optionally you can specify which level you want to have
loadGADM <- function (fileName, baseurl, level = 0, ...) {
urlstring <- paste(baseurl, fileName, "_adm", level, ".RData", sep = "")
load(url(urlstring))
gadm
}
## the maps objects get a prefix (like "ARG_" for Argentina)
changeGADMPrefix <- function (GADM, prefix) {
GADM <- spChFIDs(GADM, paste(prefix, row.names(GADM), sep = "_"))
GADM
}
## load file and change prefix
loadChangePrefix <- function (fileName, level = 0, ...) {
theFile <- loadGADM(fileName, level)
theFile <- changeGADMPrefix(theFile, fileName)
theFile
}
## this function creates a SpatialPolygonsDataFrame that contains all maps you specify in "fileNames".
## E.g.:
## spdf <- getCountries(c("ARG","BOL","CHL"))
## plot(spdf) # should draw a map with Brasil, Argentina and Chile on it.
getCountries <- function (fileNames, level = 0, ...) {
polygon <- sapply(fileNames, loadChangePrefix, level)
polyMap <- do.call("rbind", polygon)
polyMap
}
@embruna
Copy link

embruna commented Mar 5, 2015

Hey, thanks for the help. I hadn't seen this before I set up my repo based on the StackOverflow question or I would have forked it over. Here's the version with my changes: https://github.com/embruna/multicountry_mapping

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment