getKMLcoordinates返回列表列表,而不是段列表
有一次,我绘制了一张很酷的地图,用R表示kml坐标,并将物种丰度数据叠加在上面。当我第一次编写代码时,我使用以下命令调用我的坐标列表:getKMLcoordinates返回列表列表,而不是段列表,r,gps,kml,R,Gps,Kml,有一次,我绘制了一张很酷的地图,用R表示kml坐标,并将物种丰度数据叠加在上面。当我第一次编写代码时,我使用以下命令调用我的坐标列表: spa示例文件包含getKMLcoordinates函数(maptools版本0.8-14)不需要的制表符 如果你做了一个小的修改,它会像预期的那样工作。我在添加内容的地方添加了评论: getKMLcoordinates_01 <- function (kmlfile, ignoreAltitude = FALSE) { if (missing(
spa示例文件包含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
getKMLcoordinates_01修复已提交至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)
库(rgdal)
ogrListLayers(dsn=“permiture_Track.kml”)#查找图层名称
spa1我现在把它附在问题之后。我希望它能有所帮助。我没有使用rgdal,因为MacOSX没有现成的二进制文件。我一直在努力安装这个包(请参阅我以前的.kml相关的呼救:)
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
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
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)
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)