2

我有一个 sfnetwork ( net) 并且pts我正在使用st_network_blend(). 在为这些点计算了一些新属性之后,我想提取它们并重新获得原始的列points。使用空间连接st_join不起作用,因为这些点在期间被捕捉st_network_blend()(因此不再重叠)并且cbind()不起作用,因为并非所有points点都在捕捉公差范围内。

有没有办法通过混合过程保留一个 ID,pts以便之后可以用于加入out_pts?或者通过申请left_join()加入时会给出正确答案?rowid_to_column()ptsout_pts

library(sfnetworks)
library(sf)
library(dplyr)

# Create a network and a set of points to blend.
n11 = st_point(c(0,0))
n12 = st_point(c(1,1))
e1 = st_sfc(st_linestring(c(n11, n12)), crs = 3857)

n21 = n12
n22 = st_point(c(0,2))
e2 = st_sfc(st_linestring(c(n21, n22)), crs = 3857)

n31 = n22
n32 = st_point(c(-1,1))
e3 = st_sfc(st_linestring(c(n31, n32)), crs = 3857)

net = as_sfnetwork(c(e1,e2,e3))

pts = net %>%
  st_bbox() %>%
  st_as_sfc() %>%
  st_sample(10, type = "random") %>%
  st_set_crs(3857) %>%
  st_cast('POINT') %>%
  st_as_sf() %>%
  mutate(is_point = TRUE,
         dummy_var = paste0("A", seq_along(1:nrow(.)))) %>%
  tibble::rowid_to_column("node_id")

plot(net)
plot(st_geometry(pts), col = "red", pch = 20, add = TRUE)

# Blend points into the network.
# --> Not all points get blended 
tol = units::set_units(0.2, "m")
net_blend = st_network_blend(net, pts["is_point"], tolerance = tol)

# now only interested in points that fell within tol
# but dummy_var column was not preserved from pts
# added node_id column, will this differ from pts??
out_pts <- st_as_sf(net_blend, "nodes") %>%
  tibble::rowid_to_column("node_id") %>%
  filter(is_point) %>%
  select(-is_point)

# there's now a mismatch because pts and out_pts are different lengths
# as not all pts made it through st_network_blend()
nrow(pts)
nrow(out_pts)

# try st_join anyway
# dummy_var should != NA
st_join(out_pts, pts) # nope, doesn't work    

# pts without geometry
pts_cols <-  pts %>%
  as_data_frame()%>%
  select(-is_point)
pts_cols$x <- NULL

# what about cbind()
cbind(out_pts, pts_cols) # nope

# how do I tell if dummy_var has been joined to the correct point?
left_join(out_pts, pts_cols, by = "node_id")

我也试过

tol = units::set_units(0.2, "m")
net_blend = st_network_blend(net, pts, tolerance = tol)

sf_attr(net_blend, "agr", active = "nodes")

但这给了

  node_id  is_point dummy_var 
     <NA>      <NA>      <NA> 
Levels: constant aggregate identity
4

1 回答 1

0

属性被保留...

我变了

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)
于 2021-11-30T10:05:07.427 回答