使用 sf 包计算多边形的直径
Calculating diameter of a polygon with sf package
我在 R 中有一个 sf
对象代表一个县,我想计算它的直径。也就是说,我想知道其边界上任意两点之间的最大距离。我如何在 R 中做到这一点?
我知道原则上可以用旋转卡尺的方法应用于县的凸包。有 R 函数吗?我看到有人建议取最小外接圆的直径,但这只是对取凸包直径的方法的粗略近似。
以下是基于肯塔基州路易斯维尔的示例。下图显示了由 lwgeom
包确定的县及其凸包。
library(dplyr)
library(ggplot2)
library(tigris)
library(sf)
library(lwgeom)
# Set plotting theme ----
theme_set(theme_minimal() +
theme(panel.background = element_rect(fill = 'black'),
plot.background = element_rect(fill = 'black'),
panel.grid = NULL))
# Download data ----
louisville_ky <- tigris::counties(state = "KY", year = 2019) %>%
filter(COUNTYFP == "111")
lou_convex_hull <- st_convex_hull(louisville_ky)
# Plot the data ----
ggplot() +
geom_sf(data = louisville_ky,
color = 'dodgerblue4', fill = 'dodgerblue4') +
geom_sf(data = lou_convex_hull,
color = "white", fill = NA, size = 2)
下面的方法不是很有效,但是很管用。这只是简单地计算凸集中所有点之间的距离,然后确定哪两个点具有最大距离。然后使用 st_length()
函数以适当的单位计算这些点的距离。
# Get the points in the convex set
ch_points <- st_coordinates(lou_convex_hull)[,c("X", "Y")]
# Find the most distant points
dist_matrix <- as.matrix(dist(ch_points))
max_inds <- arrayInd(which.max(dist_matrix), dim(dist_matrix))
most_distant_points <- ch_points[as.vector(max_inds),]
# Calculate their distance
distance <- st_linestring(most_distant_points, dim = "XY") %>%
st_sfc(crs = st_crs(lou_convex_hull)) %>%
st_length()
print(distance)
#> 55462.51 [m]
您可以将凸包转换为 MULTIPOINT,然后是 POINT,使用 st_distance
,并仅保留最大距离:
lou_convex_hull %>%
st_cast('MULTIPOINT') %>%
st_cast('POINT') %>%
st_distance() %>%
max()
55462.84 [m]
好像不用凸包也一样:
louisville_ky %>%
st_cast('MULTIPOINT') %>%
st_cast('POINT') %>%
st_distance %>%
max()
55462.84 [m]
我在 R 中有一个 sf
对象代表一个县,我想计算它的直径。也就是说,我想知道其边界上任意两点之间的最大距离。我如何在 R 中做到这一点?
我知道原则上可以用旋转卡尺的方法应用于县的凸包。有 R 函数吗?我看到有人建议取最小外接圆的直径,但这只是对取凸包直径的方法的粗略近似。
以下是基于肯塔基州路易斯维尔的示例。下图显示了由 lwgeom
包确定的县及其凸包。
library(dplyr)
library(ggplot2)
library(tigris)
library(sf)
library(lwgeom)
# Set plotting theme ----
theme_set(theme_minimal() +
theme(panel.background = element_rect(fill = 'black'),
plot.background = element_rect(fill = 'black'),
panel.grid = NULL))
# Download data ----
louisville_ky <- tigris::counties(state = "KY", year = 2019) %>%
filter(COUNTYFP == "111")
lou_convex_hull <- st_convex_hull(louisville_ky)
# Plot the data ----
ggplot() +
geom_sf(data = louisville_ky,
color = 'dodgerblue4', fill = 'dodgerblue4') +
geom_sf(data = lou_convex_hull,
color = "white", fill = NA, size = 2)
下面的方法不是很有效,但是很管用。这只是简单地计算凸集中所有点之间的距离,然后确定哪两个点具有最大距离。然后使用 st_length()
函数以适当的单位计算这些点的距离。
# Get the points in the convex set
ch_points <- st_coordinates(lou_convex_hull)[,c("X", "Y")]
# Find the most distant points
dist_matrix <- as.matrix(dist(ch_points))
max_inds <- arrayInd(which.max(dist_matrix), dim(dist_matrix))
most_distant_points <- ch_points[as.vector(max_inds),]
# Calculate their distance
distance <- st_linestring(most_distant_points, dim = "XY") %>%
st_sfc(crs = st_crs(lou_convex_hull)) %>%
st_length()
print(distance)
#> 55462.51 [m]
您可以将凸包转换为 MULTIPOINT,然后是 POINT,使用 st_distance
,并仅保留最大距离:
lou_convex_hull %>%
st_cast('MULTIPOINT') %>%
st_cast('POINT') %>%
st_distance() %>%
max()
55462.84 [m]
好像不用凸包也一样:
louisville_ky %>%
st_cast('MULTIPOINT') %>%
st_cast('POINT') %>%
st_distance %>%
max()
55462.84 [m]