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
#> 3611         90    3611 7794286100 76.43425 15.32886 7794286099 76.43414
#> 1285         35    1285  571423206 76.48734 15.35598 5351515769 76.48789
#> 6201        203    6201 7769271419 76.37263 15.34499 7769190961 76.38203
#> 6209        203    6209 1388482509 76.38860 15.34745 1204772662 76.38895
#> 6273        203    6273 1388483466 76.43520 15.34951 1204772874 76.43822
#> 6201.1      203    6201 7769271419 76.37263 15.34499 7769190961 76.38203
#> 6303        205    6303 7799710765 76.47804 15.32587 7799710766 76.47809
#> 6201.2      203    6201 7769271419 76.37263 15.34499 7769190961 76.38203
#> 6209.1      203    6209 1388482509 76.38860 15.34745 1204772662 76.38895
#> 5931        184    5931 6161030370 76.46631 15.33329 6161030371 76.46593
#>          to_lat           d d_weighted highway    way_id component       time
#> 3611   15.32869   21.903916   23.05675   track 252787544         1  15.770820
#> 1285   15.35570   66.442849  132.88570 primary  53658844         2  47.838851
#> 6201   15.34708 1034.099756 2068.19951 primary 835018468         2 744.551824
#> 6209   15.34748   38.100586   76.20117 primary 835018468         2  27.432422
#> 6273   15.34926  325.065460  650.13092 primary 835018468         2 234.047131
#> 6201.1 15.34708 1034.099756 2068.19951 primary 835018468         2 744.551824
#> 6303   15.32594    9.703626   10.21434   track 835627549         3   6.986611
#> 6201.2 15.34708 1034.099756 2068.19951 primary 835018468         2 744.551824
#> 6209.1 15.34748   38.100586   76.20117 primary 835018468         2  27.432422
#> 5931   15.33319   42.134080   42.13408    path 657845270         1  30.336538
#>        time_weighted
#> 3611       16.600863
#> 1285       95.677702
#> 6201     1489.103648
#> 6209       54.864844
#> 6273      468.094263
#> 6201.1   1489.103648
#> 6303        7.354327
#> 6201.2   1489.103648
#> 6209.1     54.864844
#> 5931       30.336538