These functions allow to query specific coordinate values from the geometries of the nodes.
Details
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.
Examples
library(sf, quietly = TRUE)
library(tidygraph, quietly = TRUE)
# Create a network.
net = as_sfnetwork(roxel)
# Use query function in a filter call.
filtered = net |>
activate(nodes) |>
filter(node_X() > 7.54)
oldpar = par(no.readonly = TRUE)
par(mar = c(1,1,1,1))
plot(net, col = "grey")
plot(filtered, col = "red", add = TRUE)
par(oldpar)
# Use query function in a mutate call.
net |>
activate(nodes) |>
mutate(X = node_X(), Y = node_Y())
#> # A sfnetwork: 987 nodes and 1215 edges
#> #
#> # A directed multigraph with 9 components and spatially explicit edges
#> #
#> # Dimension: XY
#> # Bounding box: xmin: 7.522595 ymin: 51.94151 xmax: 7.546705 ymax: 51.96119
#> # Geodetic CRS: WGS 84
#> #
#> # Node data: 987 × 3 (active)
#> geometry X Y
#> <POINT [°]> <dbl> <dbl>
#> 1 (7.538109 51.95286) 7.54 52.0
#> 2 (7.537867 51.95282) 7.54 52.0
#> 3 (7.537815 51.95867) 7.54 52.0
#> 4 (7.537015 51.95848) 7.54 52.0
#> 5 (7.533441 51.95578) 7.53 52.0
#> 6 (7.533415 51.95561) 7.53 52.0
#> # ℹ 981 more rows
#> #
#> # Edge data: 1,215 × 5
#> from to name type geometry
#> <int> <int> <chr> <chr> <LINESTRING [°]>
#> 1 1 2 Hagemanns Kämpken residential (7.538109 51.95286, 7.537867 51.95…
#> 2 3 4 Stiegkamp residential (7.537815 51.95867, 7.537015 51.95…
#> 3 5 6 Havixbecker Straße residential (7.533441 51.95578, 7.533467 51.95…
#> # ℹ 1,212 more rows
# Use query function directly.
X = with_graph(net, node_X())
head(X)
#> [1] 7.538109 7.537867 7.537815 7.537015 7.533441 7.533415