Last data update: 2014.03.03

R: Least Cost Moving Windows Calculation
lcmwR Documentation

Least Cost Moving Windows Calculation

Description

This is a moving window that for each cell returns the minimum 'cost' based on surrounding data cells and some dispersal distance cost.

Usage

lcmw(mat, mw, mnc)

Arguments

mat

a matrix of values that can be based on a raster dataset. Lower values should represent lower cost. The matrix can be a raster of class 'asc' (adehabitat package), 'RasterLayer' (raster package) or 'SpatialGridDataFrame' (sp package)

mw

a distance-cost matrix to be applied to each cell of 'mat'. This matrix can be dispersal costs. Lower values should represent lower cost.

mnc

an integer value representing the radius for 'mw' in number of cells.

Details

This method moves over the matrix of values, summing the moving window cost mw and the matrix mat, returning the minimum cost value. This was created to estimate the least cost path through time for all cells in a matrix (see example).

Value

A matrix of values of the same dimensions and class as input mat

Author(s)

Jeremy VanDerWal jjvanderwal@gmail.com

Examples

#create a simple object of class 'asc'
tasc = as.asc(matrix(1:100,nr=10,nc=10)); print(tasc)

#show the input matrix
print(tasc[1:10,1:10])

#vary the moving windows

###no cost window of 2 cell radius
tcost = matrix(0,nr=5,nc=5); print(tcost)
out = lcmw(tasc, tcost, 2); print(out[1:10,1:10])

###no cost with a circular radius of 2
tcost = matrix(NA,nr=5,nc=5)
#populate the distances
for (y in 1:5){
    for (x in 1:5){
        tcost[y,x] = sqrt((3-y)^2 + (3-x)^2)
    }
}

#remove distance values > max.num.cells
tcost[which(tcost>2)]=NA

#no cost matrix
tcost1 = tcost; tcost1[is.finite(tcost1)]=1; print(tcost1)
out = lcmw(tasc, tcost1, 2); print(out[1:10,1:10])

#linear cost
tcost = tcost/2; print(tcost)
out = lcmw(tasc, tcost, 2); print(out[1:10,1:10])

Results


R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(SDMTools)
> png(filename="/home/ddbj/snapshot/RGM3/R_CC/result/SDMTools/lcmw.Rd_%03d_medium.png", width=480, height=480)
> ### Name: lcmw
> ### Title: Least Cost Moving Windows Calculation
> ### Aliases: lcmw
> 
> ### ** Examples
> 
> #create a simple object of class 'asc'
> tasc = as.asc(matrix(1:100,nr=10,nc=10)); print(tasc)
Raster map of class "asc":
Cell size:  1 
Number of rows:  10 
Number of columns:  10 
Type:  numeric 
> 
> #show the input matrix
> print(tasc[1:10,1:10])
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    1   11   21   31   41   51   61   71   81    91
 [2,]    2   12   22   32   42   52   62   72   82    92
 [3,]    3   13   23   33   43   53   63   73   83    93
 [4,]    4   14   24   34   44   54   64   74   84    94
 [5,]    5   15   25   35   45   55   65   75   85    95
 [6,]    6   16   26   36   46   56   66   76   86    96
 [7,]    7   17   27   37   47   57   67   77   87    97
 [8,]    8   18   28   38   48   58   68   78   88    98
 [9,]    9   19   29   39   49   59   69   79   89    99
[10,]   10   20   30   40   50   60   70   80   90   100
> 
> #vary the moving windows
> 
> ###no cost window of 2 cell radius
> tcost = matrix(0,nr=5,nc=5); print(tcost)
     [,1] [,2] [,3] [,4] [,5]
[1,]    0    0    0    0    0
[2,]    0    0    0    0    0
[3,]    0    0    0    0    0
[4,]    0    0    0    0    0
[5,]    0    0    0    0    0
> out = lcmw(tasc, tcost, 2); print(out[1:10,1:10])
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    1    1    1   11   21   31   41   51   61    71
 [2,]    1    1    1   11   21   31   41   51   61    71
 [3,]    1    1    1   11   21   31   41   51   61    71
 [4,]    2    2    2   12   22   32   42   52   62    72
 [5,]    3    3    3   13   23   33   43   53   63    73
 [6,]    4    4    4   14   24   34   44   54   64    74
 [7,]    5    5    5   15   25   35   45   55   65    75
 [8,]    6    6    6   16   26   36   46   56   66    76
 [9,]    7    7    7   17   27   37   47   57   67    77
[10,]    8    8    8   18   28   38   48   58   68    78
> 
> ###no cost with a circular radius of 2
> tcost = matrix(NA,nr=5,nc=5)
> #populate the distances
> for (y in 1:5){
+     for (x in 1:5){
+         tcost[y,x] = sqrt((3-y)^2 + (3-x)^2)
+     }
+ }
> 
> #remove distance values > max.num.cells
> tcost[which(tcost>2)]=NA
> 
> #no cost matrix
> tcost1 = tcost; tcost1[is.finite(tcost1)]=1; print(tcost1)
     [,1] [,2] [,3] [,4] [,5]
[1,]   NA   NA    1   NA   NA
[2,]   NA    1    1    1   NA
[3,]    1    1    1    1    1
[4,]   NA    1    1    1   NA
[5,]   NA   NA    1   NA   NA
> out = lcmw(tasc, tcost1, 2); print(out[1:10,1:10])
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    2    2    2   12   22   32   42   52   62    72
 [2,]    2    2    3   13   23   33   43   53   63    73
 [3,]    2    3    4   14   24   34   44   54   64    74
 [4,]    3    4    5   15   25   35   45   55   65    75
 [5,]    4    5    6   16   26   36   46   56   66    76
 [6,]    5    6    7   17   27   37   47   57   67    77
 [7,]    6    7    8   18   28   38   48   58   68    78
 [8,]    7    8    9   19   29   39   49   59   69    79
 [9,]    8    9   10   20   30   40   50   60   70    80
[10,]    9   10   11   21   31   41   51   61   71    81
> 
> #linear cost
> tcost = tcost/2; print(tcost)
     [,1]      [,2] [,3]      [,4] [,5]
[1,]   NA        NA  1.0        NA   NA
[2,]   NA 0.7071068  0.5 0.7071068   NA
[3,]    1 0.5000000  0.0 0.5000000    1
[4,]   NA 0.7071068  0.5 0.7071068   NA
[5,]   NA        NA  1.0        NA   NA
> out = lcmw(tasc, tcost, 2); print(out[1:10,1:10])
      [,1]     [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]  1.0 1.500000    2   12   22   32   42   52   62    72
 [2,]  1.5 1.707107    3   13   23   33   43   53   63    73
 [3,]  2.0 2.707107    4   14   24   34   44   54   64    74
 [4,]  3.0 3.707107    5   15   25   35   45   55   65    75
 [5,]  4.0 4.707107    6   16   26   36   46   56   66    76
 [6,]  5.0 5.707107    7   17   27   37   47   57   67    77
 [7,]  6.0 6.707107    8   18   28   38   48   58   68    78
 [8,]  7.0 7.707107    9   19   29   39   49   59   69    79
 [9,]  8.0 8.707107   10   20   30   40   50   60   70    80
[10,]  9.0 9.707107   11   21   31   41   51   61   71    81
> 
> 
> 
> 
> 
> dev.off()
null device 
          1 
>