-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path09.removingBuilt-upAreas_combined.R
71 lines (56 loc) · 1.78 KB
/
09.removingBuilt-upAreas_combined.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
# In this study, our aim is to assess the performance of current PAs and identifying irreplaceable areas
# to establish future PAs, and therefore, meeting global biodiversity conservation targets. It's kind of
# impossible to designate new PAs in built-up areas. To solve the issue, we have removed built-up areas
# from the suitability map for each species.
# Loading required libraries
library(sp)
library(dplyr)
library(sf)
library(raster)
library(dismo)
library(rgeos)
library(stringr)
library(rgdal)
library(parallel)
library(foreach)
library(tidyverse)
# Extracting built-up areas
lc <- raster("data/clim/lc_up.tif")
lc <- lc == 50
lc[lc == 0] <- NA
# List of rasters
tifs <- list.files(path = "outputs/BinaryRasters/", pattern = ".tif", recursive = TRUE, full.names = TRUE)
# Species list
sp_list <- read_csv("outputs/targetRepresentation_taxonomicDetails_up.csv")
sp <- unique(sp_list$species)
###############
# In parallel
# Define variables
n_threads <- 23
# Setup parallel cluster
cl <- makeCluster(n_threads, "PSOCK") # create workers
clusterEvalQ(cl, { # load packages into workers
library(sp)
library(dplyr)
library(sf)
library(raster)
library(dismo)
library(rgeos)
library(stringr)
library(rgdal)
library(parallel)
library(foreach)
})
clusterExport(cl, c("tifs", "lc"))
# Main processing
result <- try(parLapply(cl, sp, function(i) {
speciesname <- gsub("Binary", "", i)
raster_file <- tifs[stringr::str_detect(tifs, as.character(i))]
sdm.hull <- raster(raster_file)
new_raster <- sdm.hull - lc
new_raster[new_raster == -1] <- NA
writeRaster(new_raster, paste0("outputs/BinaryRasters_Built-upAreasRemoved/",
speciesname, ".tif"), NAflag=-9999, overwrite = TRUE)
}), silent = TRUE)
# Stop cluster
cl <- stopCluster(cl)