Skip to contents

This function computes least-cost path distances between all nodes belonging to two polygons in a gData or gGraph using dijkstraBetween. The polygons are specified via a node attribute layer. Optionally, only outline nodes (nodes with at least one neighbor outside the polygon) are used to reduce computation time.

Usage

polygonBetween(g, layer, from, to, outline = TRUE)

Arguments

g

gData or gGraph object with edge costs already defined.

layer

Character. Name of the node attribute layer containing polygon membership.

from

Character. Name of the first polygon.

to

Character. Name of the second polygon.

outline

Logical. If TRUE, only outline nodes of each polygon (nodes with at least one neighbor outside the polygon) are used. Defaults to TRUE.

Value

A numeric vector containing the least-cost distances between nodes of the two polygons.

Details

The function extracts all nodes belonging to the two specified polygons and computes least-cost paths between them using dijkstraBetween. If outline = TRUE, the computation is restricted to outline nodes of each polygon, which can substantially reduce runtime for large polygons.

Examples

world.countries <- rnaturalearth::ne_countries(
  scale = "medium",
  returnclass = "sf"
)
newGraph <- assignByPolygon(rawgraph.10k,
  layer = world.countries,
  attr = c("continent", "name")
)
#> Spherical geometry (s2) switched off
#> although coordinates are longitude/latitude, st_intersects assumes that they
#> are planar
#> Spherical geometry (s2) switched on
test <- polygonBetween(newGraph, layer = "name", "Spain", "Germany", outline = TRUE)
plot(newGraph, col = NA, reset = TRUE)
#> Spherical geometry (s2) switched off
#> Spherical geometry (s2) switched on
plot(test, col = "red")