Last active
August 29, 2015 14:16
-
-
Save ashander/e7da5e58676656eb6a69 to your computer and use it in GitHub Desktop.
Looking at mapping from gadm using gadm2 urls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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