How to create a KML file using R

女生的网名这么多〃 提交于 2019-12-02 23:38:38

Check the writeOGR function in the rgdal package. Here is a simple example:

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") 

The objects exported are SpatialPointsDataFrame, SpatialLinesDataFrame, or SpatialPolygonsDataFrame objects as defined in the sp package.

R> class(meuse)
[1] "SpatialPointsDataFrame"
attr(,"package")
[1] "sp"

For writing with the KML driver, note that the geometries should be in geographical coordinates with datum WGS84.

I think is worth mentioning the plotKML package as well.

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:

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

More examples with plotKML here, with a tutorial here. For mapview, an intro can be found here.

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:

my.geo.data <- all.my.data[ c("unique.id", "lats", "longs")]

write.csv( my.geo.data, file = "myGeoData.txt")

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

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:

library(maptools)
library(sp)

## define projection
myProjection <- "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"

## your points in format dataframe
coordinates.df <- as.data.frame(MyCoordinates) 

## the number of points you have as dataframe
number <- as.data.frame(NumberOfPoints)

## convert points to Spatial Points Dataframe
myPoints.spdf <- SpatialPointsDataFrame(coordinates.df, number, proj4string = CRS(myProjection))

## save shapefile
writeSpatialShape(myPoints.spdf, "MyPointsName")

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!