我通过绘制R中的kml坐标并在顶部覆盖物种丰度数据,制作了一张很酷的地图。当我第一次编写代码时,我使用以下内容来调用我的坐标列表:
spa<-getKMLcoordinates('Perimeter_Track.kml', ignoreAltitude=TRUE)
summary(spa)
返回:
Length Class Mode
[1,] 128 -none- numeric
[2,] 242 -none- numeric
[3,] 34 -none- numeric
[4,] 126 -none- numeric
(GPS坐标是以4个块为单位测量的周边轨迹,因此每个列表都是其中一个块):
现在,当我再次运行代码时,summary(spa)
返回:
Length Class Mode
[1,] 2 -none- list
[2,] 2 -none- list
[3,] 2 -none- list
[4,] 2 -none- list
如果我使用as.data.frame()
将spa转换为数据框,当我尝试使用rbind
将4个块连接成一个大的轨道时,我仍然会收到此错误:
Error in match.names(clabs, names(xi)) :
names do not match previous names
R似乎没有读取kml文件,或者只读取部分文件。
列表(或数据框)spa是
c..157.80736808..21.4323436..20.324951171875.
1 -157.80737
2 21.43234
3 20.32495
c..157.80738216..21.43231443..19.36376953125.
1 -157.80738
2 21.43231
3 19.36377
c..157.80533605..21.43536092..15.9990234375. c..157.8053208..21.43541138..15.9990234375.
1 -157.80534 -157.80532
2 21.43536 21.43541
3 15.99902 15.99902
c..157.80998348..21.43706806..15.9990234375.
1 -157.80998
2 21.43707
3 15.99902
c..157.80997007..21.43711106..17.441162109375.
1 -157.80997
2 21.43711
3 17.44116
c..157.81074733..21.43717535..13.5958251953125.
1 -157.81075
2 21.43718
3 13.59583
c..157.81071673..21.43718331..14.076416015625.
1 -157.81072
2 21.43718
3 14.07642
这是maptools或我的代码的错误吗?据我所知,这是将kml坐标转换为R可以使用的坐标的最简单方法。非常感谢您的帮助。
该文件的链接位于:https://www.dropbox.com/s/y8elanjnst6438v/Perimeter_Track.kml
答案 0 :(得分:7)
该修复已经致力于R-forge,maptools项目,修订版232.请在欧洲时间晚上之后试用:
install.packages("maptools", repos="http://R-Forge.R-project.org")
处理这个问题。但是,我不知道你为什么不使用:
library(rgdal)
ogrListLayers(dsn="Perimeter_Track.kml") # to find the layer name
spa1 <- readOGR(dsn="Perimeter_Track.kml", layer="Perimeter_Track.kml")
summary(spa1)
这是一个SpatialLinesData框架,可以通过多种方式使用,无需进一步努力。它也毫不费力地处理标签。要获得ad-hoc maptools函数返回的表单的输入,请执行:
o0 <- coordinates(spa1)
o1 <- lapply(o0, function(x) do.call("rbind", x))
library(maptools) # SVN revision >= 232
spa <- getKMLcoordinates('Perimeter_Track.kml', ignoreAltitude=TRUE)
all.equal(o1, spa)
答案 1 :(得分:0)
您的示例文件包含getKMLcoordinates
函数不期望的制表符(maptools版本0.8-14)。
如果进行小修改,它会按预期工作。我已将评论添加到我的位置:
getKMLcoordinates_01 <- function (kmlfile, ignoreAltitude = FALSE)
{
if (missing(kmlfile))
stop("kmlfile is missing")
kml <- paste(readLines(kmlfile, encoding = "UTF-8"), collapse = " ")
re <- "<coordinates> *([^<]+?) *<\\/coordinates>"
## ++ new code
## remove tabs first
kml <- gsub("\\t", "", kml)
##
mtchs <- gregexpr(re, kml)[[1]]
coords <- list()
for (i in 1:(length(mtchs))) {
kmlCoords <- unlist(strsplit(gsub(re, "\\1", substr(kml,
mtchs[i], (mtchs[i] + attr(mtchs, "match.length")[i])),
perl = TRUE), split = " "))
m <- t(as.matrix(sapply(kmlCoords, function(x) as.numeric(unlist(strsplit(x,
","))), USE.NAMES = FALSE)))
if (!ignoreAltitude && dim(m)[2] != 3)
message(paste("no altitude values for KML object",
i))
coords <- append(coords, ifelse(ignoreAltitude, list(m[,
1:2]), list(m)))
}
coords
}
spa <- getKMLcoordinates_01("Perimeter_Track.kml")
summary(spa)
Length Class Mode
[1,] 192 -none- numeric
[2,] 363 -none- numeric
[3,] 51 -none- numeric
[4,] 189 -none- numeric
R version 2.15.0 Patched (2012-05-05 r59321) Platform: x86_64-pc-mingw32/x64 (64-bit) locale: [1] LC_COLLATE=English_Australia.1252 LC_CTYPE=English_Australia.1252 LC_MONETARY=English_Australia.1252 LC_NUMERIC=C [5] LC_TIME=English_Australia.1252 attached base packages: [1] stats graphics grDevices utils datasets compiler methods base other attached packages: [1] rgdal_0.7-8 maptools_0.8-14 lattice_0.20-6 sp_0.9-98 foreign_0.8-49 loaded via a namespace (and not attached): [1] grid_2.15.0 tools_2.15.0