Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate minimum distance between multiple polygons with R

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.

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.

like image 338
shindig Avatar asked Dec 19 '18 15:12

shindig


People also ask

How do you find the minimum distance between two curves?

The points on the curve which corresponds to the shortest distance between the curves will have the slope of their tangent equal to that of the line y =x. That is, if tangents are drawn at points corresponding to the shortest distance then these tangents will have a slope equal to 1.

What is minimal distance?

The term minimum distance may refer to. Minimum distance estimation, a statistical method for fitting a model to data. Closest pair of points problem, the algorithmic problem of finding two points that have the minimum distance among a larger set of points.


1 Answers

@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
like image 132
shindig Avatar answered Sep 22 '22 05:09

shindig