[R] GADM Data Download

Lorenzo Isella lorenzo.isella at gmail.com
Wed Nov 27 09:55:09 CET 2013


Dear All,
Please consider the snippet at the end of the email.
I often download some maps (in the R format) from

http://www.gadm.org/

However, when I run (typically more than once) a variation of the script  
below (based on http://bit.ly/1b3W0Aa ),
I often get


Error in load(url(paste("http://gadm.org/data/rda/", fileName, "_adm",  :
   cannot open the connection
In addition: Warning message:
In load(url(paste("http://gadm.org/data/rda/", fileName, "_adm",  :
   cannot open: HTTP status was '504 Gateway Time-out'


Does anybody know if gadm blocks repeated attempts to retrieve the same  
data?
I am not talking about saturated its bandwidth, just retrieving a few tens  
of Mb per day at most.
Many thanks

Lorenzo







##############################################à	


## you will need the sp-package
library('sp')

## 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, level = 0, ...) {
     load(url(paste("http://gadm.org/data/rda/", fileName, "_adm", level,  
".RData", sep     = "")))
     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
}

################################################################
################################################################
################################################################
################################################################


spdf <- getCountries(c("ITA","FRA", "DEU","BEL", "LUX", "ESP",
                        "FIN", "SWE","DNK", "POL", "PRT", "CZE",
                        "SVK", "SVN", "GBR", "IRL", "ROU", "HUN",
                        "NLD", "AUT", "BGR","GRC",
                        "EST","LVA", "LTU","CYP","MLT", "HRV", "CHE"
                        ))


save(spdf,file="gadm_data.Rdata")



More information about the R-help mailing list