r - 计算两条线之间的最宽距离

标签 r sf

我想计算平均距离以及两条线之间的最宽距离。我知道如何使用 st_distance() 函数找到最小距离,但我不确定如何找到其他两个指标。红线是我认为我需要测量以找到两条线之间的平均距离和最宽距离的线。 enter image description here

附件是一些示例数据。

pts1<- data.frame(
  x= c(-103.485342, -103.482808),
  y = c(31.348758, 31.376947))
) %>% 
  sf::st_as_sf(coords = c("x","y"))

st_crs(pts1)<- "+init=epsg:2257"

pts2<- data.frame(
  x= c(-103.492822, -103.484231),
  y = c(31.348181, 31.377191))
) %>% 
  sf::st_as_sf(coords = c("x","y"))

st_crs(pts2)<- "+init=epsg:2257"

a <- pts1 %>% st_coordinates() %>% st_linestring()
b<- pts2 %>% st_coordinates() %>% st_linestring()

min_dist<-st_distance(a,b,by_element = T)

请参阅下面的第二个示例。从图像中,我计算出距垂直线约 300 米,该垂直线最大程度地穿过两条线。

enter image description here

pts1 <- data.frame(x = c(-103.485342, -103.482808),
                   y = c(31.348758, 31.376947)) %>% 
  sf::st_as_sf(coords = c("x","y"))

st_crs(pts1) <- "+proj=longlat +datum=WGS84"

pts1<- st_transform(pts1,"+init=epsg:2257")


pts2 <- data.frame(x = c(-103.492812, -103.484231),
                   y = c(31.318181, 31.377991)) %>% 
  sf::st_as_sf(coords = c("x","y"))


st_crs(pts2) <- "+proj=longlat +datum=WGS84"

pts2<- st_transform(pts2,"+init=epsg:2257")

a <- pts1 %>% st_coordinates() %>% st_linestring()
b <- pts2 %>% st_coordinates() %>% st_linestring()

st_distance(pts1, pts2, by_element = T)

最佳答案

我会给你一个见解,也许这不是你想象的完整答案。

由于线是由点组成的,如果你只做两个轻微的改变,你不仅可以有最小距离,还可以通过做与线相同的事情来获得最大距离,但是线是从点开始的包括在内。

library(sf)
#> Linking to GEOS 3.8.0, GDAL 2.4.2, PROJ 6.2.1

pts1 <- data.frame(x = c(-103.485342, -103.482808),
                  y = c(31.348758, 31.376947)) %>% 
  sf::st_as_sf(coords = c("x","y"))

st_crs(pts1) <- "+init=epsg:2257"

pts2 <- data.frame(x = c(-103.492822, -103.484231),
                   y = c(31.348181, 31.377191)) %>% 
  sf::st_as_sf(coords = c("x","y"))

st_crs(pts2)<- "+init=epsg:2257"

a <- pts1 %>% st_coordinates() %>% st_linestring()
b <- pts2 %>% st_coordinates() %>% st_linestring()

st_distance(pts1, pts2, by_element = T)
#> Units: [US_survey_foot]
#> [1] 0.007502222 0.001443768

对于平均距离,我不确定这是否是你想要的,但我认为你可以获取两条线的质心,然后处理 st_distance

ca <- st_centroid(a)
cb <- st_centroid(b)

st_distance(ca, cb, by_element = T)
#> [1] 0.004454613

编辑:我最后一次尝试基于评论

我认为,如果找到最长的线(在您的示例中为 b),而不是处理并找到较短线的点与最长线本身之间的最长距离,您可能会拥有想要的东西:

(我还对您的原始代码进行了一些更改以使其工作)

library(sf)

pts1 <- data.frame(x = c(-103.485342, -103.482808),
                   y = c(31.348758, 31.376947)) %>% 
  st_as_sf(coords = c("x","y")) %>% 
  st_set_crs(4326) %>% 
  st_transform(2257)

pts2 <- data.frame(x = c(-103.492812, -103.484231),
                   y = c(31.318181, 31.377991)) %>% 
  st_as_sf(coords = c("x","y")) %>% 
  st_set_crs(4326) %>% 
  st_transform(2257)

a <- pts1 %>% 
  st_union(.) %>% 
  st_cast(to = "LINESTRING")

b <- pts2 %>% 
  st_union(.) %>% 
  st_cast(to = "LINESTRING")

longest <- ifelse(test = st_length(a) > st_length(b),
                  yes = quote(a),
                  no = quote(b))

max(st_distance(pts1, eval(longest)))
#> 955.7374 [US_survey_foot]

关于r - 计算两条线之间的最宽距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59951552/

相关文章:

对 sf 对象的行操作

r - 为什么RUnit更改我的随机数?

r - 使用 openxlsx 包的工作表链接问题

r - 使用R进行网页抓取时如何处理验证码

r - parse_aws_s3_response 错误,禁止 (http 403)

r - 使用 dplyr distinct 忽略 R 中 sf 对象的几何形状

ggplot2 - 结合 sf 的多边形在 ggplot 中绘图时,栅格的 NA 值具有填充

在R中使用Inf和NaN删除行

r - 按字符拆分 sf 对象而不删除它在 R 中的几何形状

r - 用 st_buffer 围绕一个地理点