-
Notifications
You must be signed in to change notification settings - Fork 0
/
raster_utils.R
29 lines (24 loc) · 860 Bytes
/
raster_utils.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
# *** Author: Odilon Vieira da Fonseca ***
# *** Date: 31/March/2020 ***
library(raster)
library(dplyr)
read_stack = function(directory,pattern="tif"){
#shortcut to read stacks
list.files(directory,pattern,full.names = T) %>%
stack()
}
write_stack = function(s,fn,type="Gtiff",suffix="numbers"){
#shortcut to write stacks
writeRaster(s,fn,type,bylayer=T,suffix=suffix)
}
split_raster <- function(r, row, col, na.rm=T){
#split raster in sub-raster with equal dimensions (row, col)
#aggregate cells by dimension (row, col)
aggregate(r,c(col,row), na.rm=na.rm) %>%
#create polygons with the aggregations
as('SpatialPolygons') %>%
#use polygons to get raster cuts
do(
lapply(seq_along(.), function(i) crop(r,.[i]))
)
}