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
#> 6257      203    6257 1204772830 76.42729 15.35071 1388481903 76.43011 15.35046
#> 485        18     485  339574181 76.44887 15.33268  339574182 76.44817 15.33271
#> 3395       89    3395 2588119056 76.42341 15.31717 2588146107 76.42349 15.31746
#> 859        34     859 2398957540 76.44876 15.30889  676635813 76.44890 15.30911
#> 819        34     819  286632878 76.44887 15.30104  676635779 76.44832 15.30230
#> 3467       89    3467 5351719173 76.43085 15.31804 5351719172 76.43136 15.31796
#> 6201      203    6201 7769271419 76.37263 15.34499 7769190961 76.38203 15.34708
#> 5675      171    5675 5974426256 76.44354 15.31398 5351820892 76.44350 15.31399
#> 761        31     761  571423430 76.47690 15.35360  571423433 76.47649 15.35310
#> 6203      203    6203 7769190961 76.38203 15.34708 1204772804 76.38329 15.34736
#>                d  d_weighted      highway    way_id component       time
#> 6257  304.133008  608.266016      primary 835018468         2 218.975766
#> 485    74.866713   78.807066        track  30704678         1  53.904033
#> 3395   33.008446   41.260558 unclassified 252786290         1  23.766081
#> 859    28.521630   47.536050    secondary  53626074         1  20.535573
#> 819   151.598902  252.664836    secondary  53626074         1 109.151209
#> 3467   55.711284   69.639105 unclassified 252786290         1  40.112125
#> 6201 1035.681414 2071.362827      primary 835018468         2 745.690618
#> 5675    4.550499    5.688124 unclassified 554572321         1   3.276359
#> 761    70.305169   78.116855      service  45064036         2  50.619722
#> 6203  138.666644  277.333288      primary 835018468         2  99.839984
#>      time_weighted
#> 6257    437.951532
#> 485      56.741088
#> 3395     29.707601
#> 859      34.225956
#> 819     181.918682
#> 3467     50.140156
#> 6201   1491.381236
#> 5675      4.095449
#> 761      56.244135
#> 6203    199.679967