Using the below I output a KML file from a Large SpatialPolygonsDataFrame

polygonWGS <- spTransform(Polygon, CRS("+proj=longlat +ellps=WGS84 
writeOGR(polygonWGS, dsn="polygonWGS1.kml", layer="Area", driver="KML", 
overwrite = TRUE)

However, the resulting attributes table is blank in this KML. Specicially I'm interested in having the polygons named as the 'Area' name.

Is this possible?

| improve this question | | | | |
  • Can you show us a bit more info about your Polygon object? Maybe it doesn't have any attributes? summary(Polygon) and summary(polygonWGS) would help enormously if you can't demonstrate your problem with data you can share or other public data. – Spacedman Aug 8 '18 at 16:29
  • If I use Polygon@data and PolygonWGS@data the output is a list of 'Areas' with their full name – Rtab Aug 8 '18 at 16:48

Use sf package:

Reproducible example

# simple example, from scratch:
Sr1 = Polygon(cbind(c(2,4,4,1,2),c(2,3,5,4,2)))
Sr2 = Polygon(cbind(c(5,4,2,5),c(2,3,2,2)))
Sr3 = Polygon(cbind(c(4,4,5,10,4),c(5,3,2,5,5)))
Sr4 = Polygon(cbind(c(5,6,6,5,5),c(4,4,3,3,4)), hole = TRUE)

Srs1 = Polygons(list(Sr1), "s1")
Srs2 = Polygons(list(Sr2), "s2")
Srs3 = Polygons(list(Sr3, Sr4), "s3/4")
SpP = SpatialPolygons(list(Srs1,Srs2,Srs3), 1:3)
plot(SpP, col = 1:3, pbg="white")

grd <- GridTopology(c(1,1), c(1,1), c(10,10))
polys <- as(grd, "SpatialPolygons")
centroids <- coordinates(polys)
x <- centroids[,1]
y <- centroids[,2]
z <- 1.4 + 0.1*x + 0.2*y + 0.002*x*x
ex_1.7 <- SpatialPolygonsDataFrame(polys,
                                   data=data.frame(x=x, y=y, z=z, row.names=row.names(polys)))

Now, save it with sf:


st_write(st_as_sf(ex_1.7),'layer_name.kml', driver = 'kml')

enter image description here

| improve this answer | | | | |
  • the names of the layer don't show up in the table of contents and this info screen doesn't update when I double-click on a new polygon to navigate there. If you have experience filling in the name attribute of the kml so that it appears in the table of contents, consider my question, here: stackoverflow.com/questions/57415464/… – Nova Aug 8 '19 at 15:09

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