sftraj 步骤说明准确地描述了我的目标:https://mablab.org/post/sftraj-model/:
这个解决方案也很有帮助:Plotting lines between two sf POINT features in r
library(readr)
library(sf)
library(tidyverse)
# Read-in GPS data. Data is in ascending order by date.
gps <- read_csv("X:/gps_xyt_data.csv")
# Create a unique ID column starting at 1
gps <- tibble::rowid_to_column(gps, "KeyID")
# Create a second unique ID column starting at 0
gps$JoinID <- gps$KeyID - 1
# Select coordinates and KeyId
start_xy <- gps %>% select(start_x = utm_e, start_y = utm_n, KeyID)
# Select coordinates and JoinID
end_xy <- gps %>% select(end_x = utm_e, end_y = utm_n, JoinID)
# Inner join to have start/end coordinate pairs for each record
start_end_xy <- inner_join(start_xy, end_xy, by = c("KeyID" = "JoinID"))
# Select for start geometries and convert to sf object
pnts_start <- start_end_xy %>% st_as_sf( coords = c("start_x", "start_y"), crs = 26911)
# Select for end geometries and convert to sf object
pnts_end <- start_end_xy %>% st_as_sf(coords = c("end_x", "end_y"), crs = 26911)
# Combine start and end geometries
cbind(pnts_start,pnts_end) -> points_ready
# Generate line segments via union of paired geometries
line_segments <- as.data.frame(st_sfc(mapply(function(a,b){st_cast(st_union(a,b),"LINESTRING")}, points_ready$geometry, points_ready$geometry.1, SIMPLIFY=FALSE)))
# Add a unique ID to line segments
line_segments <- tibble::rowid_to_column(trj_lines, "KeyID")
# Join the attribute data to line segments
line_segments <- inner_join(line_segments, gps, by = c("KeyID" = "KeyID"))