8

All,

I have exported an ESRI shapefile (polygon) to KML using R, but the resulting layer shows only the outer extent of the polygon. How can I fill in the area with a color? Any help is greatly appreciated.

The code is as follows:

library(rgdal)
setwd("G:\\GIS_SJR\\GIS Shapefiles\\Boundaries")
polygon <- readOGR(".", "20km_buffer_albers")
polygonWGS <- spTransform(polygon, CRS("+proj=longlat +ellps=WGS84 +datum=WGS84"))  
writeOGR(polygonWGS, dsn="polygonWGS.kml", layer="polygonWGS", driver="KML")
3
  • 1
    "Limited support is available for fills, line color and other styling attributes" check their website
    – Gago-Silva
    Jan 15, 2013 at 9:17
  • I have a similar issue. I am studying this web site, gsif.isric.org/doku.php?id=wiki:tutorial_plotkml, my files are in a raster format. try with KML(RAD.all[[100]], file='1998.kml', col=rev(terrain.colors(255)),alpha = 0.5,colNA=NA, maxpixels=100000, blur=2,overwrite=TRUE ) and plotKML(RAD.all[[100]], colour_scale = rev(c(terrain.colors(255))),alpha = 0.5) plotKML works better for me
    – Nahm
    Aug 22, 2014 at 21:22
  • 2
    That web site has soil example too.
    – Nahm
    Aug 22, 2014 at 21:29

1 Answer 1

6

As said in the comments above, the Global Soil Information Facilities (tutorials) website maintained, amongst others, by some of the plotKML authors and contributors gives you everything you need to produce filled polygon areas. In addition to the absolutely correct solution by @Nahm above, here's a reproducible example for future queries on the topic.

## required packages
library(rgdal)
library(rworldmap)
library(plotKML)

## shapefile data
data("countriesCoarse")
spy <- subset(countriesCoarse, continent == "Africa")

## plotKML
data("worldgrids_pal")
plotKML(spy["MAP_COLOR"], filename = "africa.kml",
        colour_scale = worldgrids_pal[["lgn3"]])

africa

You may find extra information on further plotKML settings including supported color palettes here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.