Сохранить столбцы точек sf после st_network_blend()
У меня sfnetwork (
G
) и то, что я комбинирую, используя. После вычисления некоторых новых атрибутов для этих точек я хотел бы извлечь их и восстановить столбцы оригинала. Пространственное соединение с использованием
st_join
не будет работать, потому что точки привязаны во время
st_network_blend()
(поэтому не долго на круге) и
cbind()
не сработает, потому что не все находятся в пределах допуска привязки.
Если есть способ сохранить ID от
points
через процесс смешивания, чтобы его можно было использовать для присоединения впоследствии к
out_points
?
library(sfnetworks)
library(sf)
#> Linking to GEOS 3.9.0, GDAL 3.2.0, PROJ 7.2.0
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(tidygraph)
#>
#> Attaching package: 'tidygraph'
#> The following object is masked from 'package:stats':
#>
#> filter
# Create an example network.
n01 = st_sfc(st_point(c(0, 0)))
n02 = st_sfc(st_point(c(1, 2)))
n03 = st_sfc(st_point(c(1, 3)))
n04 = st_sfc(st_point(c(1, 4)))
n05 = st_sfc(st_point(c(2, 1)))
n06 = st_sfc(st_point(c(2, 3)))
n07 = st_sfc(st_point(c(2, 4)))
n08 = st_sfc(st_point(c(3, 2)))
n09 = st_sfc(st_point(c(3, 3)))
n10 = st_sfc(st_point(c(3, 4)))
n11 = st_sfc(st_point(c(4, 2)))
n12 = st_sfc(st_point(c(4, 4)))
from = c(1, 2, 2, 3, 3, 5, 5, 8, 8, 9, 9)
to = c(5, 3, 6, 4, 7, 2, 8, 9, 11, 10, 12)
nodes = st_as_sf(c(n01, n02, n03, n04, n05, n06, n07, n08, n09, n10, n11, n12))
edges = data.frame(from = from, to = to)
G_1 = sfnetwork(nodes, edges)
#> Checking if spatial network structure is valid...
#> Spatial network structure is valid
n01 = st_sfc(st_point(c(0, 0)))
n02 = st_sfc(st_point(c(-1, 2)))
n03 = st_sfc(st_point(c(-1, 3)))
n04 = st_sfc(st_point(c(-1, 4)))
n05 = st_sfc(st_point(c(-2, 1)))
n06 = st_sfc(st_point(c(-2, 3)))
n07 = st_sfc(st_point(c(-2, 4)))
n08 = st_sfc(st_point(c(-3, 2)))
n09 = st_sfc(st_point(c(-3, 3)))
n10 = st_sfc(st_point(c(-3, 4)))
n11 = st_sfc(st_point(c(-4, 2)))
n12 = st_sfc(st_point(c(-4, 4)))
from = c(1, 2, 2, 3, 3, 5, 5, 8, 8, 9, 9)
to = c(5, 3, 6, 4, 7, 2, 8, 9, 11, 10, 12)
nodes = st_as_sf(c(n01, n02, n03, n04, n05, n06, n07, n08, n09, n10, n11, n12))
edges = data.frame(from = from, to = to)
G_2 = sfnetwork(nodes, edges)
#> Checking if spatial network structure is valid...
#> Spatial network structure is valid
G = st_network_join(G_1, G_2) %>%
convert(to_spatial_explicit, .clean = TRUE)
# Create a set of points.
p1 = st_sfc(st_point(c(1, 0.5)))
p2 = st_sfc(st_point(c(2.5, 1.5)))
p3 = st_sfc(st_point(c(1, 2.5)))
p4 = st_sfc(st_point(c(1, 3.5)))
p5 = st_sfc(st_point(c(1.5, 3.5)))
p6 = st_sfc(st_point(c(-1.5, 1.5)))
p7 = st_sfc(st_point(c(-1.5, 2.5)))
p8 = st_sfc(st_point(c(-1.5, 3.5)))
p9 = st_sfc(st_point(c(-1, 0.5)))
# Important!
# Add a column to the points with TRUE values.
# Such that we know which nodes correspond to the points after we include them in the network.
points = st_as_sf(c(p1, p2, p3, p4, p5, p6, p7, p8, p9))
points$is_point = TRUE
points$attribute = paste("A", seq_along(a:nrow(points))
points_df = points$geometry <- NULL
G_blend <- st_network_blend(G, points["is_point"])
nodes <- st_as_sf(G_blend, "nodes") %>%
tibble::rowid_to_column("node_id")
# regain original points columns using st_join, left_join or cbind....
out_points <- nodes %>%
filter(is_point) %>%
select(-is_point) %>%
filter(!row_number() %in% c(1, 2, 4)) %>% # simulate 3 points not being snapped during st_network_blend()
cbind(points_df) # doesn't work if some points aren't snapped during st_network_blend()
1 ответ
Атрибуты сохраняются...
я изменил
net_blend = st_network_blend(net, pts[is_point], tolerance = tol)
out_pts <- st_as_sf(net_blend, "nodes") %>%
tibble::rowid_to_column("node_id") %>%
filter(is_point) %>%
select(-is_point)
К
net_blend = st_network_blend(net, pts, tolerance = tol)#
out_pts <- st_as_sf(net_blend, "nodes") %>%
filter(is_point) %>%
select(-is_point)
Давать
> out_pts
Simple feature collection with 2 features and 2 fields
Geometry type: POINT
Dimension: XY
Bounding box: xmin: 0.6040494 ymin: 1.27107 xmax: 0.7289301 ymax: 1.395951
Projected CRS: WGS 84 / Pseudo-Mercator
# A tibble: 2 x 3
node_id dummy_var x
<int> <chr> <POINT [m]>
1 6 A6 (0.7289301 1.27107)
2 3 A3 (0.6040494 1.395951)