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)

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.

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
#> 6207      203    6207 1388481924 76.38383 15.34743 1388482509 76.38860 15.34745
#> 6259      203    6259 1388481903 76.43011 15.35046 3921525779 76.43165 15.35029
#> 3403       89    3403 7793366198 76.42424 15.31863 2588146138 76.42439 15.31886
#> 3395       89    3395 2588119056 76.42341 15.31717 2588146107 76.42349 15.31746
#> 5875      183    5875  313796426 76.47957 15.31553 1128374399 76.47924 15.31509
#> 6201      203    6201 7769271419 76.37263 15.34499 7769190961 76.38203 15.34708
#> 1281       35    1281 1206252958 76.48546 15.35660  571423205 76.48656 15.35631
#> 2399       70    2399 7799711029 76.47534 15.33166 7799711030 76.47548 15.33171
#> 5301      158    5301 3921524453 76.44483 15.34252 3921524452 76.44502 15.34246
#> 2615       70    2615 1376768826 76.48596 15.33405 1376769204 76.48618 15.33392
#>               d d_weighted      highway    way_id component      time
#> 6207  511.92811 1023.85621      primary 835018468         2 368.58824
#> 6259  166.13032  332.26064      primary 835018468         2 119.61383
#> 3403   31.37821   39.22276 unclassified 252786290         1  22.59231
#> 3395   33.00845   41.26056 unclassified 252786290         1  23.76608
#> 5875   60.34322  120.68645      primary 652570479         1  43.44712
#> 6201 1035.68141 2071.36283      primary 835018468         2 745.69062
#> 1281  122.10375  244.20750      primary  53658844         2  87.91470
#> 2399   16.06702   16.06702         path 123463598         1  11.56826
#> 5301   20.81961   21.91537        track 388983289         2  14.99012
#> 2615   26.74674   26.74674         path 123463598         1  19.25765
#>      time_weighted
#> 6207     737.17647
#> 6259     239.22766
#> 3403      28.24039
#> 3395      29.70760
#> 5875      86.89424
#> 6201    1491.38124
#> 1281     175.82940
#> 2399      11.56826
#> 5301      15.77907
#> 2615      19.25765