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
#> 5925      183    5925 2398957516 76.47461 15.30765  676635868 76.47446 15.30734
#> 2733       70    2733 4474520380 76.48887 15.32820 4474520379 76.48903 15.32825
#> 4563      129    4563 5358983985 76.45981 15.32519 1148815010 76.45980 15.32528
#> 3395       89    3395 2588119056 76.42341 15.31717 2588146107 76.42349 15.31746
#> 6237      203    6237 1388483320 76.40250 15.35238 1388482647 76.40387 15.35230
#> 6209      203    6209 1388482509 76.38860 15.34745 1204772662 76.38895 15.34748
#> 6241      203    6241 1204772675 76.40457 15.35226 1204772877 76.40606 15.35237
#> 6211      203    6211 1204772662 76.38895 15.34748 1204772772 76.38938 15.34775
#> 6201      203    6201 7769271419 76.37263 15.34499 7769190961 76.38203 15.34708
#> 6255      203    6255 1388482473 76.42580 15.35076 1204772830 76.42729 15.35071
#>               d d_weighted      highway    way_id component       time
#> 5925   37.85114   75.70227      primary 652570479         1  27.252818
#> 2733   18.28658   18.28658         path 123463598         1  13.166338
#> 4563   10.67810   17.79684    secondary 327102372         1   7.688233
#> 3395   33.00845   41.26056 unclassified 252786290         1  23.766081
#> 6237  146.93904  293.87809      primary 835018468         2 105.796112
#> 6209   38.16886   76.33773      primary 835018468         2  27.481581
#> 6241  160.21420  320.42840      primary 835018468         2 115.354224
#> 6211   55.47203  110.94405      primary 835018468         2  39.939860
#> 6201 1035.68141 2071.36283      primary 835018468         2 745.690618
#> 6255  160.53814  321.07628      primary 835018468         2 115.587460
#>      time_weighted
#> 5925      54.50564
#> 2733      13.16634
#> 4563      12.81372
#> 3395      29.70760
#> 6237     211.59222
#> 6209      54.96316
#> 6241     230.70845
#> 6211      79.87972
#> 6201    1491.38124
#> 6255     231.17492