How to create a KML file using R

I wrote an R script to get map data (latitude and longitude values). I can capture them in R and visualize them. But now I want to generate a KML file from this data and view it using Google Earth. So I can share it with my colleagues, and they too can see it on Google Earth.

What is the best method / package for this?

+7
source share
4 answers

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

Exported objects: SpatialPointsDataFrame , SpatialLinesDataFrame or SpatialPolygonsDataFrame objects defined in the sp package.

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

To record using the KML driver, note that the geometry must be in geographic coordinates with a WGS84 reference.

+14
source

I think it's worth mentioning the plotKML package.

However, for a simple exchange among colleagues, I found an interesting leaflet -based mapview . You can save the map as an HTML document with various options for the background map; there is no need for Google Earth, and the HTML map will be launched in 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 

Additional examples with plotKML here , with a tutorial here . For mapview can be found here.

+2
source

If you're ready to go beyond R, there is a free program called DNRGarmin that can take a comma-delimited file in .txt format 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

therefore in R:

 my.geo.data <- all.my.data[ c("unique.id", "lats", "longs")] write.csv( my.geo.data, file = "myGeoData.txt") 

open DNRGarmin,

File β†’ Download from β†’ File β†’ myGeoData.txt Then, File β†’ Save to β†’ File β†’ myGeoData.kml

@rcs advice re: WGS84 also applies to this answer.

Good luck.

0
source

If you / your colleagues know QGIS, this is a very good way to display data in Google Earth. QGIS has the function of displaying 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, this requires your data to be properly designed, as rcs says.

Here you need to export your points as a form file using the maptools package and the 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 displayed in Google Earth. In QGIS, your data can also be easily saved as a kmz file, if necessary.

0
source

All Articles