Export a spatial object as KML with R

It can be useful sometimes to visualize your data using Google Earth. For example, if you want to explore the population density map of a city, you can project it on Google Earth to investigate in detail which neighbours are most populated. In this post, I’m going to show you how to export a spatial object in KML format with R.

First we need a shapefile, we can for example use this one that I have already used in a previous post. This shapefile contains the boundaries of the 38 municipalities composing the metropolitan area of Barcelona. We import the file using the readOGR function from the package rgdal. The resulting object MA is a SpatialPolygonsDataFrame (see this post for more details about this type of object).


To make things a bit more complicated we are going to randomly assign to each of the spatial polygons a colour. To do that I first need to create a vector colo containing the colours. In this case, we can use the function rgb to define three semi-transparent colours:


Then we use the function sample to draw at random an integer between 1 and 3 (corresponding to the position of the colour in the vector colo), for each of the spatial polygons, that we store in a vector ID.


Now we can plot the coloured SpatialPolygonsDataFrame. First, we plot an empty map using the bbox attribute to define the plot region. Then we add the Polygons one by one using the function fortify (package ggplot2). This allows us to modify the colour of the polygons according to their value stored in the vector ID:


plot(1,xlab=" ",ylab=" ",xlim=c(minx,maxx),ylim=c(miny,maxy),axes=FALSE)
for(i in 1:dim(MA)[1]){
    for(po in 1:length(MA@polygons[[i]]@Polygons)){
      for(k in 1:length(colo)){

Now we can use the function kmlPolygons proposed by Tom Philippi to export this coloured map in KML format.


This function extends the kmlPolygon function in package maptools to accept a SpatialPolygonsDataFrame object, and allow labels, descriptions, and polygon fill colours to be based on attributes in the dataframe of the SpatialPolygonsDataFrame object. First we have to add the vector ID, as a factor, in the data attribute of MA and rename the column ‘COLOUR’.


Finally, we can use the kmlPolygons to export the SpatialPolygonsDataFrame as KML file. All the parameters are passed as character values of the names of the objects,

  • PGdv: the character value of the name of the SpatialPolygonsDataFrame object, MA in our case;
  • kmlFileName: the name of the output *.kml file;
  • Pname: the name of the variable in PGdv’s dataframe with values for names of the individual polygons;
  • Pdescription: the name of the variable in PGdv’s dataframe with values for descriptions of the individual polygons;
  • Kname: a character string name for the kml object in GoogleEarth (one value per kml file);
  • Kdescription: a character string description for the kml object in GoogleEarth (one value per kml file);
  • ColorTheme: the name of the (numeric or factor) variable in PGdv’s dataframe with integer values or factor numbers mapped to colors by ColorMap, COLOUR in our case;
  • ColorMap: a vector of character strings in #RRGGBBAA form (AA=alpha 00=transparent FF=solid) this vector must be as long as the number of levels in ColorTheme, or the maximumvalue if ColorTheme is numeric integers, colo in our case.

We can now visualize the resulting KML file MA.kml in Google Earth. You can modify the colours of the boundaries by playing with the parameter border in the script kmlPolygons.R.

comments powered by Disqus