-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path.Rhistory
512 lines (512 loc) · 19.3 KB
/
.Rhistory
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
res <- cbind.data.frame(tax = EOO.poly.name[x],
prop,
stringsAsFactors = FALSE)
if(output_raster %in% "area.table")
area <- round((prop * r.area)/100, 3)
res <- cbind.data.frame(tax = EOO.poly.name[x],
area,
stringsAsFactors = FALSE)
}
}
}
res
# if(is.null(result)) result <- res else result <- rbind.data.frame(result, res, stringsAsFactors = FALSE)
# cat(x, "\n")
}
if(parallel) snow::stopCluster(cl)
if(show_progress) close(pb)
result <- do.call(rbind.data.frame, output)
rownames(result) <- NULL
return(result)
}
EOO.habitat(EOO.poly[1:10,], hab.map, hab.class = NULL, years = NULL,
parallel = TRUE, NbeCores = 4)
require(ConR)
EOO.habitat(EOO.poly[1:10,], hab.map, hab.class = NULL, years = NULL,
parallel = TRUE, NbeCores = 4)
## EOO shapefiles
EOO.poly <- readRDS(file = "C:/Users/renato/Documents/raflima/Pos Doc/Manuscritos/Artigo Extincao na MA/data analysis/data/spp.convex.hull.polys_sf_uncropped.rds")
### HUMAN INFLUENCES ### between 1995 - 2004 (0 low and 65 is high)
hab.map <- raster::raster(paste(path,"//Am_Lat_Global_Human_Influence_Index_v2//hii_global//hii_neotrop.tif",sep=""))
## ESA land cover
path <- "E://ownCloud//W_GIS" # Renato's path
### HUMAN INFLUENCES ### between 1995 - 2004 (0 low and 65 is high)
hab.map <- raster::raster(paste(path,"//Am_Lat_Global_Human_Influence_Index_v2//hii_global//hii_neotrop.tif",sep=""))
image(hab.map)
raster::image(hab.map)
EOO.habitat(EOO.poly[1:10,], hab.map, hab.class = NULL, years = NULL,
parallel = TRUE, NbeCores = 4)
toto2.1 <- EOO.habitat(EOO.poly[1:50,], hab.map, hab.class = NULL, years = NULL,
parallel = TRUE, NbeCores = 4)
x=1
proj_crs <- proj_crs(proj_type = proj_type)
proj_hab <- raster::crs(hab.map)
if(is.na(sf::st_crs(EOO.poly)))
sf::st_crs(EOO.poly) <- 4326
EOO.poly <- sf::st_transform(EOO.poly, crs = proj_hab)
# Croping EOO shapefiles to the raster extent
EOO.poly.crop <- suppressWarnings(
sf::st_intersection(EOO.poly, sf::st_as_sfc(sf::st_bbox(hab.map))))
if (length(EOO.poly$geometry) > length(EOO.poly.crop$geometry))
warning(paste0("The EOO of ",
length(EOO.poly$geometry) - length(EOO.poly.crop$geometry),
" species became empty after croping them to the habitat map extent"))
EOO.poly.proj <- sf::st_transform(EOO.poly.crop, crs = proj_crs)
EOO.poly.area <- as.double(sf::st_area(EOO.poly.proj))/1000000 # area in km2
EOO.poly.name <- EOO.poly.crop
sf::st_geometry(EOO.poly.name) <- NULL
EOO.poly.name <- EOO.poly.name[,1]
if(any(grepl("SpatialPolygon", class(hab.map))))
hab.map <- sf::st_as_sf(hab.map)
if (is.null(hab.class) & any(grepl("Raster", class(hab.map))))
classes <- seq(hab.map[[1]]@data@min, hab.map[[1]]@data@max, 1)
### HUMAN INFLUENCES ### between 1995 - 2004 (0 low and 65 is high)
hab.map <- raster::raster(paste(path,"//Am_Lat_Global_Human_Influence_Index_v2//hii_global//hii_neotrop.tif",sep=""))
hab.class = NULL
years = NULL
export_shp = FALSE
plot = FALSE
output_raster = "summary"
proj_type = "cea"
parallel = TRUE
NbeCores = 4
show_progress = TRUE
proj_crs <- proj_crs(proj_type = proj_type)
proj_hab <- raster::crs(hab.map)
if(is.na(sf::st_crs(EOO.poly)))
sf::st_crs(EOO.poly) <- 4326
EOO.poly <- sf::st_transform(EOO.poly, crs = proj_hab)
# Croping EOO shapefiles to the raster extent
EOO.poly.crop <- suppressWarnings(
sf::st_intersection(EOO.poly, sf::st_as_sfc(sf::st_bbox(hab.map))))
if (length(EOO.poly$geometry) > length(EOO.poly.crop$geometry))
warning(paste0("The EOO of ",
length(EOO.poly$geometry) - length(EOO.poly.crop$geometry),
" species became empty after croping them to the habitat map extent"))
EOO.poly.proj <- sf::st_transform(EOO.poly.crop, crs = proj_crs)
EOO.poly.area <- as.double(sf::st_area(EOO.poly.proj))/1000000 # area in km2
EOO.poly.name <- EOO.poly.crop
sf::st_geometry(EOO.poly.name) <- NULL
EOO.poly.name <- EOO.poly.name[,1]
if(any(grepl("SpatialPolygon", class(hab.map))))
hab.map <- sf::st_as_sf(hab.map)
if (is.null(hab.class) & any(grepl("Raster", class(hab.map))))
classes <- seq(hab.map[[1]]@data@min, hab.map[[1]]@data@max, 1)
x
any(grepl("sf", class(hab.map)))
grepl("Raster", class(hab.map))
ext <- raster::extent(sf::st_bbox(EOO.poly.crop[x, ])[c(1,3,2,4)])
crop <- raster::crop(hab.map, ext) # cropping raster to the extent of the polygon
mask <- raster::mask(crop, EOO.poly.crop[x, ])
tmp <- raster::getValues(mask) # much faster than raster::extract
head(tmp)
class(tmp)
class(tmp) %in% c("numeric","integer")
tmp <- tmp[!is.na(tmp)] # excluding pixels outside the EOO
head(tmp)
#Getting the approximated area of the masked raster (in km2)
mask.area <- raster::area(mask[[1]], na.rm = TRUE)
mask
image(mask)
raster::image(mask)
proj_crs <- proj_crs(proj_type = proj_type)
proj_crs
proj_hab <- raster::crs(hab.map)
proj_hab
is.na(sf::st_crs(EOO.poly))
EOO.poly <- sf::st_transform(EOO.poly, crs = proj_hab)
EOO.poly
image(hab.map)
raster::image(hab.map)
plot(EOO.comp[,1], add=TRUE
)
plot(EOO.poly[,1], add=TRUE
)
raster::image(hab.map)
plot(EOO.poly[,1], add=TRUE
)
proj_crs <- proj_crs(proj_type = proj_type)
proj_hab <- raster::crs(hab.map)
if(is.na(sf::st_crs(EOO.poly)))
sf::st_crs(EOO.poly) <- 4326
EOO.poly <- sf::st_transform(EOO.poly, crs = proj_hab)
# Croping EOO shapefiles to the raster extent
EOO.poly.crop <- suppressWarnings(
sf::st_intersection(EOO.poly, sf::st_as_sfc(sf::st_bbox(hab.map))))
if (length(EOO.poly$geometry) > length(EOO.poly.crop$geometry))
warning(paste0("The EOO of ",
length(EOO.poly$geometry) - length(EOO.poly.crop$geometry),
" species became empty after croping them to the habitat map extent"))
EOO.poly.proj <- sf::st_transform(EOO.poly.crop, crs = proj_crs)
EOO.poly.area <- as.double(sf::st_area(EOO.poly.proj))/1000000 # area in km2
EOO.poly.name <- EOO.poly.crop
sf::st_geometry(EOO.poly.name) <- NULL
EOO.poly.name <- EOO.poly.name[,1]
raster::image(hab.map)
plot(EOO.poly.crop[,1], add=TRUE
)
plot(EOO.poly[,1]
)
raster::image(hab.map, add=TRUE)
raster::image(hab.map)
plot(EOO.poly[,1]
)
raster::image(hab.map, add=TRUE)
raster::image(hab.map)
plot(EOO.poly[,1], add=TRUE
)
raster::image(hab.map, add=TRUE)
if(any(grepl("SpatialPolygon", class(hab.map))))
hab.map <- sf::st_as_sf(hab.map)
if (is.null(hab.class) & any(grepl("Raster", class(hab.map))))
classes <- seq(hab.map[[1]]@data@min, hab.map[[1]]@data@max, 1)
classes
length(classes)
x
ext <- raster::extent(sf::st_bbox(EOO.poly.crop[x, ])[c(1,3,2,4)])
crop <- raster::crop(hab.map, ext) # cropping raster to the extent of the polygon
mask <- raster::mask(crop, EOO.poly.crop[x, ])
tmp <- raster::getValues(mask) # much faster than raster::extract
if(class(tmp) %in% c("numeric","integer"))
tmp <- tmp[!is.na(tmp)] # excluding pixels outside the EOO
if(class(tmp) %in% c("matrix","data.frame"))
tmp <- tmp[!is.na(tmp[,1]),] # excluding pixels outside the EOO
crop
mask
#Getting the approximated area of the masked raster (in km2)
mask.area <- raster::area(mask[[1]], na.rm = TRUE)
mask.area
r.area <- raster::cellStats(mask.area, 'sum')
r.area
!is.null(hab.class)
class(tmp)
if(class(tmp) %in% c("integer","numeric")) {
vals <- tmp # excluding pixels outside the EOO
pixs <- length(vals)
}
if(class(tmp) %in% c("matrix","data.frame")) {
last <- dim(tmp)[2]
vals <- tmp[, last] # excluding pixels outside the EOO
pixs <- length(vals)
}
sumario <- matrix(unclass(summary(vals)), nrow = 1, ncol = 6,
dimnames = list("", c("Min","1st_Qu","Median","Mean","3rd_Qu","Max")))
sumario
plot(ext, add=TRUE, border = 5)
plot(EOO.poly.crop[x, ], add=TRUE, border = 5)
if(output_raster %in% "summary") {
res <- cbind.data.frame(tax = EOO.poly.name[x],
sumario, # ci,
stringsAsFactors = FALSE)
} else {
#Tabulating pixels
tmp1 <- table(factor(vals, levels = classes))
prop <- matrix(round(100*unclass(tmp1)/pixs, 8), nrow = 1, ncol = length(classes),
dimnames = list("", classes))
if(output_raster %in% "prop.table")
res <- cbind.data.frame(tax = EOO.poly.name[x],
prop,
stringsAsFactors = FALSE)
if(output_raster %in% "area.table")
area <- round((prop * r.area)/100, 3)
res <- cbind.data.frame(tax = EOO.poly.name[x],
area,
stringsAsFactors = FALSE)
}
res
rm(hab.map)
rm(list=ls())
#' last rasters are used to calculate habitat loss.
#'
#'
#' @examples #To be included
#'
#' @importFrom raster crs crop extract mask
#' @importFrom sf st_crs st_transform st_intersection st_intersects st_as_sf st_geometry
#'
#'
#'
EOO.habitat <- function(EOO.poly,
hab.map = NULL,
hab.class = NULL,
years = NULL,
export_shp = FALSE,
plot = FALSE,
output_raster = "summary",
proj_type = "cea",
parallel = FALSE,
NbeCores = 2,
show_progress = TRUE
) {
proj_crs <- proj_crs(proj_type = proj_type)
proj_hab <- raster::crs(hab.map)
if(is.na(sf::st_crs(EOO.poly)))
sf::st_crs(EOO.poly) <- 4326
EOO.poly <- sf::st_transform(EOO.poly, crs = proj_hab)
# Croping EOO shapefiles to the raster extent
EOO.poly.crop <- suppressWarnings(
sf::st_intersection(EOO.poly, sf::st_as_sfc(sf::st_bbox(hab.map))))
if (length(EOO.poly$geometry) > length(EOO.poly.crop$geometry))
warning(paste0("The EOO of ",
length(EOO.poly$geometry) - length(EOO.poly.crop$geometry),
" species became empty after croping them to the habitat map extent"))
EOO.poly.proj <- sf::st_transform(EOO.poly.crop, crs = proj_crs)
EOO.poly.area <- as.double(sf::st_area(EOO.poly.proj))/1000000 # area in km2
EOO.poly.name <- EOO.poly.crop
sf::st_geometry(EOO.poly.name) <- NULL
EOO.poly.name <- EOO.poly.name[,1]
if(any(grepl("SpatialPolygon", class(hab.map))))
hab.map <- sf::st_as_sf(hab.map)
if (is.null(hab.class) & any(grepl("Raster", class(hab.map))))
classes <- seq(hab.map[[1]]@data@min, hab.map[[1]]@data@max, 1)
# Extracting the map information for each EOO polygon
if (parallel) {
cl <- snow::makeSOCKcluster(NbeCores)
doSNOW::registerDoSNOW(cl)
message('Parallel running with ',
NbeCores, ' cores')
`%d%` <- foreach::`%dopar%`
} else{
`%d%` <- foreach::`%do%`
}
x <- NULL
if (show_progress) {
pb <-
txtProgressBar(
min = 0,
max = length(EOO.poly.crop$geometry),
style = 3
)
progress <- function(n)
setTxtProgressBar(pb, n)
opts <- list(progress = progress)
} else{
opts <- NULL
}
output <-
foreach::foreach(
x = 1:length(EOO.poly.crop$geometry),
#x = 4000:5000,
#.combine = 'c',
#.packages=c("raster","sf"),
.options.snow = opts
) %d% {
# if (!parallel & show_progress)
# setTxtProgressBar(pb, x)
if(any(grepl("sf", class(hab.map)))) {
crop <- sf::st_intersection(hab.map, EOO.poly.crop[x, ]) # cropping raster to the extent of the polygon
crop.proj <- sf::st_transform(crop, crs = proj_crs)
area.hab <- as.numeric(sum(sf::st_area(crop), na.rm = TRUE))/1000000
tmp <- c(as.numeric(sf::st_area(EOO.poly.crop[x, ]))/1000000 - area.hab, area.hab)
tmp1 <- crop
sf::st_geometry(tmp1) <- NULL
n.polys <- length(unique(as.character(tmp1[,1]))) ### CHECK HERE: FAIL TO GET THE NUMBER OF SHAPE IDs
hab.mat <- matrix(c(NA, n.polys, 100*tmp/EOO.poly.area[x], tmp), ncol = 3, nrow = 2, byrow = FALSE)
row.names(hab.mat) <- c("non_habitat", "habitat")
colnames(hab.mat) <- c("numb.polys","prop.EOO", "area.EOO")
res <- cbind.data.frame(tax = EOO.poly.name[x], hab.mat,
stringsAsFactors = FALSE)["habitat",]
if(export_shp) {
## Tried to combine the two sf objects but could not make it with the df info
# toto <- c(st_geometry(crop), st_geometry(EOO.poly.crop[x, ]))
# merge(toto, crop)
# toto <- sf::st_multipolygon(st_geometry(eoo1), st_geometry(crop1))
# crop1 <- st_cast(crop, "POLYGON")
# eoo1 <- st_cast(EOO.poly.crop[x,], "POLYGON")
res <- list(res, crop)
if (plot) {
par(mar = c(3,3,2,2), las=1, tcl=-0.25, mgp=c(2.5,0.5,0))
plot(sf::st_geometry(EOO.poly.crop[x, ]),
main = EOO.poly.name[x], bg=0)
plot(sf::st_geometry(crop), add = TRUE, col = 1)
}
}
}
# result <- NULL
# for(x in 4000:5000) {
if(grepl("Raster", class(hab.map))) {
ext <- raster::extent(sf::st_bbox(EOO.poly.crop[x, ])[c(1,3,2,4)])
crop <- raster::crop(hab.map, ext) # cropping raster to the extent of the polygon
mask <- raster::mask(crop, EOO.poly.crop[x, ])
tmp <- raster::getValues(mask) # much faster than raster::extract
if(class(tmp) %in% c("numeric","integer"))
tmp <- tmp[!is.na(tmp)] # excluding pixels outside the EOO
if(class(tmp) %in% c("matrix","data.frame"))
tmp <- tmp[!is.na(tmp[,1]),] # excluding pixels outside the EOO
#Getting the approximated area of the masked raster (in km2)
mask.area <- raster::area(mask[[1]], na.rm = TRUE)
r.area <- raster::cellStats(mask.area, 'sum')
if (!is.null(hab.class)) {
# Getting habitat quantity (land-use maps)
mask.dim <- dim(tmp)
pixs <- mask.dim[1]
last <- mask.dim[2]
hab.mat <- as.matrix(table(factor(tmp[, last] %in% hab.class, levels = c(FALSE, TRUE))))
row.names(hab.mat) <- c("non_habitat", "habitat")
hab.mat <- cbind(hab.mat, 100 * hab.mat/pixs)
hab.mat <- cbind(hab.mat, (hab.mat[,2]* r.area)/100)
colnames(hab.mat) <- c("n.pixs", "prop.EOO", "area.EOO")
if (class(crop) == "RasterBrick") {
# Getting the overal percentage of each transition
transit.mat <- 100 * as.matrix(table(factor(tmp[, 1] %in% hab.class, levels = c(FALSE, TRUE)),
factor(tmp[, last] %in% hab.class, levels = c(FALSE, TRUE)))) / pixs
colnames(transit.mat) <- c("non_habitat_t1", "habitat_t1")
row.names(transit.mat) <- c("non_habitat_t0", "habitat_t0")
hab.mat <- cbind(hab.mat, loss = c(transit.mat[1, 2], transit.mat[2, 1]))
hab.mat <- cbind(hab.mat, recover = c(transit.mat[2, 1], transit.mat[1, 2]))
# % of habitat loss and recovery
hab_loss <- 100 * transit.mat[2, 1] / sum(transit.mat[2, ])
non_hab_loss <- 100 * transit.mat[1, 2] / sum(transit.mat[1, ])
hab_rec <- 100 * transit.mat[1, 2] / sum(transit.mat[, 2])
non_hab_rec <- 100 * transit.mat[2, 1] / sum(transit.mat[, 1])
hab.mat <- cbind(hab.mat, rel.loss = c(non_hab_loss, hab_loss))
hab.mat <- cbind(hab.mat, rel.recover = c(non_hab_rec, hab_rec))
# rate of loss and recovery
if(is.null(years))
years <- dim(hab.map)[3] - 1
non_hab_loss_rate <- non_hab_loss / years
hab_loss_rate <- hab_loss / years
non_hab_rec_rate <- non_hab_rec / years
hab_rec_rate <- hab_rec / years
hab.mat <- cbind(hab.mat, rate.loss = c(non_hab_loss_rate, hab_loss_rate))
hab.mat <- cbind(hab.mat, rate.recover = c(non_hab_rec_rate, hab_rec_rate))
# defining habitat quality at time t+1
transit <- matrix(NA, ncol = 1, nrow = pixs)
transit[tmp[, 1] %in% hab.class &
tmp[, last] %in% hab.class, 1] <- 2
transit[tmp[, 1] %in% hab.class &
!tmp[, last] %in% hab.class, 1] <- -2
transit[!tmp[, 1] %in% hab.class &
tmp[, last] %in% hab.class, 1] <- 1
transit[!tmp[, 1] %in% hab.class &
!tmp[, last] %in% hab.class, 1] <- 0
mod <- stats::lm(base::jitter(transit, factor=0.1) ~ 1)
ci <- round(stats::confint(mod),4)
hab.mat <- cbind(hab.mat,
hab.quality = round(as.numeric(stats::coef(mod)),4),
hab.quality.lo = ci[1],
hab.quality.hi = ci[2])
res <- cbind.data.frame(tax = EOO.poly.name[x], hab.mat[,-1],
stringsAsFactors = FALSE)["habitat",]
# location of decline and recover
if (export_shp) {
#mask <- raster::mask(crop, EOO.poly.crop[x,], cellnumbers = TRUE)
loc.loss <- data.frame(
cell = 1:length(mask[[1]]@data@values),
raster::coordinates(mask[[1]]),
classes = NA_integer_
)
#loc.loss$classes[loc.loss$cell %in% tmp[, 1]] <- transit
loc.loss$classes[!is.na(mask[[1]]@data@values)] <- transit
r <- raster::rasterFromXYZ(loc.loss[!is.na(loc.loss$classes), 2:4],
crs = raster::crs(mask[[1]]))
if (plot) {
par(mar = c(3,3,2,2), las=1, tcl=-0.25, mgp=c(2.5,0.5,0))
raster::plot(r, col = c("red", "grey", "green", "darkgreen"),
main = EOO.poly.name[x])
plot(sf::st_geometry(EOO.poly.crop[x,]), add = TRUE, bg = 0)
}
res <- list(res, r)
}
}
} else {
#Getting summary stats for the variable in the EOO (quantitative maps)
if(class(tmp) %in% c("integer","numeric")) {
vals <- tmp # excluding pixels outside the EOO
pixs <- length(vals)
}
if(class(tmp) %in% c("matrix","data.frame")) {
last <- dim(tmp)[2]
vals <- tmp[, last] # excluding pixels outside the EOO
pixs <- length(vals)
}
sumario <- matrix(unclass(summary(vals)), nrow = 1, ncol = 6,
dimnames = list("", c("Min","1st_Qu","Median","Mean","3rd_Qu","Max")))
# if(class(vals) %in% "integer") {
# mod <- stats::glm(vals ~ 1, family = "poisson" )
# ci <- suppressMessages(round(exp(stats::confint(mod)),4))
# }
# if(class(tmp) %in% "numeric") {
# mod <- stats::lm(vals ~ 1)
# ci <- suppressMessages(round(stats::confint(mod),4))
# }
if(output_raster %in% "summary") {
res <- cbind.data.frame(tax = EOO.poly.name[x],
sumario, # ci,
stringsAsFactors = FALSE)
} else {
#Tabulating pixels
tmp1 <- table(factor(vals, levels = classes))
prop <- matrix(round(100*unclass(tmp1)/pixs, 8), nrow = 1, ncol = length(classes),
dimnames = list("", classes))
if(output_raster %in% "prop.table")
res <- cbind.data.frame(tax = EOO.poly.name[x],
prop,
stringsAsFactors = FALSE)
if(output_raster %in% "area.table")
area <- round((prop * r.area)/100, 3)
res <- cbind.data.frame(tax = EOO.poly.name[x],
area,
stringsAsFactors = FALSE)
}
}
}
res
# if(is.null(result)) result <- res else result <- rbind.data.frame(result, res, stringsAsFactors = FALSE)
# cat(x, "\n")
}
if(parallel) snow::stopCluster(cl)
if(show_progress) close(pb)
result <- do.call(rbind.data.frame, output)
rownames(result) <- NULL
return(result)
}
### HUMAN INFLUENCES ### between 1995 - 2004 (0 low and 65 is high)
hab.map <- raster::raster(paste(path,"//Am_Lat_Global_Human_Influence_Index_v2//hii_s_amer//w001001.adf",sep=""))
## ESA land cover
path <- "E://ownCloud//W_GIS" # Renato's path
### HUMAN INFLUENCES ### between 1995 - 2004 (0 low and 65 is high)
hab.map <- raster::raster(paste(path,"//Am_Lat_Global_Human_Influence_Index_v2//hii_s_amer//w001001.adf",sep=""))
hab.map
hii <- raster::raster(paste(path,"//Am_Lat_Global_Human_Influence_Index_v2//hii_global//hii_neotrop_meth3.tif",sep=""))
hii
### HUMAN INFLUENCES ### between 1995 - 2004 (0 low and 65 is high)
hab.map <- raster::raster(paste(path,"//Am_Lat_Global_Human_Influence_Index_v2//hii_neotrop.tif",sep=""))
EOO.habitat(EOO.poly[1:50,], hab.map, hab.class = NULL, years = NULL,
parallel = TRUE, NbeCores = 4)
## EOO shapefiles
EOO.poly <- readRDS(file = "C:/Users/renato/Documents/raflima/Pos Doc/Manuscritos/Artigo Extincao na MA/data analysis/data/spp.convex.hull.polys_sf_uncropped.rds")
EOO.habitat(EOO.poly[1:50,], hab.map, hab.class = NULL, years = NULL,
parallel = TRUE, NbeCores = 4)
t0 <- Sys.time()
toto2.1 <- EOO.habitat(EOO.poly[1:1000,], hab.map, hab.class = NULL, years = NULL,
parallel = TRUE, NbeCores = 6)
toto2.2 <- EOO.habitat(EOO.poly[1001:2000,], hab.map, hab.class = NULL, years = NULL,
parallel = TRUE, NbeCores = 6)
toto2.3 <- EOO.habitat(EOO.poly[2001:3000,], hab.map, hab.class = NULL, years = NULL,
parallel = TRUE, NbeCores = 6)
toto2.4 <- EOO.habitat(EOO.poly[3001:4000,], hab.map, hab.class = NULL, years = NULL,
parallel = TRUE, NbeCores = 6)
toto2.5 <- EOO.habitat(EOO.poly[4001:length(EOO.poly$geometry),], hab.map, hab.class = NULL, years = NULL,
parallel = TRUE, NbeCores = 6)
toto2 <- cbind.data.frame(toto2.1, toto2.2, toto2.3, toto2.4, toto2.5,
stringsAsFactors = FALSE)
Sys.time() - t0
dim(toto2.1)
dim(toto2.2)
dim(toto2.3)
dim(toto2.4)
dim(toto2.5)
toto2 <- rbind.data.frame(toto2.1, toto2.2, toto2.3, toto2.4, toto2.5,
stringsAsFactors = FALSE)
saveRDS(toto2, "C:/Users/renato/Documents/raflima/Pos Doc/Manuscritos/Artigo Extincao na MA/data analysis/data/EOO_HII.rds")
## ESA land cover
path <- "E://ownCloud//W_GIS" # Renato's path
fcov <- raster::stack(paste(path,"//WO_ESA_Land_Cover_map//v2.0.7//ESACCI-LC-L4-LCCS-Map-300m-P1Y-1992_2015-v2.0.7_AF_50km_buffer_mask.tif",sep=""))
names(fcov) = paste("ESA",c(1992,1995,2000,2005,2010,2015), sep=".")
hab.map <- fcov[[c(1,6)]]
toto = read.csv(paste(path,"//WO_ESA_Land_Cover_map//v2.0.7//ESACCI-LC-Legend.csv",sep=""), as.is=TRUE)
hab.class = toto$NB_LAB[grepl("ForestCover", toto$LegendTreeCoSimp)]
toto.1 <- EOO.habitat(EOO.poly[1:10,], hab.map, hab.class = hab.class, years = 23,
parallel = TRUE, NbeCores = 6)
toto.1