make a dataset different sets of coordinates in 4 different columns, then in longitude and latitude format, then using st_as_sf convert these into 2 geometry columns in r in r

library(sf)
library(dplyr)

# Create a dataframe with different sets of coordinates in 4 different columns
data <- data.frame(
  id = 1:5,
  lon1 = c(-122.335167, -122.313248, -122.207833, -122.327584, -122.324042),
  lat1 = c(47.608013, 47.638719, 47.841325, 47.607076, 47.617501),
  lon2 = c(-122.335035, -122.312630, -122.199942, -122.335707, -122.319662),
  lat2 = c(47.608714, 47.637843, 47.831253, 47.606138, 47.613111)
)

# Convert the dataframe to spatial features
data_sf <- st_as_sf(data, coords = c("lon1", "lat1"), crs = 4326) %>%
  st_set_geometry(NULL) %>%
  st_sf(coords = c("lon1", "lat1", "lon2", "lat2"))

data_sf
632 chars
19 lines

related categories

gistlibby LogSnag