New to R and to coding in general.
I'm attempting to explore the spatial properties of cities using network analysis. I am unable to share the data at this time.
I have a shapefile with various points. Using the sf and sf_network packages, how would I use the output of a st_nearest_points(x,y) against the same dataset as the connections in a network analysis?
Any advice/help/wisdom would be appreciated. This is about as far as I've gotten.
library(sf)
library(sfnetworks)
p1 = st_point(c(7, 51))
p2 = st_point(c(7, 52))
p3 = st_point(c(8, 52))
p4 = st_point(c(9, 40))
nodes = st_as_sf(st_sfc(p1, p2, p3, p4, crs = 4326))
nodes_2 = nodes
edges = st_nearest_points(nodes, nodes_2)
edges$to= c(st_cast(edges,"POINT"))
edges$from= c(st_cast(edges,"POINT"))
sfnetwork(nodes, edges)
Using
nngeo::st_nn()
works well to find the nearest neighbors to build ansfnetwork
object. https://michaeldorman.github.io/nngeo/index.htmlThe below code takes the four points as nodes, connects each to its two closest neighbors, and creates a network from the resulting connections. Depending on your data, the number of connections may need to be adjusted. A problem could arise with clusters of points distant enough from others to create an island separated from the rest of the points.
Created on 2022-11-07 with reprex v2.0.2