-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathgl.costdistances.r
104 lines (96 loc) · 3.66 KB
/
gl.costdistances.r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#' Calculates cost distances for a given landscape (resistance matrix)
#'
#' @param landscape A raster object coding the resistance of the landscape
#' [required].
#' @param locs Coordinates of the subpopulations. If a genlight object is
#' provided coordinates are taken from @other$latlon and centers for population
#' (pop(gl)) are calculated. In case you want to calculate costdistances between
#' individuals redefine pop(gl) via: \code{pop(gl)<- indNames(gl)} [required].
#' @param method Defines the type of cost distance, types are 'leastcost',
#' 'rSPDistance' or 'commute' (Circuitscape type) [required].
#' @param NN Number of next neighbours recommendation is 8 [required].
#' @return A costdistance matrix between all pairs of locs.
#' @param verbose Verbosity: 0, silent or fatal errors; 1, begin and end; 2,
#' progress log ; 3, progress and results summary; 5, full report
#' [default 2, unless specified using gl.set.verbosity].
#' @description
#' Calculates a cost distance matrix, to be used with run.popgensim.
#' @export
#' @examples
#' \dontrun{
#' data(possums.gl)
#' library(raster) #needed for that example
#' landscape.sim <- readRDS(system.file('extdata','landscape.sim.rdata', package='dartR'))
#' #calculate mean centers of individuals per population
#' xy <- apply(possums.gl@other$xy, 2, function(x) tapply(x, pop(possums.gl), mean))
#' cd <- gl.costdistances(landscape.sim, xy, method='leastcost', NN=8)
#' round(cd,3)
#' }
gl.costdistances <- function(landscape,
locs,
method,
NN,
verbose = NULL) {
# SET VERBOSITY
verbose <- gl.check.verbosity(verbose)
# FLAG SCRIPT START
funname <- match.call()[[1]]
utils.flag.start(func = funname,
build = "Jody",
verbosity = verbose)
# CHECK IF PACKAGES ARE INSTALLED
pkg <- "gdistance"
if (!(requireNamespace(pkg, quietly = TRUE))) {
stop(error(
"Package",
pkg,
" needed for this function to work. Please install it."
))
}
if (class(locs) == "genlight") {
if (is.null(locs@other$latlon)) {
stop(
error(
"No locations were provided in the genlight object [@other$latlon].\n"
)
)
}
if (is.null(pop(locs))) {
cat(
warn(
"No population definition provided, hence I will calculate costdistances between individuals\n"
)
)
pop(locs) <- indNames(locs)
if (is.null(pop(locs))) {
pop(locs) <- 1:nInd(locs)
}
}
locs <-
apply(locs@other$latlon, 2, function(x)
tapply(x, pop(locs), mean))
} else {
locs <- as.matrix(locs)
}
fric.mat <-
gdistance::transition(landscape, function(x)
1 / x[2], NN)
# set distances to meters if no projected already
fric.mat@crs@projargs <- "+proj=merc +units=m"
fric.mat.cor <- gdistance::geoCorrection(fric.mat)
if (method == "leastcost")
cd.mat <- gdistance::costDistance(fric.mat.cor, locs, locs)
if (method == "rSPDistance")
cd.mat <-
gdistance::rSPDistance(fric.mat.cor, locs, locs, theta = 1)
if (method == "commute")
cd.mat <-
as.matrix(gdistance::commuteDistance(fric.mat.cor, locs))
colnames(cd.mat) <- row.names(locs)
rownames(cd.mat) <- row.names(locs)
# FLAG SCRIPT END
if (verbose > 0) {
cat(report("Completed:", funname, "\n"))
}
return(cd.mat)
}