Skip to contents

Match spatial points to the edges of a spatial graph, through finding the edge with the closest perpendicular intersection. NOTE: Intersections are calculated geometrically, and presume planar geometry. It is up to users of projected geometrical data, such as those within a dodgr_streetnet object, to ensure that either: (i) Data span an sufficiently small area that errors from presuming planar geometry may be ignored; or (ii) Data are re-projected to an equivalent planar geometry prior to calling this routine.

Usage

match_pts_to_graph(graph, xy, connected = FALSE, distances = FALSE)

Arguments

graph

A dodgr graph with spatial coordinates, such as a dodgr_streetnet object.

xy

coordinates of points to be matched to the vertices, either as matrix or sf-formatted data.frame.

connected

Should points be matched to the same (largest) connected component of graph? If FALSE and these points are to be used for a dodgr routing routine (dodgr_dists, dodgr_paths, or dodgr_flows_aggregate), then results may not be returned if points are not part of the same connected component. On the other hand, forcing them to be part of the same connected component may decrease the spatial accuracy of matching.

distances

If TRUE, return a 'data.frame' object with 'index' column as described in return value; and additional columns with perpendicular distance to nearest edge in graph, and coordinates of points of intersection. See description of return value for details.

Value

For distances = FALSE (default), a vector index matching the xy coordinates to nearest edges. For bi-directional edges, only one match is returned, and it is up to the user to identify and suitably process matching edge pairs. For 'distances = TRUE', a 'data.frame' of four columns:

  • "index" The index of closest edges in "graph", as described above.

  • "d_signed" The perpendicular distance from ech point to the nearest edge, with negative distances denoting points to the left of edges, and positive distances denoting points to the right. Distances of zero denote points lying precisely on the line of an edge (potentially including cases where nearest point of bisection lies beyond the actual edge).

  • "x" The x-coordinate of the point of intersection.

  • "y" The y-coordinate of the point of intersection.

Examples

graph <- weight_streetnet (hampi, wt_profile = "foot")
# Then generate some random points to match to graph
verts <- dodgr_vertices (graph)
npts <- 10
xy <- data.frame (
    x = min (verts$x) + runif (npts) * diff (range (verts$x)),
    y = min (verts$y) + runif (npts) * diff (range (verts$y))
)
edges <- match_pts_to_graph (graph, xy)
graph [edges, ] # The edges of the graph closest to `xy`
#>      geom_num edge_id    from_id from_lon from_lat      to_id   to_lon   to_lat
#> 1711       50    1711 2632626798 76.46957 15.34620 2632626796 76.46953 15.34602
#> 3613       90    3613 7794286099 76.43414 15.32869 7794286098 76.43411 15.32859
#> 835        34     835  286632879 76.44767 15.30570 2398957492 76.44765 15.30586
#> 3405       89    3405 2588146138 76.42439 15.31886 2588146016 76.42477 15.31911
#> 1287       35    1287 5351515769 76.48789 15.35570 1206252310 76.48878 15.35523
#> 2999       82    2999 2195424979 76.45258 15.35231 2195424977 76.45296 15.35224
#> 6243      203    6243 1204772877 76.40606 15.35237 1204772708 76.40809 15.35245
#> 5877      183    5877 1128374399 76.47924 15.31509  313796427 76.47893 15.31474
#> 3479       89    3479 2588146070 76.43557 15.31713 7793307898 76.43574 15.31712
#> 3633       90    3633 7794286091 76.43510 15.32773 7794286090 76.43523 15.32765
#>              d d_weighted      highway    way_id component       time
#> 1711  21.23355   23.59284  residential  84014148         2  15.288159
#> 3613  11.99340   12.62464        track 252787544         1   8.635251
#> 835   17.37563   28.95938    secondary  53626074         1  12.510454
#> 3405  48.82435   61.03044 unclassified 252786290         1  35.153534
#> 1287 108.83993  217.67986      primary  53658844         2  78.364749
#> 2999  41.18963   51.48704 unclassified 209318354         2  29.656533
#> 6243 218.61497  437.22995      primary 835018468         2 157.402782
#> 5877  51.46562  102.93124      primary 652570479         1  37.055247
#> 3479  19.03358   23.79198 unclassified 252786290         1  13.704181
#> 3633  17.16278   18.06608        track 252787544         1  12.357201
#>      time_weighted
#> 1711     16.986843
#> 3613      9.089737
#> 835      20.850756
#> 3405     43.941918
#> 1287    156.729498
#> 2999     37.070667
#> 6243    314.805564
#> 5877     74.110494
#> 3479     17.130226
#> 3633     13.007580