luukvdmeer / sfnetworks

Tidy Geospatial Networks in R
https://luukvdmeer.github.io/sfnetworks/
Other
347 stars 20 forks source link

Function that finds the nearest nodes or edges to a spatial feature #20

Closed luukvdmeer closed 4 years ago

luukvdmeer commented 4 years ago

Is your feature request related to a problem? Please describe. Especially when calculating shortest paths, it should be possible to enter any point in space as to and from. Then, a function should find the nearest node in the sfnetwork object, for both this to and from point, and calculate the shortest path in the network accordingly. Also, when you have additional data that you want to join with the network, but the coordinates are not exactly matching, this functionality would be useful.

Describe the solution you'd like A function st_nearest_node that takes an sfnetwork object and an sf/sfg/sfc object. Then, it will find for each feature in the sf/sfg/sfc object the nearest node in the sfnetwork object, and replace the geometry of the feature with the geometry of its nearest node.

luukvdmeer commented 4 years ago

After finding out about sf's st_nearest_feature(x,y): the proposed st_nearest_node(x,y) function would be equal to st_nearest_feature(x,y) where y is an object of class sfnetwork. Although the documentation specifies that argument y should be of class sf, sfc or sfg, it already works for sfnetworks objects, because the function takes the geometries of the input objects, and st_geometry works on sfnetwork objects as well.

net = as_sfnetwork(roxel)
st_nearest_feature(st_centroid(roxel), net)

Still I think st_nearest_node would be semantically more intuitive in the case of working with networks. Also, this will skip the need to have the nodes activated when applying the function.

Similarly, st_nearest_edge would work on the edges.

luukvdmeer commented 4 years ago

After thinking more about it, this does not really fit good into tidygraph philosophy. Rather than creating separate, but similar functions for nodes and edges, you should use the activate verb and apply the same function.