I have a dataframe in R with three columns; latitude, longitude and cluster. I would like to export this data into a KML file for visualizing these clusters in Google Earth. Here's a snippet of my data:
structure(list(longitude = c(-73.1576, 109.5244, 109.5235, 109.5237,
109.5244, 19.9022, -73.1575, -73.1574, 109.5235, 19.9069, 109.5242,
19.9023, -73.1579, -73.1578, -73.1594, 19.9051, 109.5242, 19.9009,
-73.1575, -73.1578, -73.1592, -73.1577, 109.5235, 109.5246, 109.5243,
-73.1578, 19.9052, -73.1569, 19.9022, 19.9061, -73.1595, 109.5238,
-73.157, -73.1576, 19.9044, 109.5257, -73.1571, 109.5249, -73.1577,
109.5245, -73.1573, 19.9021, 19.9022, 19.9029, -73.1575, -73.1573,
-73.157, 109.5243, 19.907, 19.9031, -73.1574, -73.1569, 109.5245,
-73.1593, 19.9057, 109.5242, 109.5236, 19.9054, 109.5235, 109.5241,
19.9058, 19.9055, 109.5242, -73.1593, 109.5239, 19.9009, 109.5241,
-73.1572, -73.1572, -73.1571, 109.5235, -73.1576, 109.5243, 19.9031,
19.9021, 19.9021, 19.9008, 109.5245, 19.9022, 109.5239, 19.903,
-73.1583, 109.5242, -73.1575, 19.9045, 19.9008, -73.1574, 19.9008,
-73.1573, 109.5242, -73.1576, 19.9053, 109.5237, -73.1572, -73.157,
19.9061, -73.158, 19.9007, 19.9028, 19.9058, 109.5245, 19.905,
19.9031, -73.1574, -73.1584, 109.5236, 19.9052, 19.9055, 19.9022,
19.9051, 109.5241, 109.5244, -73.1571, -73.157, -73.1583, 109.524,
19.9061, -73.1572, 109.5241, 19.9058, 109.5243, 19.906, 109.5244,
-73.1577, -73.1577, -73.1576, -73.1579, 109.5238, -73.1569, 109.5243,
19.9045, 109.5238, 19.9054, -73.1578, 109.5237, 109.5236, -73.1571,
19.9057, 109.5243, -73.1577, -73.1575, 109.5236, 19.9023, 109.5235,
109.5237, 109.5236, 109.5234, -73.1573, 19.9044, -73.1578, 19.9022
), latitude = c(-37.0423, 21.4841, 21.482, 21.4827, 21.4839,
39.6318, -37.0423, -37.0425, 21.4821, 39.6329, 21.4838, 39.6331,
-37.0423, -37.0423, -37.0423, 39.6329, 21.4834, 39.6315, -37.0426,
-37.0422, -37.0423, -37.0424, 21.4824, 21.4842, 21.4836, -37.0424,
39.6329, -37.0424, 39.6331, 39.6277, -37.0428, 21.4827, -37.0424,
-37.0425, 39.6326, 21.4826, -37.0425, 21.4836, -37.0425, 21.4835,
-37.0423, 39.6304, 39.6332, 39.6325, -37.0422, -37.0426, -37.0423,
21.4841, 39.6329, 39.6334, -37.0424, -37.0423, 21.4842, -37.0422,
39.6325, 21.4837, 21.4825, 39.6329, 21.4822, 21.4837, 39.6326,
39.6329, 21.4835, -37.0423, 21.4829, 39.6316, 21.4835, -37.0423,
-37.0426, -37.0424, 21.4823, -37.0426, 21.4839, 39.6335, 39.6315,
39.6316, 39.6317, 21.484, 39.6316, 21.4828, 39.6325, -37.0426,
21.4833, -37.0425, 39.6325, 39.6315, -37.0426, 39.6316, -37.0425,
21.4836, -37.0424, 39.6331, 21.4824, -37.0425, -37.0425, 39.6276,
-37.0424, 39.6316, 39.6322, 39.6324, 21.4841, 39.6317, 39.6325,
-37.0423, -37.0429, 21.4824, 39.633, 39.633, 39.6317, 39.633,
21.4834, 21.484, -37.0423, -37.0426, -37.0425, 21.4833, 39.6324,
-37.0424, 21.4836, 39.6325, 21.4835, 39.6277, 21.4842, -37.0422,
-37.0423, -37.0422, -37.0424, 21.4828, -37.0425, 21.4837, 39.6326,
21.4829, 39.633, -37.0421, 21.4828, 21.4823, -37.0426, 39.6326,
21.4838, -37.0426, -37.0424, 21.4821, 39.6316, 21.4825, 21.4823,
21.4822, 21.4823, -37.0424, 39.6325, -37.0425, 39.6315), cluster = c(2579,
3050, 3050, 3050, 3050, 5222, 2579, 2579, 3050, 5222, 3050, 5222,
2579, 2579, 2579, 5222, 3050, 5222, 2579, 2579, 2579, 2579, 3050,
3050, 3050, 2579, 5222, 2579, 5222, 5222, 2579, 3050, 2579, 2579,
5222, 3050, 2579, 3050, 2579, 3050, 2579, 5222, 5222, 5222, 2579,
2579, 2579, 3050, 5222, 5222, 2579, 2579, 3050, 2579, 5222, 3050,
3050, 5222, 3050, 3050, 5222, 5222, 3050, 2579, 3050, 5222, 3050,
2579, 2579, 2579, 3050, 2579, 3050, 5222, 5222, 5222, 5222, 3050,
5222, 3050, 5222, 2579, 3050, 2579, 5222, 5222, 2579, 5222, 2579,
3050, 2579, 5222, 3050, 2579, 2579, 5222, 2579, 5222, 5222, 5222,
3050, 5222, 5222, 2579, 2579, 3050, 5222, 5222, 5222, 5222, 3050,
3050, 2579, 2579, 2579, 3050, 5222, 2579, 3050, 5222, 3050, 5222,
3050, 2579, 2579, 2579, 2579, 3050, 2579, 3050, 5222, 3050, 5222,
2579, 3050, 3050, 2579, 5222, 3050, 2579, 2579, 3050, 5222, 3050,
3050, 3050, 3050, 2579, 5222, 2579, 5222)), row.names = c(NA,
-151L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x10200e8e0>)
I created a list of spatialPolygondataframe's containing concave hulls as follows:
points_df <- split(points_df, points_df$cluster)
clusters <- names(points_df)
points_df <- lapply(points_df, function(x) { x$cluster <- NULL; x })
points_df <- lapply(points_df, st_as_sf, coords = c("longitude", "latitude"), crs = "+proj=longlat +datum=WGS84")
points_df <- lapply(points_df, concaveman)
points_df <- lapply(points_df, as_Spatial)
So I ended up with a list of SpatialPolygonsDataFrame's. Now I want to use the kmlPolygon function in maptools to generate a KML file for visualization in Google Earth as follows:
mycolors <- colorRampPalette(brewer.pal(8, "Set2"))(length(clusters))
kmlPolygon(obj=points_df, kmlfile="myfile.kml", col=mycolors)
This throws the following error:
Error in kmlPolygon(obj = points_df, kmlfile = "myfile.kml", col = mycolors) :
obj must be of class 'Polygons' or 'SpatialPolygonsDataFrame' [package 'sp']
Now if I use one element from the list (correctly):
kmlPolygon(obj=points_df[[1]]], kmlfile="myfile.kml", col=mycolors)
everything works and I am able to generate the kml file and visualize it in Google Earth. So my question is, how do I use the whole list of SpatialPolygonsDataFrame's - is there some sort of operation I need to do to combine them or ....?