# [R-sig-Geo] Cost allocation from cost distance

Jacob van Etten jacobvanetten at yahoo.com
Tue Jul 12 17:19:35 CEST 2011

```I am not aware of a function that does this in R.

But this is not difficult to program with existing raster and gdistance functions.

This toy example works:

library(gdistance)

costAll <- function(fromCoords, cost)
{
aC <- stack(cost,cost)
cT <- transition(cost, mean, 8)
cT <- geoCorrection(cT, scl=FALSE)
for(i in 1:dim(fromCoords)) {aC <- stack(aC, accCost(cT, fromCoords[i,]))}
func <- function(x) which(x == min(x))
aC <- dropLayer(aC,1:2)
alC <- calc(aC, func)
return(alC)
}

fromCoords <- cbind(c(-80,50),c(50,-80))
cost <- raster(ncol=36,nrow=18)
cost <- setValues(cost,rep(1,times=ncell(cost)))
cA <- costAll(fromCoords, cost)

plot(cA)
points(fromCoords)

Jacob.

--- On Tue, 12/7/11, Etienne B. Racine <etiennebr at gmail.com> wrote:

> From: Etienne B. Racine <etiennebr at gmail.com>
> Subject: [R-sig-Geo] Cost allocation from cost distance
> To: "r-sig-geo" <R-sig-Geo at stat.math.ethz.ch>
> Date: Tuesday, 12 July, 2011, 15:40
> Hi all,
>
> I'm looking for a function which would allow to compute on
> a matrix the id
> of the closest point according to a matrix of cost. I've
> used
> gdistance::accCost() to get the cost, but I need to relate
> that cost to
> points. A good description of the function I'm looking for
> would be from
> ESRI Cost Allocation tool . I couldn't find an
> equivalent function in R.
>
> 
> http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/Cost_Allocation/009z00000016000000/
>
> Etienne
>
>     [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

```