使用R

时间:2018-12-19 15:51:50

标签: r polygon distance spatial sf

我还是R和sf包的新手...

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

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

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

1 个答案:

答案 0 :(得分:1)

@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