r - 如何使用 SF 包计算质心和多边形边缘之间的最大距离?

标签 r gis sp sf

我有一堆具有质心的各种形状和大小的多边形。我想计算从每个质心到其各自多边形最远点的距离。

这个问题已经resolved here使用 package::sp 和 package::rgeos。

根据 its vignette , sf 包“旨在长期接替 sp”。翻看the documentation我找不到解决方案,但我不是简单功能方面的专家。有没有使用 sf 包完成此操作的好方法,还是我现在应该坚持使用 sf 和 rgeos?

最佳答案

将多边形转换为 POINT(从而获得顶点),然后计算质心的距离应该可行。像这样的东西:

library(sf)

# build a test poly
geometry <- st_sfc(st_polygon(list(rbind(c(0,0), c(1,0), c(1,3),  c(0,0))))) 
pol <- st_sf(r = 5, geometry)

# compute distances 
distances <- pol %>% 
  st_cast("POINT") %>% 
  st_distance(st_centroid(pol))

distances
#>          [,1]
#> [1,] 1.201850
#> [2,] 1.054093
#> [3,] 2.027588
#> [4,] 1.201850

# maximum dist:
max_dist <- max(distances)
max_dist
#> [1] 2.027588

# plot to see if is this correct: seems so.
plot(st_geometry(pol))
plot(st_centroid(pol), add = T)
plot(st_cast(pol, "POINT")[which.max(distances),],
     cex =3, add = T, col = "red")

您两次获得相同的距离,因为第一个和最后一个顶点相同,但由于您对最大值感兴趣,所以这无关紧要。

HTH

关于r - 如何使用 SF 包计算质心和多边形边缘之间的最大距离?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48447680/

相关文章:

R:使用 lapply 添加新列

r - Shiny 的 "ERROR: Tabs should all be unnamed arguments"

r - 如何将id变量添加到R中的多个数据集

r - R-在城市 map 上拟合网格并将数据输入到网格正方形中

r - 对于一个数据集中的每个点,计算到第二个数据集中最近点的距离

r - R-将SpatialLines转换为栅格

r - 在 R 中,比较两个字符串列表,在第二个列表中找到第一个列表的每个元素的部分

java - 使用 Geotools 创建 Google map 叠加层

r - 将 DMS 坐标转换为 R 中的十进制度数

python - 关于创建 WMS 服务和桌面客户端的一些建议?