我们可以用{sf}
美元.第sf::st_linestring()
章不是你说的st_line()
(1)个 如您的素描所示,如果是直线型:
library(sf)
#> Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE
ls = st_linestring(rbind(st_point(c(0L, 0L)), # point A
st_point(c(10L, 0L)))) # point B
X = st_point(c(3L, 0L))
plot(ls); plot(X, col = "red", add = TRUE)
pnts = st_cast(st_sfc(ls), to = "POINT") # plot endpoints
plot(pnts, col = "blue", add = TRUE)
segments(0L, 0L, 3L, 0L, col = "red")
st_distance(x = pnts[[1L]], y = X, by_element = TRUE) # pnts[[1L]] = A
#> X
#> 3
返回类为[1] "XY" "LINESTRING" "sfg"
的对象的终结点的替代方法:
st_line_sample(ls, sample = 0L)
#> Geometry set for 1 feature
#> Geometry type: MULTIPOINT
#> Dimension: XY
#> Bounding box: xmin: 0 ymin: 0 xmax: 0 ymax: 0
#> CRS: NA
#> MULTIPOINT ((0 0))
st_line_sample(ls, sample = 1L)
#> Geometry set for 1 feature
#> Geometry type: MULTIPOINT
#> Dimension: XY
#> Bounding box: xmin: 10 ymin: 0 xmax: 10 ymax: 0
#> CRS: NA
#> MULTIPOINT ((10 0))
(2)对于"弯曲"LINESTRING,一种 Select 可能是 将LINESTRING分解为其基础点.然后,我们从这些点的子集中创建一个新的子LINESTRING,并计算其长度:
# library(sf)
X1 = st_point(c(3L, 1L))
x = 0L:10L
ls_sin = st_linestring(cbind(x, sin(x)))
# assuming we start with a LINESTRING, we need to get out the points:
mx = matrix(unlist(st_geometry(ls_sin)), ncol = 2L)
(idx = mx[which.min(st_distance(st_as_sf(as.data.frame(mx), coords = c(1L, 2L)), X1)), ])
#> [1] 3.00000 0.14112
ls_sin_sup = st_linestring(cbind(x[1L:idx[1L]], sin(x[1L:idx[1L]])))
plot(ls_sin, col = "black")
plot(ls_sin_sup, col = "red", add = TRUE)
plot(ls, col = "blue", add = TRUE) # from straight line example
sapply(list(ls_sin, ls_sin_sup), st_length)
#> [1] 12.10540 2.30923
(3)是替代 Select ,通过{sfnetworks}
.我们将创建网络、添加新点并计算子线的长度:
# library(sf)
library(sfnetworks)
library(dplyr, warn.conflicts = FALSE)
net = as_sfnetwork(st_as_sf(st_sfc(ls_sin)), directed = FALSE)
X1adj = st_point(mx[idx[1L], ]) # index shifting
netX = st_network_blend(net, X1adj) # add point to sfnetwork
plot(net)
plot(netX |> activate("edges") |> st_as_sf() |> {\(.) .[1L, "x"]}(),
col = "red", add = TRUE)
netX |>
activate("edges") |>
st_as_sf() |>
mutate(Length = st_length(x))
#> Simple feature collection with 2 features and 3 fields
#> Geometry type: LINESTRING
#> Dimension: XY
#> Bounding box: xmin: 0 ymin: -0.9589243 xmax: 10 ymax: 0.9893582
#> CRS: NA
#> # A tibble: 2 × 4
#> from to x Length
#> * <int> <int> <LINESTRING> <dbl>
#> 1 1 3 (0 0, 1 0.841471, 2 0.9092974) 2.31
#> 2 2 3 (2 0.9092974, 3 0.14112, 4 -0.7568025, 5 -0.9589243, 6 -0.… 9.80
得look分.
(4)还没有找到从{sf}
中导出的显式函数,可以准确地将LINESTRING缩短到新的终结点.