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 surrounding land. Basically, I need to do multiple least cost path analyses and join them altogether. I would like to do this in R, as I have more experience there than I do with ArcGIS, python, modelbuilder, etc.
I have used ArcGIS to create a cost surface, with water coded as 0 and land coded as “NoData”, and have imported this into R.
I have tried using shortestPath from the gdistance pkg, but R always shuts down on me when I try running it even just between two points. For example:
costpath=shortestPath(costtrans,c29924[1,],c29924[2,],output="SpatialLines")
where my cost surface is “costtrans”, and “c29924” is a SpatialPointsDataFrame with my lat/long locations. I had planned to run a loop so that I could do this for each row in the data frame. However, I do not know why R is not handling even one iteration well. When converting my cost surface to a transition object for the first arguements, I do receive the following warning messages:
Warning messages:
1: In array(ans, c(len.a%/%d2, d.ans), if (!all(vapply(dn.ans, is.null, :
Reached total allocation of 6057Mb: see help(memory.size)
2: In array(ans, c(len.a%/%d2, d.ans), if (!all(vapply(dn.ans, is.null, :
Reached total allocation of 6057Mb: see help(memory.size)
3: In as.vector(transition.values) :
Reached total allocation of 6057Mb: see help(memory.size)
4: In as.vector(transition.values) :
Reached total allocation of 6057Mb: see help(memory.size)
5: In .TM.repl.i.mat(as(x, "TsparseMatrix"), i = i, value = value) :
number of items to replace is not a multiple of replacement length
Any suggestions for solving this, or other approaches to my initial goal would be greatly appreciated!
It is possible to do what you wish using the
gdistancepackage. 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 withaggregate()from therasterpackage. 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.