在 sfnetwork 图中包含简单的特征属性
Posted
技术标签:
【中文标题】在 sfnetwork 图中包含简单的特征属性【英文标题】:include simple features attributes in a sfnetwork graph 【发布时间】:2021-12-24 18:45:56 【问题描述】:我正在处理一个带有 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)?
【问题讨论】:
嗨!我认为您在运行st_combine()
时会“丢失”这些属性。你能检查st_combine(shp.file)
的输出吗?而且,你为什么需要st_combine
?
【参考方案1】:
sf
对象只是带有几何列的数据框。
因此,您可以使用mutate
和left_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)
由reprex package (v2.0.1) 于 2021 年 11 月 12 日创建
【讨论】:
以上是关于在 sfnetwork 图中包含简单的特征属性的主要内容,如果未能解决你的问题,请参考以下文章