Skip to content

Instantly share code, notes, and snippets.

@JosiahParry
Created July 31, 2021 15:56
Show Gist options
  • Save JosiahParry/dcfe18bd22571c7ac7f5b77e5c549797 to your computer and use it in GitHub Desktop.
Save JosiahParry/dcfe18bd22571c7ac7f5b77e5c549797 to your computer and use it in GitHub Desktop.
Find polygon distances for combined nb list
library(sf)
library(sfweight)
library(tidyverse)
acs %>%
mutate(nb = st_neighbors(geometry),
# centroids are used for distance calculation
centroid = st_centroid(geometry),
# finding nearest polygon based on centroid
nb2 = st_knn(centroid),
# create some random missing nb objects
nb = ifelse(
sample(c(TRUE, FALSE), size = 203,
replace = TRUE, prob = c(0.8, .2)),
nb, NA),
# create new nb list coalescing existing from from adjacency and knn
final_nb = coalesce(nb, nb2),
dists = st_nb_dist(centroid, final_nb),
# find the minimum distance
min_dist_nb = map_dbl(dists, min))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment