I am trying to connect bivariate location points together to form a route. My points refer to fish locations, and so I need paths that only pass through water, and not the surro
You could use a minimum spanning tree, where the cost on each edge is a function of distance and other factors. In your case, set the weight to infinity if an edge crosses non-water, making sure it never gets used unless they are required at the end to connect the graph. If the graph is non-connected, you can run two or more minimum spanning trees on each disconnected group of points. See wikipedia for easily implemented algorithms, notably Prim's algorithm.
It is possible to do what you wish using the gdistance
package. It may be an issue with the size of your raster (i.e. it is too big for memory), in which case you can upscale it with aggregate()
from the raster
package. It may also be an issue with your parameterization of land and sea as noted in another comment.
Here is an example of what I believe you want to achieve (below). I have parameterized the land as a high cost barrier (=10000 cost units), and the sea as no barrier (=1 cost unit). Note also that I take the inverse to produce a conductance surface. If you want the lengths of the paths between locations, it can be done with costDistance()
and this will give you the result as a geographic path length in the units of the raster.
library(gdistance)
## Create cost surface where "land" exists in the middle
cost <- raster(nrow=100, ncol=100,
xmn=0, xmx=100, ymn=0, ymx=100, crs="+proj=utm")
cost[] <- 1
cost[cellFromRowColCombine(cost, 50:55,20:80)] <- 10000
## Produce transition matrices, and correct because 8 directions
trCost <- transition(1/cost, mean, directions=8)
trCost <- geoCorrection(trCost, type="c")
## Create three points (representing three points in time series)
pts <- cbind(x=c(20, 60, 40), y=c(80, 60, 20))
## Display results
plot(cost)
plot(SpatialPoints(pts), add=TRUE, pch=20, col="red")
text(pts[,1]+2, pts[,2]+2, 1:nrow(pts))
plot(shortestPath(trCost, pts[1,], pts[2,], output="SpatialLines"), add=TRUE)
plot(shortestPath(trCost, pts[2,], pts[3,], output="SpatialLines"), add=TRUE)