I’ve got a set of points (in long and lat), and a single multilinestring. Is it possible to get an adjacency matrix, of distances ALONG this line?
Similar to this answer here, but the points are given, instead of generated: Combine Multilinestring into one line string to generate regular spaced points using sf
reprex below:
library(sf)
library(SSNdata)
library(osmdata)
library(igraph)
library(tidyverse)
## Import data.frame containing observations and covariates
clear <- readRDS(system.file("extdata/clear_obs.RDS", package = "SSNdata"))
# Get the river lines
lines <- (opq(bbox = clear$river %>%
gsub(pattern = "_", replacement = " ") %>%
unique %>%
str_to_title,
timeout = 2000,
memsize = 10e8) %>%
add_osm_feature(key = 'waterway', value = c('river', 'stream')) %>%
osmdata_sf())$osm_lines
sensors <- structure(
list(
section = c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12),
geometry = structure(
list(structure(c(169.893668510998, -43.4485707831223), class = c("XY", "POINT", "sfg")),
structure(c(169.905356945663, -43.4501164165107), class = c("XY", "POINT", "sfg")),
structure(c(169.9072738056,-43.4465855404346), class = c("XY", "POINT", "sfg")),
structure(c(169.9019276, -43.4494298), class = c("XY", "POINT", "sfg")),
structure(c(169.88333135407, -43.451279558067), class = c("XY", "POINT", "sfg")),
structure(c(169.883056036222,-43.4554894608215), class = c("XY", "POINT", "sfg")),
structure(c(169.898097916634, -43.4588427716324), class = c("XY", "POINT", "sfg")),
structure(c(169.88278569869, -43.4602902410717), class = c("XY", "POINT", "sfg")),
structure(c(169.924591882517, -43.4496436972259), class = c("XY", "POINT", "sfg")),
structure(c(169.919694386786, -43.4442892230613), class = c("XY", "POINT", "sfg")),
structure(c(169.8837607, -43.4711193), class = c("XY", "POINT", "sfg")),
structure(c(169.918834656941, -43.4659988545691), class = c("XY", "POINT", "sfg")),
structure(c(169.902468538258, -43.4638261136778), class = c("XY", "POINT", "sfg"))),
n_empty = 0L,
crs = structure(
list(input = "WGS 84",
wkt = "GEOGCRS["WGS 84",n DATUM["World Geodetic System 1984",n ELLIPSOID["WGS 84",6378137,298.257223563,n LENGTHUNIT["metre",1]]],n PRIMEM["Greenwich",0,n ANGLEUNIT["degree",0.0174532925199433]],n CS[ellipsoidal,2],n AXIS["latitude",north,n ORDER[1],n ANGLEUNIT["degree",0.0174532925199433]],n AXIS["longitude",east,n ORDER[2],n ANGLEUNIT["degree",0.0174532925199433]],n ID["EPSG",4326]]"),
class = "crs"),
class = c("sfc_POINT","sfc"),
precision = 0,
bbox = structure(c(xmin = 169.88278569869,ymin = -43.4711193, xmax = 169.924591882517, ymax = -43.4442892230613), class = "bbox"))),
row.names = c(NA, -13L),
class = c("sf","data.frame"), sf_column = "geometry",
agr = structure(c(FID = NA_integer_), class = "factor",
levels = c("constant", "aggregate", "identity")))
# Convert to a multilinestring
concat <- lines %>%
st_touches() %>%
graph_from_adj_list() %>%
components() %>%
pluck(membership)
concat_lines <- group_by(
lines,
section = as.character({{concat}})
) %>%
summarise()
# Get the main multilinestring of interest
main_line <- concat_lines[2,]
line <- main_line %>% st_as_sfc
point <- sensors %>% st_as_sfc
distances <- sf::st_line_project(line, point)
from the dev version of sf
on GitHub has this solution, but it doesn’t work on my use case (keeps giving, Error: Incompatible lengths: 141, 13
)