简体   繁体   中英

include simple features attributes in a sfnetwork graph

I'm working with a .shp file with LINESTRING geometry and for each line there are several attributes.

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...

I use the next code to find lines intersect and generate new nodes. but doing this with sfnetwork I get a graph without attributes.

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

How can I add a dataframe with attributes to this type of graph (like a SpatialLineDataframe)?

sf objects are just data frames with a geometry column. So you can use functions like mutate and left_join to add more columns as attributes to each line (edge) or intersection node:

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)

Created on 2021-11-12 by the reprex package (v2.0.1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM