首页 > 解决方案 > 用R计算多个多边形之间的最小距离

问题描述

我对 R 和 sf 包还是有点陌生​​...

我有两组要分析的多面数据。我的第一组多边形(火灾)包含数百个野火周界。第二组(城镇)包含数百个城市区域边界。

对于每次火灾,我想计算到最近城镇的距离(火灾多边形边缘到最近城镇多边形边缘),并将其作为一个字段添加到每个火灾中。

到目前为止,我主要使用 sf 包来存储空间数据。在我的搜索中,我只能找到多边形到点、点到点、线到点等的最小距离方法,但似乎找不到多边形到多边形的例子。任何帮助我朝着正确的方向前进将不胜感激!谢谢你。

标签: rpolygondistancespatialsf

解决方案


@TimSalabim 感谢您向我发送正确的方向。我能够完成我所追求的。也许不是最优雅的解决方案,但它确实有效。

# create an index of the nearest feature
index <- st_nearest_feature(x = poly1, y = poly2)

# slice based on the index
poly2 <- poly2 %>% slice(index)

# calculate distance between polygons
poly_dist <- st_distance(x = poly1, y= poly2, by_element = TRUE)

# add the distance calculations to the fire polygons
poly1$distance <- poly_dist

推荐阅读