Guest User

Untitled

a guest
Apr 7th, 2014
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 1.19 KB | None | 0 0
  1. # "getCountries" code taken from:
  2. # http://stackoverflow.com/questions/5126745/gadm-maps-cross-country-comparision-graphics
  3.  
  4. # Load required libraries
  5. library(sp)
  6.  
  7. # Load file from GADM
  8. # Specify the countries for fileName using ISO3C
  9. # like "AFG" for Afghanistan.
  10. # "level" specifies adminsitrative level.
  11. loadGADM<-function(fileName,level=0,...){
  12.     load(url(paste("http://gadm.org/data/rda/",fileName,"_adm",level,".RData",sep = "")))
  13.     gadm
  14. }
  15.  
  16. # Add prefix (ISO3C code) to shapefile.
  17. changeGADMPrefix<-function(GADM, prefix) {
  18.     GADM <- spChFIDs(GADM, paste(prefix, row.names(GADM), sep = "_"))
  19.     GADM
  20. }
  21.  
  22. # Load file and change prefix
  23. loadChangePrefix<-function (fileName, level = 0, ...) {
  24.     theFile <- loadGADM(fileName, level)
  25.     theFile <- changeGADMPrefix(theFile, fileName)
  26.     theFile
  27. }
  28.  
  29. # Apply all the functions:
  30. getCountries <- function (fileNames, level = 0, ...) {
  31.     polygon <- sapply(fileNames, loadChangePrefix, level)
  32.     polyMap <- do.call("rbind", polygon)
  33.     polyMap
  34. }
  35.  
  36.  
  37. ## Example
  38.  
  39. countries<-c("CZE","DEU","AUT","SVK","POL")
  40. EUR<-getCountries(countries,level=1) # Level 1
  41. CZE<-subset(EUR,ISO=="CZE")
  42. plot(EUR)
  43. plot(CZE,col="steelblue4",add=T)
Advertisement
Add Comment
Please, Sign In to add comment