Replies: 2 comments 4 replies
-
Hi and thanks for your question! I looked again at the documentation of The main problem is the following: The only way around this currently, is that you do the step of finding the indices of the nearest nodes yourself. In that way you have the overview of which of your provided "to" points are actually missing in the cost matrix. library(sfnetworks)
#> Registered S3 method overwritten by 'spatstat.geom':
#> method from
#> print.boxx cli
library(sf)
#> Linking to GEOS 3.9.0, GDAL 3.2.1, PROJ 7.2.1
# Create a network.
net = as_sfnetwork(roxel, directed = FALSE, length_as_weight = TRUE)
# Create a set of points to calculate the cost matrix with.
p1 = st_sfc(st_point(c(7.5321, 51.9533)), crs = 4326)
p2 = st_sfc(st_point(c(7.5322, 51.9532)), crs = 4326)
p3 = st_sfc(st_point(c(7.5337, 51.9555)), crs = 4326)
pts = c(p1, p2, p3)
# Find the nearest feature in the nodes table of the network to each point.
# Note that the first two points have the same nearest node.
nearest = st_nearest_feature(pts, st_geometry(net, "nodes"))
#> although coordinates are longitude/latitude, st_nearest_feature assumes that they are planar
nearest
#> [1] 4 4 1
# Retrieve the cost matrix.
# Note that we retrieve a 3x2 matrix instead of a 3x3 one.
# That is: igraph forced us to remove duplicated "to" indices, but *not* duplicated "from" indices.
costs = st_network_cost(net, from = nearest, to = nearest)
#> Warning: Duplicated values in argument 'to' were removed.
costs
#> [,1] [,2]
#> [1,] 0.0000 354.6676
#> [2,] 0.0000 354.6676
#> [3,] 354.6676 0.0000
# Map each of your original point geometries to a column index in the cost matrix.
# The second point will have the cost values towards it stored in the first column, since it has the same nearest node as the first point.
col_idxs = match(nearest, unique(nearest))
col_idxs
#> [1] 1 1 2
# So if we want to obtain the travel cost from the third point to the second point, we find is as follows:
costs[3, col_idxs[2]]
#> [1] 354.6676 Created on 2021-05-13 by the reprex package (v2.0.0) Not sure if this exactly answers your question, but it may help you a step further. |
Beta Was this translation helpful? Give feedback.
-
Just as a (late) update: since version 0.6 the |
Beta Was this translation helpful? Give feedback.
-
Hello again!
When using the st_network_cost function sometimes I want to visualize some particular shortest paths returned from from the cost matrix. This is straightforward when nrow(cost_matrix)= nrow(from_argument) & ncol(cost_matrix)=nrow(to_argument). However, often, the ncol(cost_matrix) < nrow(to_argument). This seems to make it difficult to index the closest value in the to_argument for a given shortest path for subsequent visualization
I believe the issue arises due to this warning: "duplicated values in argument 'to' were removed" and the code below
if (any(duplicated(to))) {
warning("Duplicated values in argument 'to' were removed.",
call. = FALSE)
to = unique(to)
}
It makes sense to me to remove the duplicated "to" nodes prior to the calculation, but I am wondering what is the best way to use the returned cost_matrix to to index the closest "to" argument for a particular shortest path.
I hope this makes sense, thanks again for the great work on this!
Beta Was this translation helpful? Give feedback.
All reactions