I would like to group polygons together based on a distance criteria:
- Any polygon within a certain distance (1200 metres or less) of an origin polygon are grouped together
- If other polygons are within the same distance (1200 metres or less) of these 'neighbouring' polygons they are added to this same group
- The process for this group continues until no further polygons are added (because they are all further than 1200 metres away).
- The next ungrouped polygon is selected and the process repeats for a new grouping
- Polygons with no neighbour within 1200 metres are assigned to be in a group by themselves
- A polygon should only belong to one group
The final output would be a table with the single polygon ID (UID) and the group ID it belongs to (GrpID) and the average distance between the polygons in that group
I am sure a distance matrix with st_distance
means this is possible, but I'm just not getting it.
library(sf)
library(dplyr)
download.file("https://drive.google.com/uc?export=download&id=1-I4F2NYvFWkNqy7ASFNxnyrwr_wT0lGF" , destfile="ProximityAreas.zip")
unzip("ProximityAreas.zip")
Proximity_Areas <- st_read("Proximity_Areas.gpkg")
Dist_Matrix <- Proximity_Areas %>%
st_distance(. , by_element = FALSE)