0

我有一个具有空间显式边的有根树,它只包含一个边和两个节点。

数据

n01 = st_sfc(st_point(c(0, 0)))
n02 = st_sfc(st_point(c(0, 10)))

from = c(1)
to = c(2)

nodes = st_as_sf(c(n01, n02))
edges = data.frame(from = from, to = to)

G = sfnetwork(nodes, edges) %>%
  convert(to_spatial_explicit, .clean = TRUE)
> G
# A sfnetwork with 2 nodes and 1 edges
#
# CRS:  NA 
#
# A rooted tree with spatially explicit edges
#
# Node Data:     2 x 1 (active)
# Geometry type: POINT
# Dimension:     XY
# Bounding box:  xmin: 0 ymin: 0 xmax: 0 ymax: 10
        x
  <POINT>
1   (0 0)
2  (0 10)
#
# Edge Data:     1 x 3
# Geometry type: LINESTRING
# Dimension:     XY
# Bounding box:  xmin: 0 ymin: 0 xmax: 0 ymax: 10
   from    to     geometry
  <int> <int> <LINESTRING>
1     1     2  (0 0, 0 10)

当我检查哪个节点时,node_is_root()我发现它是第一个节点。

> with_graph(G, node_is_root())
[1]  TRUE FALSE

有没有可能反过来呢?

期望的输出

> with_graph(G, node_is_root())
[1] FALSE TRUE

注意:模式必须"in"在调用图上的st_network_path()st_network_cost()等其他函数时使用,因为每个节点都代表河流的源头或河口,因此如果在"out"只有一条边的情况下切换到模式,结果将不正确。

4

1 回答 1

0

我认为您需要在创建对象之前反转fromand 。例如:tosfnetwork

library(sf)
#> Linking to GEOS 3.9.0, GDAL 3.2.1, PROJ 7.2.1
library(tidygraph)
library(sfnetworks)

创建数据

n01 = st_sfc(st_point(c(0, 0)))
n02 = st_sfc(st_point(c(0, 10)))

from = 2L
to = 1L

nodes = st_as_sf(c(n01, n02))
edges = data.frame(from = from, to = to)

创建网络对象

G = sfnetwork(nodes, edges) %>%
  convert(to_spatial_explicit, .clean = TRUE)
#> Checking if spatial network structure is valid...
#> Spatial network structure is valid

检查结果

G
#> # A sfnetwork with 2 nodes and 1 edges
#> #
#> # CRS:  NA 
#> #
#> # A rooted tree with spatially explicit edges
#> #
#> # Node Data:     2 x 1 (active)
#> # Geometry type: POINT
#> # Dimension:     XY
#> # Bounding box:  xmin: 0 ymin: 0 xmax: 0 ymax: 10
#>         x
#>   <POINT>
#> 1   (0 0)
#> 2  (0 10)
#> #
#> # Edge Data:     1 x 3
#> # Geometry type: LINESTRING
#> # Dimension:     XY
#> # Bounding box:  xmin: 0 ymin: 0 xmax: 0 ymax: 10
#>    from    to     geometry
#>   <int> <int> <LINESTRING>
#> 1     2     1  (0 10, 0 0)

检查输出

with_graph(G, node_is_root())
#> [1] FALSE  TRUE

reprex 包(v1.0.0)于 2021-04-06 创建

于 2021-04-06T17:57:40.810 回答