使用R和sf包,我研究了lat/lon路径,但找不到一个看起来很明显的函数.

Given the situation of a trip A->B, which may have any continuous geometry, and is described by an st_line, a point X (or a circle, by adding a st_buffer), lies somewhere along the path. How can I compute the distance from A to X ? Trip from A to B

我只能对一大块路径使用st_long函数.

推荐答案

我们可以用{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缩短到新的终结点.

R相关问答推荐

在通过最大似然估计将ODE模型与数据匹配时,为什么要匹配实际参数的转换值?

列出用m n个值替换来绘制n个数字的所有方法(i.o.w.:R中大小为n的集合的所有划分为m个不同子集)

修改用R编写的用户定义函数

自动变更列表

如何删除仅在数据集顶部和底部包含零的行

try 将 colored颜色 编码添加到ggploly的标题中

如何在PackageStatus()中列出&q;不可用的包&q;?

用R ggplot2求上、下三角形中两个变量的矩阵热图

如何在ggplot2中绘制具有特定 colored颜色 的连续色轮

在保留列表元素属性的同时替换列表元素

如何计算每12行的平均数?

如果条件匹配,则使用Mariate粘贴列名

对R中的列表列执行ROW Mean操作

计算多变量的加权和

如何使用grepl()在数据帧列表中 Select 特定字符串?

如何使用循环从R中的聚合函数创建列,而不会在名称中给出&q;$&q;?

禁用时,SelecizeInput将变得不透明

将美学添加到ggploy中的文本标签

使用nls()函数的非线性模型的半正态图

如何使用ggsurvfit包更改风险表中的标签名称?