16

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 ?

Vijay Barve
  • 325
  • 1
  • 4
  • 13

4 Answers4

24

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.

rcs
  • 67,191
  • 22
  • 172
  • 153
9

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:

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.

For mapview, an intro can be found here.

Valentin_Ștefan
  • 6,130
  • 2
  • 45
  • 68
1

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

Chris
  • 418
  • 3
  • 10
0

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.

dtanon
  • 173
  • 1
  • 2
  • 14