-
Notifications
You must be signed in to change notification settings - Fork 0
/
reproject_gee_rasters.R
executable file
·77 lines (65 loc) · 1.99 KB
/
reproject_gee_rasters.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
#!/usr/bin/Rscript
argv <- commandArgs(trailingOnly=T)
if(length(argv)<1) stop("requires one argument: ",
"[full path to gee zipfile with raster to reproject] or a directory of ",
"tifs specified with the -d argument")
require(snow)
require(raster)
beginCluster(n=2)
rasterOptions(tmpdir="/home/ktaylora/r_raster_tmp")
#
# local function declarations
#
reproject_raster <- function(r=NULL,template=NULL){
if(!inherits(r,"Raster")){
r <- raster::raster(r)
}
# fixed raster template object we are projecting "to"
if(is.null(template)){
template <- raster("/global_workspace/ring_necked_pheasant_imbcr_models/
raster/2016_crp_107x107.tif")
}
cat(" -- reprojecting:",names(r),"\n")
reprojected <- raster::projectRaster(r, crs=CRS(projection(template)),progress='text')
final <- raster::projectRaster(reprojected,to=template, progress='text')
return(final)
}
#
# MAIN
#
using_directory <- which(grepl(tolower(argv),pattern="-d"))
if(sum(using_directory)>0){
rasters <- list.files(argv[using_directory+1], pattern="tif$", fullnames=T)
if(length(rasters)<1){
stop("-d directory argument didn't contain any .tif images")
}
# if there wasn't a directory specification we will treat this as one image
} else {
if(!file.exists(argv)){
stop("file",argv,"doesn't exist")
} else {
if(grepl(argv,pattern="zip$")){
if(dir.exists("unpack")){
unlink("unpack")
}
dir.create("unpack")
utils::unzip(argv, exdir="unpack", overwrite=T)
rasters <- list.files("unpack", pattern="tif$", full.names=T)
} else { # assume it's a raster : AFNP
rasters <- argv
}
}
}
for(r in rasters){
target <- <- gsub(r, pattern="tif$", replacement="reprojected.tif")
if(file.exists(target)){
cat("target file",target,"already exists... skipping...\n")
} else {
r <- reproject_raster(r)
writeRaster(r, target, overwrite=T, progress='text')
rm(r);
raster::removeTmpFiles();
gc()
}
}
endCluster()