UrbanAnalyst / dodgr

Distances on Directed Graphs in R
https://urbananalyst.github.io/dodgr/
128 stars 16 forks source link

Use off land #98

Closed Maschette closed 5 years ago

Maschette commented 5 years ago

This package looks great but I was wondering if there is a way to move it off land for example I have a bunch of points in water and want to calculate a distance matrix between all the points without going over the continent between them?

JimShady commented 5 years ago

For the distance matrix, you will want to use the dodgr_dists() function . Of course you will need a graph first to perform the function on.

The package vignette gives an example of how your graph should be set-up.

graph <- data.frame (from = c ("A", "B", "B", "B", "C", "C", "D", "D"),
                     to = c ("B", "A", "C", "D", "B", "D", "C", "A"),
                     d = c (1, 2, 1, 3, 2, 1, 2, 1))

dodgr_dists (graph)

  A B C D
A 0 1 2 3
B 2 0 1 2
C 2 2 0 1
D 1 2 2 0

Can you create your network? If you don't want your network to go 'over land' you could first create your network, and then use something like st_intersection from the sf package to remove any lines in your network that cross land.

mpadge commented 5 years ago

Thanks for the Q @Maschette. It's a cool idea, but as @JimShady pointed out, dodgr is hard-coded to use network representations of systems, and your use case is really beyond that domain. You could do it by dodgr-ising your coastlines, finding shortest distances between your origin and destination points and these coastlines, and adding those to the network to create a "routable" network representation. But that would not likely be the most efficient approach. I suspect the gdistance package would like offer more efficient alternatives, because it enables routing on raster grids with defined properties. Their examples are mostly about gradients and the like, but you could likely flag ocean as impassable and get your desired result. @mdsumner would likely be able to offer useful advice on that package. Feel free to ask any further Q's in the meantime.