Least cost path with multiple points

后端 未结 2 2030
再見小時候
再見小時候 2021-02-10 19:20

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

2条回答
  •  抹茶落季
    2021-02-10 19:59

    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)
    

    Example plot of least-cost paths around land

提交回复
热议问题