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_points_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
#> 4633      133    4633 2398957752 76.46191 15.32219  338904911 76.46199 15.32206
#> 3395       89    3395 2588119056 76.42341 15.31717 2588146107 76.42349 15.31746
#> 1317       36    1317 8615359666 76.45910 15.33583 8615359667 76.45873 15.33588
#> 6201      203    6201 7769271419 76.37263 15.34499 7769190961 76.38203 15.34708
#> 4443      122    4443 8632960966 76.47228 15.32009 8632960965 76.47220 15.31997
#> 3421       89    3421 2588146013 76.42587 15.31860 7793366194 76.42601 15.31860
#> 3433       89    3433 2588146085 76.42675 15.31926 2588146132 76.42707 15.31947
#> 6253      203    6253 6597300510 76.42343 15.35101 1388482473 76.42580 15.35076
#> 6239      203    6239 1388482647 76.40387 15.35230 1204772675 76.40457 15.35226
#> 6247      203    6247 1204772780 76.40951 15.35236 1204772868 76.41809 15.35157
#>               d d_weighted      highway    way_id component      time
#> 4633   16.34059   27.23432    secondary 327102382         1  11.76523
#> 3395   33.13677   41.42096 unclassified 252786290         1  23.85847
#> 1317   39.77370   39.77370         path  84006211         1  28.63707
#> 6201 1034.09976 2068.19951      primary 835018468         2 744.55182
#> 4443   15.93157   15.93157         path 266144436         1  11.47073
#> 3421   15.13601   18.92001 unclassified 252786290         1  10.89792
#> 3433   41.57680   51.97100 unclassified 252786290         1  29.93530
#> 6253  255.05491  510.10982      primary 835018468         2 183.63954
#> 6239   75.60595  151.21190      primary 835018468         2  54.43628
#> 6247  924.25080 1848.50160      primary 835018468         2 665.46058
#>      time_weighted
#> 4633      19.60871
#> 3395      29.82309
#> 1317      28.63707
#> 6201    1489.10365
#> 4443      11.47073
#> 3421      13.62241
#> 3433      37.41912
#> 6253     367.27907
#> 6239     108.87257
#> 6247    1330.92115