sf2KML | R Documentation |
This function creates KML files from 'sf' objects. Users can specify colours for the placemarks, which are then mapped to predefined URLs for icons in those colours. If no colour is specified, a default icon is used.
sf2KML(sf_object, colour = NULL, outputPath = NULL)
sf_object |
An 'sf' object containing the spatial data to be plotted. Must have an 'ID' column for placemark labels and geometry for coordinates. Must be of Geometry type: POINT. |
colour |
Optional; a character string specifying the colour of the placemark pins. Valid options are "green", "blue", "red", "purple", "lightblue", "pink", and "white". Defaults to NULL, which uses the default yellow colour. |
outputPath |
Optional; the file path where the KML file will be saved. If not specified, the file is saved in the current working directory with a name based on the ‘sf' object’s name. |
Writes a KML file to the specified or default path.
# Start with a simple data.frame with three columns: "ID", "X", and "Y"
Flinders_University <- data.frame(
ID = c("MELFU", "Tavern", "Animal House", "Aquaculture compound", "Lake", "$1,360 car park"),
X = c(138.570071, 138.571627, 138.569855, 138.569586, 138.572218, 138.569437),
Y = c(-35.026967, -35.026029, -35.028127, -35.027034, -35.026907, -35.029019)
)
# Convert the data frame to an sf object
library(sf)
Flinders_sf <- st_as_sf(Flinders_University, coords = c("X", "Y"), crs = 4326)
# Generate the KML file
sf2KML(Flinders_sf, colour = "red", outputPath = "Flinders_University.kml")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.