问题描述
我正在构建一个将多几何 KML 文件读入 R 的工作流程.这是我的 工作地图,带有点和路径几何图形.(可重现的)R 脚本是:
I'm building a workflow for reading multiple-geometry KML files into R. This is my working map, with point and path geometries. The (reproducible) R script is:
library(rgdal)
setwd( {SPECIFY A FOLDER} )
download.file("http://www.scribblemaps.com/maps/kml/shackleton.kml", "file.kml")
(lyr = ogrListLayers("file.kml"))
map = readOGR ("file.kml", layer=lyr, verbose = TRUE, drop_unsupported_fields=T, dropNULLGeometries=T)
失败:
Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv) :
Cannot open layer
任何想法如何解决这个问题?
Any ideas how to work around this?
我已经设法通过创建一个仅包含路径条目的新 KML 来获取路径数据:在 Google 地球中,单击 kml 对象列表中的路径条目,选择复制",粘贴到文本编辑器并另存为KML.这在 R 中打开:
I've managed to get the path data in by creating a new KML of just the path entry: in Google Earth click on the path entry in the kml object list, select 'copy', paste into text editor and save as KML. This opens in R with:
(lyr = ogrListLayers("path.kml"))
pathkml = readOGR ("path.kml", layer=lyr, verbose = TRUE, drop_unsupported_fields=T, dropNULLGeometries=T)
coordinates(pathkml)
但我还没有实现点数据的工作流程,其中包括点标签和注释.更可取的解决方案是阅读原始 KML 并消除对 Google 地球的需求,但当然可以.非常感谢您的帮助.
But I've not achieved a workflow for the point data, which includes point labels and annotation. The preferable solution would read the original KML and negate the need for Google Earth, but of course whatever works.. Very grateful for assistance.
附录:
我现在正在研究使用 XML 来解析它.数据似乎在标签内结构化.我必须:
I'm now looking at using XML to parse this. The data seems structured within tags. I've got to:
require(XML)
d = xmlParse("shackleton.kml")
doc = xpathSApply(d, "//Placemark")
但这似乎没有任何用处.
but this seems to yield nothing useful.
推荐答案
这个R-sig-geo 线程 也指.似乎解决这个问题的唯一方法是编写一个自定义函数来将 KML 解析为 XML,并从各自的几何图形构建单独的 spatialPoints 和 spatialPolygons 对象.总的来说,尝试并坚持使用单一几何 KML 可能更有效..
This R-sig-geo thread also refers. It seems the only way to approach this would be to write a custom function to parse the KML as XML and build individual spatialPoints and spatialPolygons objects from the respective geometries. On balance probably more effective to just try and stick to single geometry KMLs..
这篇关于将 KML 文件读入 R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!