josephlewis / leastcostpath

leastcostpath: Modelling Pathways and Movement Potential Within a Landscape
60 stars 7 forks source link

Results seem to be not least costly; also, feasibility to go from starting point(s) to multiple end points? #12

Closed bpfree closed 1 year ago

bpfree commented 1 year ago

Trying to create a simple least cost path from 1 starting point to a couple of ending points across a cost raster.

The aim would be to produce results that are similar to what are produced through analyses as from ArcGIS’s least cost path tool or this post from StackExchange.

I have all the relevant code and data in this GitHub. Code for this particular for this analysis is here, while data are spread across these three directories:

Some background, there is a single starting location (for simplification) and 4 destination locations (for simplification). Plus there is a cost raster that is the sum of multiple costs. I am working with leastcostpath version 2.0.4:

Screenshot 2022-12-01 at 08 30 13

When I plot the generated pathways over the original cost raster, it looks like the lines take a costly path. The darker areas are less costly (lower values) than the lighter ones, yet the path routes over lighter areas.

For the below images pink line are pathways using the create_slope_cs() on my raster, while the cyan lines are using create_cs() on my cost raster.

Screenshot 2022-12-01 at 08 24 08 Screenshot 2022-12-01 at 08 24 18 Screenshot 2022-12-01 at 08 24 26

Two more screenshots of what happens within ArcGIS and how the lines go the expected way.

image image (1)

Given this description, is it possible to use this package to get the desired output? If so, any suggestions on where I am going wrong using the package using these data?

josephlewis commented 1 year ago

Hi @bpfree,

Thanks for opening the issue.

I will be implementing multiple starting points to multiple end points in the foreseeable future. For now, this can be done by changing the arguments in origin and destination in the create_lcp function

Are you able to create a toy, reproducible example? and/or a single script that I can use to test this. I don't know the structure of the multiple file linked here.

In the meantime, be sure that the surfaces represent the conductivity of movement (leastcostpath uses conductivity rather than cost, i.e. the inverse of cost). This is one of the main differences between ArcGIS and leastcostpath

Hope that helps.

bpfree commented 1 year ago

@josephlewis

Thanks for the prompt reply and thoughtful answers. Good to hear about the functionality for handling multiple starting and/or ending locations. Yes, I have been changing the arguments in the mean time.

As requested, here is a stripped down version of the analysis (also have attached the directory if that is easier).

Even with conductivity, unless I am understanding it wrong, I would have expected the path to go down to small values (less conductive) and then move along similar values till it reaches the destination. Also when I inspect the path it seems to wriggle more than expected as it moves across the conductanceMatrix.

When I manually inspect the paths against the raster, the paths do not go towards the darker areas -- ones with lower values and where it would be expected to pass through.

Hopefully this has been helpful.

leastcostpath_temp.zip

josephlewis commented 1 year ago

@bpfree Thanks for providing a stripped down version. Very helpful!

If you want to use your already created cost surface (e.g. costs_barriers_extracted) within leastcostpath you'll need to use create_cs. create_slope_cs() is used for digital elevation models with the change in elevation used to calculate a cost (in the case of leastcostpath, time, energy or preference when using wheeled vehicles, or a user-supplied function).

cs <- leastcostpath::create_cs(x = cost_raster, neighbours = 4)

You can view a spatRaster version of the cs using rasterise. This will summarise across all the connections for each cell but it'll give you an idea of what your cs looks like (though in your case it should look exactly the same)

cs_rast <- leastcostpath::rasterise(cs) plot(cs_rast)

When calculating the LPCs using cs this is what I get. Is this more like what you're after?

image

In regards to conductivity, essentially the higher the value the higher the preference for that area (this is in the inverse of cost, where higher cost means lower preference).

bpfree commented 1 year ago

Hi @josephlewis apologies for the month delay. Thanks for the reply. Based on the information I realized I just needed to flip my values (low becomes high, and vice versa) and I got the expected results.

Rplot

Also saw that you updated the package to handle multiple starting and ending destinations, which is great! Thanks