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
#> 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  510.99150 1021.98301      primary 835018468         2 367.91388
#> 6259  165.84192  331.68383      primary 835018468         2 119.40618
#> 3403   31.45750   39.32187 unclassified 252786290         1  22.64940
#> 3395   33.13677   41.42096 unclassified 252786290         1  23.85847
#> 5875   60.47517  120.95034      primary 652570479         1  43.54212
#> 6201 1034.09976 2068.19951      primary 835018468         2 744.55182
#> 1281  121.93710  243.87420      primary  53658844         2  87.79471
#> 2399   16.04934   16.04934         path 123463598         1  11.55552
#> 5301   20.79553   21.89003        track 388983289         2  14.97278
#> 2615   26.73955   26.73955         path 123463598         1  19.25248
#>      time_weighted
#> 6207     735.82777
#> 6259     238.81236
#> 3403      28.31175
#> 3395      29.82309
#> 5875      87.08424
#> 6201    1489.10365
#> 1281     175.58942
#> 2399      11.55552
#> 5301      15.76082
#> 2615      19.25248