我编写了一个R脚本来获取一些地图点数据(纬度和经度值).我可以在R中绘制它们并将它们可视化.但现在我想使用Google地球从这些数据和视图生成KML文件.这样我就可以与同事分享,他们也可以在Google地球上看到它.
这样做的最佳方法/包是什么?
rcs*_*rcs 21
检查包中的writeOGR功能rgdal.这是一个简单的例子:
library("sp")
library("rgdal")
data(meuse)
coordinates(meuse) <- c("x", "y")
proj4string(meuse) <- CRS("+init=epsg:28992")
meuse_ll <- spTransform(meuse, CRS("+proj=longlat +datum=WGS84"))
writeOGR(meuse_ll["zinc"], "meuse.kml", layer="zinc", driver="KML")
Run Code Online (Sandbox Code Playgroud)
导出的对象是SpatialPointsDataFrame,SpatialLinesDataFrame,或SpatialPolygonsDataFrame对象中的定义,sp包.
R> class(meuse)
[1] "SpatialPointsDataFrame"
attr(,"package")
[1] "sp"
Run Code Online (Sandbox Code Playgroud)
对于使用KML驱动程序进行书写,请注意几何应位于具有基准WGS84的地理坐标中.
我认为值得一提的是plotKML软件包。
但是,为了便于同事之间共享,我发现基于传单包的mapview包很有趣。可以将地图另存为HTML文档,其中包含背景地图的各种选项;无需Google Earth,HTML地图将在您的浏览器上运行。
一些例子:
library(sp)
library(rgdal)
library(raster)
library(plotKML)
library(mapview)
# A SpatialPointsDataFrame example
data(meuse)
coordinates(meuse) <- ~x+y
proj4string(meuse) <- CRS("+init=epsg:28992") # CRS Amersfoort (Netherlands)
# make a KML file from SpatialPointsDataFrame object
# will get a warning like "Reprojecting to +proj=longlat +datum=WGS84 ..."
# as it is expected to work with geographical coordinates with datum=WGS84,
# but seems that it takes care of the reprojecting.
plotKML::kml(meuse,
file.name = "meuse_cadium.kml",
points_names = meuse$cadmium,
colour = "#FF0000",
alpha = 0.6,
size = 1,
shape = "http://maps.google.com/mapfiles/kml/pal2/icon18.png")
# Or, an easy to make interactive map with mapView()
mapView(meuse)
# A RasterLayer example
data(meuse.grid)
gridded(meuse.grid) <- ~x+y
proj4string(meuse.grid) <- CRS("+init=epsg:28992")
dist_rst <- raster(meuse.grid["dist"])
# make a KML file from RasterLayer object
plotKML::kml(dist_rst,
file.name = "dist_rst.kml",
colour_scale = SAGA_pal[[1]])
# Or, easy to make interactive map with mapView() - display raster and add the points
mapView(dist_rst, legend=TRUE) + meuse
# However, note that for bigger raster datasets mapView() might reduce from resolution
Run Code Online (Sandbox Code Playgroud)
有更多的例子plotKML 在这里,有一个教程在这里。为此mapview,可以在这里找到简介。
| 归档时间: |
|
| 查看次数: |
13750 次 |
| 最近记录: |