Skip to contents

Random spatial networks are created by randomly sampling nodes within a given area, and connecting them by edges according to a specified method.

Usage

play_geometric(
  n,
  radius,
  bounds = NULL,
  edges_as_lines = TRUE,
  compute_length = FALSE,
  ...
)

Arguments

n

The number of nodes to be sampled.

radius

The radius within which nodes will be connected by an edge.

bounds

The spatial features within which the nodes should be sampled as object of class sf, sfc, sfg or bbox. If set to NULL, nodes will be sampled within a unit square.

edges_as_lines

Should the created edges be spatially explicit, i.e. have LINESTRING geometries stored in a geometry list column? Defaults to TRUE.

compute_length

Should the geographic length of the edges be stored in a column named length? Uses st_length to compute the length of the edge geometries when edges are spatially explicit, and st_distance to compute the distance between boundary nodes when edges are spatially implicit. Please note that the values in this column are not automatically recognized as edge weights. This needs to be specified explicitly when calling a function that uses edge weights. Defaults to FALSE.

...

Additional arguments passed on to st_sample. Ignored if bounds = NULL.

Functions

  • play_geometric(): Creates a random geometric graph. Two nodes will be connected by an edge if the distance between them is within the given radius. If nodes are sampled on a unit square (i.e. when bounds = NULL) this radius is unitless. If bounds are given as a spatial feature, the radius is assumed to be in meters for geographic coordinates, and in the units of the coordinate reference system for projected coordinates. Alternatively, units can also be specified explicitly by providing a units object.

Examples

library(sf, quietly = TRUE)

oldpar = par(no.readonly = TRUE)
par(mar = c(1,1,1,1))

# Sample 10 nodes on a unit square
# Connect nodes by an edge if they are within 0.25 distance from each other
net = play_geometric(10, 0.25)
net
#> # A sfnetwork: 10 nodes and 6 edges
#> #
#> # An undirected simple graph with 5 components and spatially explicit edges
#> #
#> # Dimension: XY
#> # Bounding box: xmin: 0.08561206 ymin: 0.0002388966 xmax: 0.9256447 ymax: 0.9330341
#> # CRS: NA
#> #
#> # Node data: 10 × 1 (active)
#>                   geometry
#>                    <POINT>
#> 1   (0.08561206 0.5636468)
#> 2     (0.20857 0.08998052)
#> 3    (0.2165673 0.3052184)
#> 4 (0.2337034 0.0002388966)
#> 5     (0.333072 0.9330341)
#> 6    (0.6262453 0.7340943)
#> # ℹ 4 more rows
#> #
#> # Edge data: 6 × 3
#>    from    to                                     geometry
#>   <int> <int>                                 <LINESTRING>
#> 1     2     3    (0.20857 0.08998052, 0.2165673 0.3052184)
#> 2     2     4 (0.20857 0.08998052, 0.2337034 0.0002388966)
#> 3     6     7   (0.6262453 0.7340943, 0.6674265 0.5150633)
#> # ℹ 3 more rows

plot(net)


# Sample 10 nodes within a spatial bounding box
# Connect nodes by an edge if they are within 1 km from each other
net = play_geometric(10, units::set_units(1, "km"), bounds = st_bbox(roxel))
net
#> # A sfnetwork: 10 nodes and 32 edges
#> #
#> # An undirected simple graph with 1 component and spatially explicit edges
#> #
#> # Dimension: XY
#> # Bounding box: xmin: 7.52265 ymin: 51.94154 xmax: 7.545803 ymax: 51.95783
#> # Geodetic CRS: WGS 84
#> #
#> # Node data: 10 × 1 (active)
#>              geometry
#>           <POINT [°]>
#> 1 (7.545317 51.95206)
#> 2 (7.545803 51.95209)
#> 3 (7.540433 51.94154)
#> 4 (7.540273 51.94851)
#> 5 (7.535512 51.95356)
#> 6  (7.52265 51.95783)
#> # ℹ 4 more rows
#> #
#> # Edge data: 32 × 3
#>    from    to                               geometry
#>   <int> <int>                       <LINESTRING [°]>
#> 1     1     2 (7.545317 51.95206, 7.545803 51.95209)
#> 2     1     4 (7.545317 51.95206, 7.540273 51.94851)
#> 3     1     5 (7.545317 51.95206, 7.535512 51.95356)
#> # ℹ 29 more rows

plot(net)


par(oldpar)