如何使用 R 创建 KML 文件
我编写了一个 R 脚本来获取一些地图点数据(纬度和经度值)。我能够在 R 中绘制它们并将它们可视化。但现在我想从这些数据生成一个 KML 文件并使用 Google Earth 进行查看。这样我就可以与同事分享,他们也可以在 Google 地球上看到它。
执行此操作的最佳方法/包是什么?
I have written a R script to get some map point data (Latitude and Longitude values). I am able to plot them in R and visualize them. But now I want to generate a KML file from this data and view using Google Earth. So that I can share it with colleagues and they can see it on Google Earth too.
What is the best method / package to do this ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
检查rgdal包中的writeOGR函数。这是一个简单的示例:
导出的对象是
sp
中定义的SpatialPointsDataFrame
、SpatialLinesDataFrame
或SpatialPolygonsDataFrame
对象。包裹。对于使用 KML 驱动程序进行写入,请注意几何图形应与基准面 WGS84 处于地理坐标中。
Check the
writeOGR
function in thergdal
package. Here is a simple example:The objects exported are
SpatialPointsDataFrame
,SpatialLinesDataFrame
, orSpatialPolygonsDataFrame
objects as defined in thesp
package.For writing with the KML driver, note that the geometries should be in geographical coordinates with datum WGS84.
我认为值得一提的是 plotKML 包。
编辑 2022-05-16:似乎
plotKML
不再在 CRAN 上,但您可以使用 CRAN 存档中的旧包版本。请参阅安装从 CRAN 中删除的软件包。然而,为了在同事之间轻松共享,我发现 mapview 很有趣基于 leaflet 包的包。可以将地图另存为 HTML 文档,并提供各种背景地图选项;无需 Google 地球,HTML 地图将在您的浏览器上运行。
一些示例:
更多使用
plotKML
的示例此处。对于
mapview
,可以在此处找到简介。I think is worth mentioning the plotKML package as well.
Edit 2022-05-16: Seems that
plotKML
is no longer on CRAN, but you can use an older package version from the CRAN archive. See Installing a Package Removed from CRAN.However, for easy sharing among colleagues I found interesting the mapview package based on leaflet package. One can save a map as HTML document with various options for a background map; no need of Google Earth and the HTML map will run on your browser.
Some examples:
More examples with
plotKML
here.For
mapview
, an intro can be found here.由于 rgdal 包已从 CRAN 中删除,@rcs 提供的答案不再有效。在这种情况下,您可以使用
sf
包,例如Since
rgdal
package has been removed from CRAN, the answer provided by @rcs no longer works. In that case you can usesf
package like如果您愿意超越 R,有一个名为 DRNGarmin 的免费程序可以将逗号分隔的文件作为 .txt 并将其转换为 .kml 以导入到 google Earth 中。
您可以在这里找到它:
http:// /www.dnr.state.mn.us/mis/gis/tools/arcview/extensions/DNRGarmin/DNRGarmin.html
所以在 R 中:
打开 DNRGarmin,
文件->加载自 ->文件-> myGeoData.txt 然后,
文件->保存到->文件-> myGeoData.kml
@rcs 的建议:WGS84 也适用于这个答案。
祝你好运
If you're willing to step outside R, there is a free program called DNRGarmin can take a comma separated file as a .txt and convert it to .kml for import into google earth.
You can find it here:
http://www.dnr.state.mn.us/mis/gis/tools/arcview/extensions/DNRGarmin/DNRGarmin.html
so in R:
open DNRGarmin,
File -> Load From -> File -> myGeoData.txt Then,
File -> Save to -> File -> myGeoData.kml
@rcs's advice re: WGS84 applies for this answer too.
Good luck
如果您/您的同事了解 QGIS,这是在 Google Earth 中显示数据的一个非常好的方法。 QGIS 具有将 Google Earth 显示为底图的功能,然后您可以打开您的空间数据,它将显示在底图上。当然,它需要您的数据按照 rcs 的说法正确投影。
在这里,您需要使用maptools包和Spatial Points包将点导出为形状文件:
您的点现在可以在QGIS中打开并显示在Google Earth中。如果需要,在 QGIS 中,您的数据还可以轻松保存为 kmz 文件。
If you/your collegues know QGIS, this is a very good way to display data in Google Earth. QGIS has the feature of showing Google Earth as a base map and then you can open your spatial data and it will be displayed on the base map. Of course it requires your data to be correctly projected as rcs says.
Here you need to export your points as a shape file using the maptools package and Spatial Points package:
Your points can now be opened in QGIS and be displayed in Google Earth. In QGIS your data can also easily be saved as kmz file if necessary.