-
Notifications
You must be signed in to change notification settings - Fork 16
/
test_metric_distance.R
305 lines (265 loc) · 13.4 KB
/
test_metric_distance.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#' Test the ECA or ProtConn metrics using multiple dispersal distances
#'
#' @param nodes Object of class sf, sfc, sfg or SpatialPolygons.
#' @param attribute character. Column name with the nodes attribute. If NULL, then the nodes area will be estimated and used as the attribute.
#' @param distance1 list. Distance parameters. For example: type, resistance,or keep. For "type" choose one of the distances: "centroid" (faster), "edge",
#' "least-cost distance" or "commute distance". If the type is equal to "least-cost distance" or "commute distance", then you have to use the "resistance" argument. "keep" is a numeric value used for higher processing.
#' To See more options consult the help function of distancefile().
#' @param distance2 list. see distance1 argument
#' @param distance3 list. see distance1 argument
#' @param distance4 list. see distance1 argument
#' @param metric character. "IIC" to estimate the ECA using the IIC index, "PC" to estimate the ECA using the PC index or "ProtConn" to estimate the ProtConn indicator using the PC index.
#' @param probability numeric. numeric. Connection probability to the selected distance threshold, e.g., 0.5
#' (default) that is 50 percentage of probability connection. Use in case of selecting the "PC"
#' metric or "ProtConn". If probability = NULL, then it will be the inverse of the mean dispersal distance
#' for the species (1/α; Hanski and Ovaskainen 2000).
#' @param distance_thresholds numeric. Distances thresholds (minimum 3) to establish connections. For example, one distance: distance_threshold = 30000; two or more specific distances:
#' distance_thresholds = c(30000, 50000, 100000); sequence distances (recommended): distance_thresholds = seq(10000,100000, 10000).
#' @param region object of class sf, sfc, sfg or SpatialPolygons. If metric is equal to "ProtConn" then you must provide a region shapefile.
#' @param transboundary numeric. Buffer to select polygones in a second round, their attribute value = 0, see Saura et al. 2017. You can set one transboundary value or one per each threshold distance.
#' @param LA numeric. Maximum landscape attribute (attribute unit, if attribute is NULL then unit is equal to m2).
#' @param groups Selected representative threshold distances (distance just before the biggest changes in connectivity metric)
#' @param area_unit character. If attribute is NULL you can set an area unit, "Makurhini::unit_covert()"
#' compatible unit(e.g., "m2", "km2", "ha"). Default equal to hectares "ha".
#' @param write character. Folder path and prefix, for example: "C:/Folder/test".
#' @param intern logical. Show the progress of the process, default = TRUE. Sometimes the advance process does not reach 100 percent when operations are carried out very quickly.
#' @references Correa Ayram, C. A., Mendoza, M. E., Etter, A., & Pérez Salicrup, D. R. (2017). Anthropogenic impact on habitat connectivity: A multidimensional human footprint index evaluated in a highly biodiverse landscape of Mexico. Ecological Indicators, 72, 895–909. https://doi.org/10.1016/j.ecolind.2016.09.007
#' @export
#' @examples
#' \dontrun{
#' library(Makurhini)
#' library(sf)
#'
#' data("list_forest_patches", package = "Makurhini")
#' data("study_area", package = "Makurhini")
#'
#' Max_attribute <- unit_convert(st_area(study_area), "m2", "ha")
#'
#' test_metric_distance(nodes = list_forest_patches[[1]],
#' distance1 =list(type= "centroid"),
#' distance2 =list(type= "edge"),
#' attribute = NULL, area_unit = "ha",
#' LA = Max_attribute ,
#' distance_thresholds = seq(10000,100000, 10000),
#' groups = 0)
#'
#'
#' data("Protected_areas", package = "Makurhini")
#' data("regions", package = "Makurhini")
#' region <- regions[1,]
#'
#' test_metric_distance(nodes = Protected_areas,
#' distance1 =list(type= "centroid"),
#' distance2 =list(type= "edge", keep = 0.05),
#' metric = "ProtConn", probability = 0.5,
#' area_unit = "ha",
#' region = region, transboundary = 50000,
#' distance_thresholds = seq(10000,100000, 10000))
#'}
#' @importFrom magrittr %>%
#' @importFrom progressr handlers handler_pbcol progressor
#' @importFrom crayon bgWhite white bgCyan
#' @importFrom ggplot2 ggplot geom_line geom_point aes theme scale_x_continuous scale_y_continuous element_blank labs scale_colour_manual element_line element_text element_rect
#' @importFrom purrr compact map_dfr
#' @importFrom methods as
#' @importFrom utils tail installed.packages
#' @importFrom sf st_as_sf st_zm st_buffer st_cast st_intersection st_geometry st_difference st_area
test_metric_distance <- function(nodes,
attribute = NULL,
distance1 = NULL,
distance2 = NULL,
distance3 = NULL,
distance4 = NULL,
metric = "IIC", probability = NULL,
distance_thresholds,
region = NULL,
LA = NULL,
transboundary = NULL,
area_unit = "ha",
groups = 3, write = NULL,
intern = TRUE){
if(class(nodes)[1] != "sf") {
nodes <- st_as_sf(nodes)
}
options(warn = -1)
. = NULL
distances_test <- list(distance1, distance2, distance3, distance4)
distances_test <- compact(distances_test)
if (missing(nodes)) {
stop("error missing file of nodes")
} else {
if (is.numeric(nodes) | is.character(nodes)) {
stop("error missing file of nodes")
}
}
if(metric == "ProtConn"){
if (missing(region)) {
stop("error missing file of region")
} else {
if (is.numeric(region) | is.character(region)) {
stop("error missing file of region")
}
}
if (is.null(probability) | !is.numeric(probability)) {
stop("error missing probability")
}
region <- st_zm(region) %>% st_buffer(., dist = 0) %>% st_cast("POLYGON")
nodes <- st_zm(nodes) %>% st_buffer(., dist = 0) %>% st_cast("POLYGON")
nodes$IdTemp <- 1:nrow(nodes); nodes <- nodes[, "IdTemp"]
nodes.2 <- tryCatch(Protconn_nodes(x = region,
y = nodes,
buff = transboundary,
method = "nodes",
xsimplify = FALSE,
metrunit = area_unit,
protconn_bound = FALSE,
delta = FALSE), error = function(err)err)
if(is.null(LA)){
LA <- sum(unit_convert(as.numeric(st_area(region)), "m2", area_unit))
}
nodes <- as(nodes.2[[1]], 'Spatial'); attribute <- "attribute"
}
if (metric=="PC"){
if (is.null(probability) | !is.numeric(probability)) {
stop("error missing probability")
}
}
if(is.null(groups)){
groups <- 0
}
#Id
nodes$IdTemp <- 1:nrow(nodes); loop <- 1:length(distances_test)
if(isTRUE(intern)){
handlers(global = T)
handlers(handler_pbcol(complete = function(s) crayon::bgYellow(crayon::white(s)),
incomplete = function(s) crayon::bgWhite(crayon::black(s)),
intrusiveness = 2))
pb <- progressor(along = loop)
}
conn_metric <- map_dfr(loop, function(x){
x.1 <- distances_test[[x]]
ECA_metric <- map_dfr(distance_thresholds, function(y){
tab1 <- tryCatch(MK_dPCIIC(nodes = nodes, attribute = attribute,
restoration = NULL,
distance = x.1, area_unit = area_unit,
metric = if(metric=="ProtConn"){"PC"}else{metric},
probability = probability,
distance_thresholds = y,
overall = TRUE, onlyoverall = TRUE,
LA = LA, rasterparallel = NULL, write = NULL), error = function(err)err)
if (inherits(tab1, "error")){
stop(tab1)
}
tab1 <- tab1[2,2]
if(metric=="ProtConn"){
tab1 <- 100 * (tab1 / LA)
}
return(data.frame(Value = tab1))
})
if(length(distances_test)>1 & isTRUE(intern)){
pb()
}
conn_metric2 <- cbind(ECA_metric, distance_thresholds)
names(conn_metric2) <- c(if(metric=="ProtConn"){"ProtConn"}else{"ECA"}, "Distance")
conn_metric2$Group <- x.1$type
return(conn_metric2)
})
###Differences
peaks_groups <- list(); unique_groups <- unique(conn_metric$Group)
###Groups return
if(groups > 0){
for (i in 1:length(unique_groups)){
table1 <- conn_metric[conn_metric$Group == unique_groups[i],]
dif1 <- table1[,1]
change <- list()
for(j in 2:length(dif1)){
change[[j]] <- (abs(dif1[j]-dif1[j-1])*100)/dif1[j]
}
change <- do.call(rbind, change) %>% as.data.frame()
change$metric <- table1[2:nrow(table1),1]
change$distance <- table1$Distance[2:nrow(table1)]
change <- change[order(change$V1),]
change <- change[(nrow(change)-(groups-1)):nrow(change),]
change$Group <- 1:groups
change <- change[order(change$distance),]
peak <- data.frame(Group = change$Group)
peak$Dist_from <- c(min(table1$Distance), format(change$distance[1:(groups-1)], scientific = F))
peak$Dist_to <- c(format(change$distance[1:(groups)], scientific = F))
peak$Dist_to[groups] <- format(max(table1$Distance), scientific = F)
peak$metric <- change$metric
names(peak)[4] <- if(metric=="ProtConn"){"ProtConn"}else{"ECA"}
if(length(unique(peak[,4])) == 1){
peak <- peak[which(peak$Group ==1),]
}
peaks_groups[[i]] <- peak
}
names(peaks_groups) <- unique_groups
###Groups plot
peak_plot <- list()
for(i in 1:length(unique_groups)){
peaks_p <- cbind(peaks_groups[[i]], unique_groups[[i]])
names(peaks_p)[c(3, 5)] <- c("To", "plot_group")
peak_plot[[i]] <- peaks_p
}
peak_plot <- do.call(rbind, peak_plot)
}
###Plot
if(is.null(attribute)){
ytitle = if(metric=="ProtConn"){"ProtConn (%)"}else{"ECA (ha)"}
} else {
ytitle = if(metric=="ProtConn"){"ProtConn (%)"}else{paste0("ECA", " (", area_unit, ")")}
}
ccolour <- c("#E16A86", "#909800", "#00AD9A", "#9183E6")[1:length(unique_groups)]
p1 <- ggplot() +
geom_line(aes(y = conn_metric[,1], x = conn_metric$Distance, colour = conn_metric$Group),
size = 1.5, data = conn_metric,
stat="identity") +
theme(legend.position="bottom", legend.direction="horizontal",
legend.title = element_blank()) +
scale_x_continuous(breaks = c(seq(min(conn_metric$Distance), max(conn_metric$Distance),
max(conn_metric$Distance)/10)[2:10] -min(conn_metric$Distance),
max(conn_metric$Distance)),
labels = function(x) format(x, scientific = FALSE)) +
scale_y_continuous(breaks = c(seq(min(conn_metric[,1]), max(conn_metric[,1]),
(max(conn_metric[,1])-min(conn_metric[,1]))/4)),
labels = function(x) round(x, 2))+
labs(x = "Dispersal distance", y = ytitle) +
scale_colour_manual(values = ccolour) +
theme(axis.line = element_line(size = 1, colour = "black"),
panel.grid.major = element_line(colour = "#d3d3d3"), panel.grid.minor = element_blank(),
panel.border = element_blank(), panel.background = element_blank()) +
theme(plot.title = element_text(size = 14, family = "Tahoma", face = "bold"),
text = element_text(family = "Tahoma", size = 12),
axis.text.x= element_text(colour = "black", size = 10, angle = 90),
axis.text.y= element_text(colour = "black", size = 10),
legend.key= element_rect(fill = "white", colour = "white"),
legend.text = element_text(size = 11))
if(groups > 0){
peak_plot$To <- as.numeric(peak_plot$To)
p1 <- p1 + geom_point(data = peak_plot,
mapping = aes(x = conn_metric[which(conn_metric[,1] %in% peak_plot[,4]),2],
y = peak_plot[,4]), size = 4)+
geom_text(aes(x = conn_metric[which(conn_metric[,1] %in% peak_plot[,4]),2],
y = peak_plot[,4],
label = conn_metric[which(conn_metric[,1] %in% peak_plot[,4]),2]),
data = peak_plot, hjust = -0.3, vjust = -0.5)+
theme(legend.text = element_text(size = 11))
}
result <- list(conn_metric,if(groups>0){peaks_groups}, p1)
result <- purrr::compact(result)
if(groups > 0){
names(result) <- c("Table_metric_test", "Table_groups_test", "plot_metric_test")
} else {
names(result) <- c("Table_metric_test", "plot_metric_test")
}
if(!is.null(write)){
ggsave(paste0(write, '_metricTest.tif'), plot = p1, device = "tiff", width = 12,
height = 8, compression = "lzw")
names(peak_plot)[3] <- "To(dispersal distance)"
write.csv(conn_metric, paste0(write, '_MetricValTest.csv'), row.names = FALSE)
if(groups > 0){
write.csv(peak_plot, paste0(write, '_GroupsTest.csv'), row.names = FALSE)
}
}
return(result)
}