create two sets of points using latitude and longitutde coordinates then using st_cast draw a line connecting each set of two points in r in r

library(sf)

# Creating two sets of points using latitude and longitude coordinates
set1 <- matrix(c(-77.0369, 38.9072,
                  -74.0060, 40.7128), ncol = 2, byrow = TRUE)
set2 <- matrix(c(-73.935242, 40.73061,
                  -80.191790, 25.761680), ncol = 2, byrow = TRUE)

# Creating a data frame for each set
df1 <- data.frame(lon = set1[,1], lat = set1[,2])
df2 <- data.frame(lon = set2[,1], lat = set2[,2])

# Creating sf points
pts1 <- st_as_sf(df1, coords = c("lon", "lat"), crs = 4326)
pts2 <- st_as_sf(df2, coords = c("lon", "lat"), crs = 4326)

# Creating lines connecting each set of two points
lines <- st_cast(st_combine(st_sfc(pts1, pts2)), "LINESTRING")

# Plotting the lines
plot(lines, col = "blue", lwd = 2)
739 chars
22 lines

This code creates two sets of points using latitude and longitude coordinates, converts them into sf points, and then uses st_cast to draw lines connecting each set of two points.

related categories

gistlibby LogSnag