作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
也许是由于我对R还是比较陌生,所以在http://www.gadm.org/上使用gadm-Mapfiles时遇到了问题。
我尝试绘制几个国家/地区的 map ,并将它们相互比较(使用不同的颜色)。
这就是我要做的
library('sp')
##
load(url('http://biogeo.ucdavis.edu/data/gadm2/R/ARG_adm0.RData'))
# loads an Object "gadm" with shape of Argentinia
arg <- gadm # is there a more convenient way to do this in one line?
load(url('http://biogeo.ucdavis.edu/data/gadm2/R/CHL_adm0.RData'))
# loads an Object "gadm" with shape of Chile
chl <-gadm
load(url('http://biogeo.ucdavis.edu/data/gadm2/R/BOL_adm0.RData'))
# loads an Object "gadm" with shape of Bolivia
bol <- gadm
##
spplot(c(arg, chl, bol))
# output: unable to find an inherited method for function "spplot", for signature "list"
## 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://biogeo.ucdavis.edu/data/gadm2/R/", 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
}
最佳答案
对于问题1,它是R,因此您可以滚动自己的load()
函数来执行所需的操作,例如:
loadGADM <- function(file, ...) {
load(file, ...)
gadm
}
> ls()
character(0)
> loadGADM <- function(file, ...) {
+ load(file, ...)
+ gadm
+ }
> arg <- loadGADM(url('http://gadm.org/data/rda/ARG_adm0.RData'))
> ls()
[1] "arg" "loadGADM"
gadm
时,这是一种本地解决方案-您可以改进该函数以使其不需要,例如:
loadGADM <- function(file, ...) {
f <- load(file, ...)
get(f)
}
load()
返回已加载对象名称的字符串。
rbind()
对象一起
sp
在一起,而不是将它们连接在一起。但是,这不适用于这些对象,并且多边形ID是唯一的:
> sa <- rbind(arg, chl, bol)
Error in validObject(res) :
invalid class "SpatialPolygons" object: non-unique Polygons ID slot values
spChFIDs()
更改Polygons ID插槽值。在这里,我们将
"arg_"
等附加到对象的行名,以使这些行名不是唯一的:
arg <- spChFIDs(arg, paste("arg", row.names(arg), sep = "_"))
chl <- spChFIDs(chl, paste("chl", row.names(chl), sep = "_"))
bol <- spChFIDs(bol, paste("bol", row.names(bol), sep = "_"))
sa <- rbind(arg, chl, bol)
sp
对象:
plot(sa) ## beware might take a long time to plot...
关于r - GADM-Maps越野比较图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5126745/
我是一名优秀的程序员,十分优秀!