R语言sfnetworks包,st_network_blend()#将地理空间点混合到空间网络中
# Mon Jul 26 17:10:37 2021 -
# 字符编码:UTF-8
# R 版本:R x64 4.1 for window 11
# cgh163email@163.com
# 个人笔记不负责任,拎了个梨🍐🍈
#.rs.restartR()
require(sfnetworks)
rm(list = ls());gc()
st_network_blend()#将地理空间点混合到空间网络中
library(sf, quietly = TRUE)
#创建一个网络和一组要混合的点。
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')
# Blend points into the network.
# --> By default tolerance is set to Inf
# --> Meaning that all points get blended
b1 = st_network_blend(net, pts)
b1
#> # A sfnetwork with 12 nodes and 11 edges
#> #
#> # CRS: EPSG:3857
#> #
#> # A rooted tree with spatially explicit edges
#> #
#> # Node Data: 12 x 1 (active)
#> # Geometry type: POINT
#> # Dimension: XY
#> # Bounding box: xmin: -1 ymin: 0 xmax: 1 ymax: 2
#> x
#> <POINT [m]>
#> 1 (0 0)
#> 2 (1 1)
#> 3 (0 2)
#> 4 (-1 1)
#> 5 (0.2129865 0.2129865)
#> 6 (0.2825198 0.2825198)
#> # … with 6 more rows
#> #
#> # Edge Data: 11 x 3
#> # Geometry type: LINESTRING
#> # Dimension: XY
#> # Bounding box: xmin: -1 ymin: 0 xmax: 1 ymax: 2
#> from to x
#> <int> <int> <LINESTRING [m]>
#> 1 1 5 (0 0, 0.2129865 0.2129865)
#> 2 5 6 (0.2129865 0.2129865, 0.2825198 0.2825198)
#> 3 6 7 (0.2825198 0.2825198, 0.3112431 0.3112431)
#> # … with 8 more rows
# Blend points with a tolerance.
tol = units::set_units(0.2, "m")
b2 = st_network_blend(net, pts, tolerance = tol)
b2
#> # A sfnetwork with 6 nodes and 5 edges
#> #
#> # CRS: EPSG:3857
#> #
#> # A rooted tree with spatially explicit edges
#> #
#> # Node Data: 6 x 1 (active)
#> # Geometry type: POINT
#> # Dimension: XY
#> # Bounding box: xmin: -1 ymin: 0 xmax: 1 ymax: 2
#> x
#> <POINT [m]>
#> 1 (0 0)
#> 2 (1 1)
#> 3 (0 2)
#> 4 (-1 1)
#> 5 (0.3112431 0.3112431)
#> 6 (0.7104781 1.289522)
#> #
#> # Edge Data: 5 x 3
#> # Geometry type: LINESTRING
#> # Dimension: XY
#> # Bounding box: xmin: -1 ymin: 0 xmax: 1 ymax: 2
#> from to x
#> <int> <int> <LINESTRING [m]>
#> 1 1 5 (0 0, 0.3112431 0.3112431)
#> 2 5 2 (0.3112431 0.3112431, 1 1)
#> 3 2 6 (1 1, 0.7104781 1.289522)
#> # … with 2 more rows
## Plot results.
# Initial network and points.
oldpar = par(no.readonly = TRUE)
par(mar = c(1,1,1,1), mfrow = c(1,3))
plot(net, cex = 2, main = "Network + set of points")
plot(pts, cex = 2, col = "red", pch = 20, add = TRUE)
#不带任何公差的混合
plot(b1, cex = 2, main = "Blend with tolerance = Inf")
plot(pts, cex = 2, col = "red", pch = 20, add = TRUE)
# Blend with tolerance.
within = st_is_within_distance(pts, st_geometry(net, "edges"), tol)
pts_within = pts[lengths(within) > 0]
plot(b2, cex = 2, main = "Blend with tolerance = 0.2 m")
plot(pts, cex = 2, col = "grey", pch = 20, add = TRUE)
plot(pts_within, cex = 2, col = "red", pch = 20, add = TRUE)
par(oldpar)
dev.copy(png, "1.png");dev.off()