-
-
Save JosiahParry/dcfe18bd22571c7ac7f5b77e5c549797 to your computer and use it in GitHub Desktop.
Find polygon distances for combined nb list
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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