r-在sfnetwork图中包含简单的特征属性



我正在使用一个带有LINESTRING几何体的.shp文件,每行都有几个属性。

Simple feature collection with 5979 features and 39 fields
Geometry type: LINESTRING
Dimension:     XY
Bounding box:  xmin: 334297 ymin: 6277095 xmax: 360375.2 ymax: 6312683
Projected CRS: WGS 84 / UTM zone 19S
Id A1 A2 ... geometry
1  1  1  1 ... LINESTRING (348339.3 628293...
2  2  2  2 ... LINESTRING (343785.3 629153...
3  3  3  3 ... LINESTRING (343926.6 629186...
4  4  4  4 ... LINESTRING (343988.3 629201...
5  5  5  5 ... LINESTRING (344032.6 629212...

我使用下一个代码来查找相交的线并生成新的节点。但是通过sfnetwork,我得到了一个没有属性的图。

shp.file = st_read("myfile.shp")
graph = st_sf(shp.file) %>%
# Combine LINESTRINGS into a MULTILINESTRING geometry
st_combine() %>%
# Create a node where the MULTILINESTRINGs cross
st_node() %>%
# Cast back to LINESTRINGs
st_cast('LINESTRING') %>%
# Create sfnetwork
as_sfnetwork(directed = F) 
A sfnetwork with 6308 nodes and 6085 edges
#
# CRS:  WGS 84 / UTM zone 19S 
#
# An undirected multigraph with 282 components with spatially explicit edges
#
# Node Data:     6,308 x 1 (active)
# Geometry type: POINT
# Dimension:     XY
# Bounding box:  xmin: 334297 ymin: 6277096 xmax: 360375.2 ymax: 6312683
x
<POINT [m]>
1 (348339.3 6282939)
2 (348346.9 6282938)
3 (343785.3 6291533)
4 (343791.5 6291546)
5 (343926.6 6291865)
6 (343931.5 6291875)
# ... with 6,302 more rows
#
# Edge Data:     6,085 x 3
# Geometry type: LINESTRING
# Dimension:     XY
# Bounding box:  xmin: 334297 ymin: 6277095 xmax: 360375.2 ymax: 6312683
from    to                                    x
<int> <int>                     <LINESTRING [m]>
1     1     2 (348339.3 6282939, 348346.9 6282938)
2     3     4 (343785.3 6291533, 343791.5 6291546)
3     5     6 (343926.6 6291865, 343931.5 6291875)
# ... with 6,082 more rows

如何将具有属性的数据帧添加到此类型的图(如SpatialLineDataframe(?

sf对象只是具有几何体列的数据帧。因此,您可以使用mutateleft_join等函数将更多列作为属性添加到每条线(边(或交叉节点:

library(tidyverse)
library(sf)
#> Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
library(sfnetworks)
p1 = st_point(c(7, 51))
p2 = st_point(c(7, 52))
p3 = st_point(c(8, 52))
p4 = st_point(c(8, 51.5))
l1 = st_sfc(st_linestring(c(p1, p2)))
l2 = st_sfc(st_linestring(c(p1, p4, p3)))
l3 = st_sfc(st_linestring(c(p3, p2)))
edges = st_as_sf(c(l1, l2, l3), crs = 4326)
nodes = st_as_sf(c(st_sfc(p1), st_sfc(p2), st_sfc(p3)), crs = 4326)
edges_annotations <-
tibble(
x = c(l1, l2, l3) %>% st_set_crs(4326),
from = c(1,1,3),
to = c(2,3,2),
name = c("foo", "bar", "baz")
)
edges_annotations
#> # A tibble: 3 x 4
#>                      x  from    to name 
#>       <LINESTRING [°]> <dbl> <dbl> <chr>
#> 1         (7 51, 7 52)     1     2 foo  
#> 2 (7 51, 8 51.5, 8 52)     1     3 bar  
#> 3         (8 52, 7 52)     3     2 baz
edges <- edges %>% left_join(edges_annotations)
#> Joining, by = "x"
nodes <- nodes %>% mutate(name = c("n1", "n2", "n3"))
net = sfnetwork(nodes, edges)
#> Checking if spatial network structure is valid...
#> Spatial network structure is valid
net
#> # A sfnetwork with 3 nodes and 3 edges
#> #
#> # CRS:  EPSG:4326 
#> #
#> # A directed acyclic simple graph with 1 component with spatially explicit edges
#> #
#> # Node Data:     3 x 2 (active)
#> # Geometry type: POINT
#> # Dimension:     XY
#> # Bounding box:  xmin: 7 ymin: 51 xmax: 8 ymax: 52
#>             x name 
#>   <POINT [°]> <chr>
#> 1      (7 51) n1   
#> 2      (7 52) n2   
#> 3      (8 52) n3   
#> #
#> # Edge Data:     3 x 4
#> # Geometry type: LINESTRING
#> # Dimension:     XY
#> # Bounding box:  xmin: 7 ymin: 51 xmax: 8 ymax: 52
#>    from    to name                     x
#>   <int> <int> <chr>     <LINESTRING [°]>
#> 1     1     2 foo           (7 51, 7 52)
#> 2     1     3 bar   (7 51, 8 51.5, 8 52)
#> 3     3     2 baz           (8 52, 7 52)

创建于2021-11-12由reprex包(v2.0.1(

最新更新