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?

closed as unclear what you're asking by Spacedman, Jochen Schwarze, xunilk, csk, Andre Silva Aug 11 at 19:38

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the 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 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 at 16:48
up vote 0 down vote accepted

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

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