create a dataset with 2 sets of coordinates in latitude and longitude in 4 columns. then using st_as_sf convert these into 2 geometry columns in r

# Create a dataset with 2 sets of coordinates in latitude and longitude in 4 columns
data <- data.frame(
  id = c(1, 2, 3),
  lat1 = c(40.7128, 48.8566, 51.5074),
  lon1 = c(-74.0060, 2.3522, -0.1278),
  lat2 = c(34.0522, 33.6844, 39.9042),
  lon2 = c(-118.2437, -117.8265, 116.4074)
)

# Convert the dataset to a spatial object using st_as_sf
library(sf)

# Convert the dataset to spatial points data frame
spatial_data <- st_as_sf(data, coords = c('lon1', 'lat1'), crs = 4326)

# Add the second set of coordinates
spatial_data$geometry <- st_sfc(st_multipoint(cbind(data$lon1, data$lat1),
                                               cbind(data$lon2, data$lat2)), crs = 4326)

# Print the spatial object
print(spatial_data)
728 chars
22 lines

related categories

gistlibby LogSnag