Skip to contents

These functions allow to interpret spatial relations between edges and other geospatial features directly inside filter and mutate calls. All functions return a logical vector of the same length as the number of edges in the network. Element i in that vector is TRUE whenever the chosen spatial predicate applies to the spatial relation between the i-th edge and any of the features in y.

Usage

edge_intersects(y, ...)

edge_is_disjoint(y, ...)

edge_touches(y, ...)

edge_crosses(y, ...)

edge_is_within(y, ...)

edge_contains(y, ...)

edge_contains_properly(y, ...)

edge_overlaps(y, ...)

edge_equals(y, ...)

edge_covers(y, ...)

edge_is_covered_by(y, ...)

edge_is_within_distance(y, ...)

edge_is_nearest(y)

Arguments

y

The geospatial features to test the edges against, either as an object of class sf or sfc.

...

Arguments passed on to the corresponding spatial predicate function of sf. See geos_binary_pred. The argument sparse should not be set.

Value

A logical vector of the same length as the number of edges in the network.

Details

See geos_binary_pred for details on each spatial predicate. The function edge_is_nearest instead wraps around st_nearest_feature and returns TRUE for element i if the i-th edge is the nearest edge to any of the features in y.

Just as with all query functions in tidygraph, these functions are meant to be called inside tidygraph verbs such as mutate or filter, where the network that is currently being worked on is known and thus not needed as an argument to the function. If you want to use an algorithm outside of the tidygraph framework you can use with_graph to set the context temporarily while the algorithm is being evaluated.

Note

Note that edge_is_within_distance is a wrapper around the st_is_within_distance predicate from sf. Hence, it is based on 'as-the-crow-flies' distance, and not on distances over the network.

Examples

library(sf, quietly = TRUE)
library(tidygraph, quietly = TRUE)

# Create a network.
net = as_sfnetwork(roxel) |>
  st_transform(3035)

# Create a geometry to test against.
p1 = st_point(c(4151358, 3208045))
p2 = st_point(c(4151340, 3207520))
p3 = st_point(c(4151756, 3207506))
p4 = st_point(c(4151774, 3208031))

poly = st_multipoint(c(p1, p2, p3, p4)) |>
  st_cast('POLYGON') |>
  st_sfc(crs = 3035)

# Use predicate query function in a filter call.
intersects = net |>
  activate(edges) |>
  filter(edge_intersects(poly))

oldpar = par(no.readonly = TRUE)
par(mar = c(1,1,1,1))
plot(st_geometry(net, "edges"))
plot(st_geometry(intersects, "edges"), col = "red", lwd = 2, add = TRUE)

par(oldpar)

# Use predicate query function in a mutate call.
net |>
  activate(edges) |>
  mutate(disjoint = edge_is_disjoint(poly)) |>
  select(disjoint)
#> # A sfnetwork: 987 nodes and 1215 edges
#> #
#> # A directed multigraph with 9 components and spatially explicit edges
#> #
#> # Dimension: XY
#> # Bounding box: xmin: 4150707 ymin: 3206375 xmax: 4152366 ymax: 3208564
#> # Projected CRS: ETRS89-extended / LAEA Europe
#> #
#> # Edge data: 1,215 × 4 (active)
#>    from    to disjoint                                                  geometry
#>   <int> <int> <lgl>                                             <LINESTRING [m]>
#> 1     1     2 TRUE                            (4151782 3207612, 4151765 3207609)
#> 2     3     4 TRUE                            (4151784 3208259, 4151728 3208240)
#> 3     5     6 FALSE    (4151472 3207948, 4151474 3207941, 4151473 3207934, 4151…
#> 4     7     8 TRUE                            (4150948 3207637, 4150990 3207647)
#> 5     9    10 FALSE          (4151393 3207929, 4151388 3207948, 4151383 3207963)
#> 6    11    12 TRUE           (4152127 3207036, 4152139 3207043, 4152146 3207047)
#> # ℹ 1,209 more rows
#> #
#> # Node data: 987 × 1
#>            geometry
#>         <POINT [m]>
#> 1 (4151782 3207612)
#> 2 (4151765 3207609)
#> 3 (4151784 3208259)
#> # ℹ 984 more rows

# Use predicate query function directly.
intersects = with_graph(net, edge_intersects(poly))
head(intersects)
#> [1] FALSE FALSE  TRUE FALSE  TRUE FALSE