计算 shapefile 中每个多边形之间的最大/(或最小)距离
Calculating the maximum/(or minimum) distance between each polygons within a shapefile
我想使用 sf 包(或其他可能有效的包)计算 shapefile 中每个多边形之间的距离。我更喜欢距离是最大或最小距离(因为我不知道如何设置这些参数)。例如,假设 shapefile 中有三个多边形,即多边形 A、B 和 C,我想要得到的结果将是一个数据框,表示 A 到 B、A 到 C、B 到 C 之间的距离。
对于示例shapefile,您可以通过以下代码使用sf内置示例shapefile:
library(sf)
counties <- st_read(system.file("shape/nc.shp", package = "sf"), quiet = T)
任何提示将不胜感激:)在此先感谢您的帮助!
您可能正在寻找 sf::st_distance()
函数。
它支持多边形到多边形的距离,因此它会以矩阵形式为您提供 NC shapefile 中两个县多边形之间的最短距离。在相邻多边形的情况下,这将为零。
为了使矩阵更易于使用,您可能需要设置行名和列名;然后可以将这些用于子集化。
考虑这段代码:
library(sf)
counties <- st_read(system.file("shape/nc.shp", package = "sf"), quiet = T)
# matrix of county to county distances
distances <- st_distance(counties, counties)
# create colnames and rownames to make work easier
colnames(distances) <- counties$NAME
rownames(distances) <- counties$NAME
# distance from county Mecklenburg / 100 values, inc. zeroes
distances["Mecklenburg",]
# counties bordering Mecklenburg cnty / 6 counties - just the zeroes from example above
distances[distances["Mecklenburg", ] == units::as_units(0, "meter"), "Mecklenburg"]
我想使用 sf 包(或其他可能有效的包)计算 shapefile 中每个多边形之间的距离。我更喜欢距离是最大或最小距离(因为我不知道如何设置这些参数)。例如,假设 shapefile 中有三个多边形,即多边形 A、B 和 C,我想要得到的结果将是一个数据框,表示 A 到 B、A 到 C、B 到 C 之间的距离。
对于示例shapefile,您可以通过以下代码使用sf内置示例shapefile:
library(sf)
counties <- st_read(system.file("shape/nc.shp", package = "sf"), quiet = T)
任何提示将不胜感激:)在此先感谢您的帮助!
您可能正在寻找 sf::st_distance()
函数。
它支持多边形到多边形的距离,因此它会以矩阵形式为您提供 NC shapefile 中两个县多边形之间的最短距离。在相邻多边形的情况下,这将为零。
为了使矩阵更易于使用,您可能需要设置行名和列名;然后可以将这些用于子集化。
考虑这段代码:
library(sf)
counties <- st_read(system.file("shape/nc.shp", package = "sf"), quiet = T)
# matrix of county to county distances
distances <- st_distance(counties, counties)
# create colnames and rownames to make work easier
colnames(distances) <- counties$NAME
rownames(distances) <- counties$NAME
# distance from county Mecklenburg / 100 values, inc. zeroes
distances["Mecklenburg",]
# counties bordering Mecklenburg cnty / 6 counties - just the zeroes from example above
distances[distances["Mecklenburg", ] == units::as_units(0, "meter"), "Mecklenburg"]