I have a set of points saved with their respective latitude and longitude in a data frame like this:
points = data.frame(point_id = 1:3,
lat = c("51.7505","51.7502","51.6045"),
long = c("4.8456","4.8453","4.8012")
)
My goal is to create circular polygons with the same defined radius (in this case 200m) around each of these points and export them as a KML file. How would I do that?
CodePudding user response:
Use st_buffer
with a metric projection (you have to st_transform
the crs first).
library(sf)
points <- st_as_sf(points, coords = c("lat", "long"), crs = 4326) %>%
st_transform(20539) %>%
st_buffer(200)
Then use st_write
:
st_write(points , "points.kml", driver = "kml", delete_dsn = TRUE)
CodePudding user response:
points_buffer <- points %>%
# Transform data.frame in Simple Feature object
st_as_sf(coords = c('lat', 'long'), dim = "XY") %>%
# Determine the coordinate system
st_set_crs("EPSG:4326") %>%
# Tranform geographical coordinates to cartesian
# so we can choose the size of the buffer in meters
st_transform( crs = 27700) %>%
# Calculate buffer
st_buffer(dist = 200) %>%
# Return to WGS 84 geographical coordinate system to export in KML
st_transform( crs = 4326)
# Export results
st_write(points_buffer, "points_buffer.kml", driver = "kml", delete_dsn = TRUE)