-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.R
38 lines (32 loc) · 1 KB
/
script.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
# Loading packages
library(rgdal)
library(raster)
require(maptools)
require(rgeos)
require(rgdal)
require(sp)
require(spatstat)
require(parallel)
require(snow)
require(doParallel)
require(foreach)
# Reading the raster file and shapefile
map <- raster("climatedata.tif")
distribution <- readOGR("distributiondata.shp")
Dist <- distribution
# Extracting raster data for a polygon
extraction <- extract(map, Dist, small = TRUE, df = TRUE, fun = mean, na.rm = TRUE)
# Extracting raster data for multiple polygons
# Setting up parallel processing
clust <- makeCluster(8) # Number of cores in your PC
registerDoParallel(clust)
speciesvector <- Dist@data$MUNICIP
# Executing parallel extraction for each polygon
results <- foreach(i = 1:length(speciesvector),.packages = c("sp", "raster")) %dopar% {
spec <- speciesvector[i]
polygon <- Dist[Municipalities@data$MUNICIP == spec, ]
extract <- extract(map, polygon, small = TRUE, df = TRUE, fun = mean, na.rm = TRUE)
extract
}
# Ending parallel processing
stopCluster(clust)