Extracting points on linestring based on time since from startpoint (sf)

88 Views Asked by At

I have a series of nodes connected by paths currently imported in sf. Each node has a date-time associated with it. For example:

enter image description here

library(sf)
library(ggplot2)

path <- st_as_sf(data.frame(X = c(145, 145.2, 145.8), Y = c(-16, -16.2, -16.5), L1 = 1), coords = c('X', 'Y'), crs = 20353) %>% 
      group_by(L1) %>%
      summarise(do_union = F) %>% 
      st_cast('LINESTRING')

nodes <- st_as_sf(data.frame(X = c(145, 145.2, 145.8), Y = c(-16, -16.2, -16.5), L1 = 1), coords = c('X', 'Y'), crs = 20353) %>%
  mutate(time=seq(as.POSIXct("2020/01/01 12:00:00 UTZ"), as.POSIXct("2020/01/01 13:00:00 UTZ"), by = "30 mins"))

ggplot() + xlim(144.9, 146) + 
  geom_sf(data=nodes) +
  geom_sf(data=path) +
  geom_sf_text(data=nodes, aes(label=time), nudge_y = 0.025)

Based on these answers, I've worked out how to sample along the linestring path at known distances from the startpoint using st_line_sample, but instead I'd like to identify the point along the path at either a known time, for example at 2020-01-01 12:15:00 AEST (or here 15 minutes since the start-point):

enter image description here

subpath <- st_as_sf(data.frame(X = c(145, 145.1), Y = c(-16, -16.1), L1 = 1), coords = c('X', 'Y'), crs = 20353) %>% 
      group_by(L1) %>%
      summarise(do_union = F) %>% 
      st_cast('LINESTRING')

subnodes <- st_as_sf(data.frame(X = c(145, 145.1), Y = c(-16, -16.1), L1 = 1), coords = c('X', 'Y'), crs = 20353) %>%
  mutate(time=c("2020-01-01 12:00:00 AEST", "2020-01-01 12:15:00 AEST"))

ggplot() + xlim(144.9, 146) +
  geom_sf(data=nodes, size=1.5) +
  geom_sf(data=path) +
  geom_sf(data=subpath, color="red", size=2) +
  geom_sf(data=subnodes, color="red") +
  geom_sf_text(data=(tail(nodes,2)), aes(label=time), color="black", nudge_y = 0.025) +
  geom_sf_text(data=subnodes, aes(label=time), color="red", nudge_y = 0.025) 

In the above example 12:15 (conveniently) falls halfway between 12:00 and 12:30 in distance, but the full dataset has thousands of points/paths grouped by an id, and requires sampling at random timepoints along each path. I've tried several attempts with sfnetworks and sftracks packages but I can't find an eloquent or straight forward solution.

0

There are 0 best solutions below