Skip to contents

Groups a dataset containing points of interest based on their proximity to a source dataset. Proximity can be defined through nearest neighbors (argument n) and/or a distance buffer (radius). The grouped output can be used as an input to ors_shortest_distances. Note that unlike other rors functions, this function is not based on routing but on linear Euclidean distances and is primarily used to pre-select points of interest and save computing time.

Usage

group_by_proximity(src, dst, n = NULL, radius = NULL)

Arguments

src

[sf/sfc]

Source dataset containing point geometries that should be routed from.

dst

[sf]

Dataset that represents a list of destination points to be routed to for each row in the source dataset. For each point in src, a number of nearest points in dst is selected.

n

[numeric]

Maximum number of points of interest around each point in the source dataset that shall be returned. The actual number might be lower depending on the rows in the dst dataset and the remaining number of points if radius is not NULL. If NULL, radius must be provided.

radius

[numeric]

Maximum distance of a point of interest around each point in the source dataset. All returned points of interest lie within this distance to the source points. If NULL, n must be provided.

Value

Returns an sf dataframe containing a .group specifying the row in src for which the closest point in dst is specified. The geometry column can contain duplicated geometries because a point can be a closest point to multiple rows in src.

Examples

library(sf)
#> Linking to GEOS 3.10.2, GDAL 3.4.1, PROJ 8.2.1; sf_use_s2() is TRUE
src <- pharma[1:4, ]
dst <- pharma[5:8, ]

# group by n nearest points
group_by_proximity(src, dst, n = 2)
#> Simple feature collection with 8 features and 1 field
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: -0.7303072 ymin: 52.6507 xmax: -0.4780519 ymax: 52.66998
#> Geodetic CRS:  WGS 84
#> # A tibble: 8 × 2
#>   .group              geometry
#>    <int>           <POINT [°]>
#> 1      1 (-0.7303072 52.66998)
#> 2      1  (-0.4783052 52.6507)
#> 3      2 (-0.7303072 52.66998)
#> 4      2 (-0.4780519 52.65265)
#> 5      3 (-0.7303072 52.66998)
#> 6      3 (-0.4780519 52.65265)
#> 7      4 (-0.7303072 52.66998)
#> 8      4  (-0.4783052 52.6507)

# group by distance threshold
group_by_proximity(src, dst, radius = 5000)
#> Simple feature collection with 2 features and 1 field
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: -0.7303072 ymin: 52.66998 xmax: -0.7303072 ymax: 52.66998
#> Geodetic CRS:  WGS 84
#> # A tibble: 2 × 2
#>   .group              geometry
#>    <int>           <POINT [°]>
#> 1      2 (-0.7303072 52.66998)
#> 2      3 (-0.7303072 52.66998)

# group by distance and then by nearest points
group_by_proximity(src, dst, n = 2, radius = 5000)
#> Warning: ! Argument `n` is greater than the number of items in `dst`
#>  `n` changed to `length(dst)`.
#> Warning: ! Argument `n` is greater than the number of items in `dst`
#>  `n` changed to `length(dst)`.
#> # A tibble: 2 × 2
#>   .group              geometry
#>    <int>           <POINT [°]>
#> 1      2 (-0.7303072 52.66998)
#> 2      3 (-0.7303072 52.66998)