计算两条线之间的最大距离

时间:2020-01-28 15:01:15

标签: 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)

1 个答案:

答案 0 :(得分:1)

我会给您一个见识,也许这不是您想像的完整答案。

由于线是由点组成的,因此,只要稍作两个更改,就可以通过执行与线相同的操作,不仅可以拥有最小距离,而且还可以具有最大距离,但也可以拥有线所在的点组成。

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),然后处理并找到较短的线的点与最长的线本身之间的最长距离,也许可以拥有想要的东西: / p>

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

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]
相关问题