用R计算多个多边形之间的最小距离 [英] Calculate minimum distance between multiple polygons with R

查看:69
本文介绍了用R计算多个多边形之间的最小距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm still somewhat new to R and the sf package...

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

I have two sets of multipolygon data that I am trying to analyze. My first set of polygons (fires) contains hundreds of wildfire perimeters. The second set (towns) contains hundreds of urban areas boundaries.

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

For each fire, I would like to calculate the distance to the closest town (fire polygon edge to closest town polygon edge), and add that as a field to each fire.

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

So far I have mostly been using the sf package for spatial data. In my searches, I can only find minimum distance methods for polygons to points, points to points, lines to points, etc. but cannot seem to find polygon to polygon examples. Any help to send me in the right direction would be much appreciated! Thank you.

推荐答案

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

@TimSalabim Thank you for sending me in the right direction. I was able to accomplish what I was after. Maybe not the most elegant solution, but it worked.

# 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

这篇关于用R计算多个多边形之间的最小距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆