-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert2PNG.R
80 lines (73 loc) · 2.94 KB
/
convert2PNG.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
78
79
#convert2PNG.R
#This function creates a KMZ file, georeferenced PNG and a thumb PNG for any given tif file.
#Projection, extent and color scheme have been optimized for BioModelos
#This function can be wrapped in a for loop or apply (sapply or sfClusterApplyLB) to
#convert all tif files on a list. See example below
#
#Args:
# sp.raster = character string with tif filename (including extension)
# in.folder = folder that contains the .tif file to convert
# col.pal = color palette to be used in resulting maps
# add.trans = add trasparent color to palette? Usually you would use TRUE when the tif file
# contains NA, 0 and 1 values, whereas you would use FALSE when the tif only
# has NA and 1 values.
# params = list with elements dem, corresponding to a raster with elevation to be displayed in
# the background of thumbnails, and shape, corresponding to a SpatialPolygonsDataFrame with
# administrative boundaries to be displayed in thumbnail.
#
#Usage:
# in.folder = "~/Modelos/librorojo2"
# col.pal = rgb(193,140,40,maxColorValue=255)
# sp.raster = "Anas_bahamensis_0.tif"
# convert2PNG(sp.raster, in.folder, col.pal, TRUE, params=params)
#
#Example on parallel loop
# require(snowfall)
# sfInit(parallel=T,cpus=16)#Initialize nodes
# sfExportAll() #Export vars to all the nodes
# sfClusterSetupRNG()
# sfClusterApplyLB(sp.list, convert2PNG, in.folder=in.folder,
# col.pal=col.pal, add.trans=TRUE, params=params)
# sfStop()
#
#Author: Jorge Velasquez
#Date created: 05-09-2014
convert2PNG<-function(sp.raster, in.folder, col.pal, add.trans, params){
require(raster)
require(sp)
require(rgdal)
#Create dirs
dir.create(paste0(in.folder,"/PNG"), recursive=T)
dir.create(paste0(in.folder,"/KMZ"), recursive=T)
dir.create(paste0(in.folder,"/thumb"), recursive=T)
#Plots for geovisor
in.raster <- raster(paste0(in.folder, "/", sp.raster))
if(is.na(projection(in.raster))){
projection(in.raster)<-"+proj=longlat +ellps=WGS84 +datum=WGS84"
}
#Remove colors if not enough categories
freq.cat <- freq(in.raster)
ind <- freq.cat[which(freq.cat[, 1]>0)]
col.pal <- col.pal[ind]
tr<-rgb(255, 255, 255, 0, maxColorValue=255)
if(add.trans){
col.pal <- c(tr, col.pal)
}
print(col.pal)
#Create KML
name <- strsplit(sp.raster,"[.]")[[1]][1]
KML(in.raster, filename=paste0(in.folder,"/KMZ/",name,".kmz"),
maxpixels=ncell(in.raster), col=col.pal, overwrite=T, blur=3)
unzip(paste0(in.folder, "/KMZ/", name, ".kmz"), exdir=paste0(in.folder,"/PNG"))
file.remove(paste0(in.folder, "/PNG/", name,".kml"))
#Generate thumbnails
in.raster.co <- in.raster
png(paste0(in.folder, "/thumb/", name, "_thumb.png"),
width=145, height=205, units="px", type="cairo")
op <- par(mar = rep(0, 4), bg=NA)
image(params$dem, axes=F, xlab="", ylab="", col=c(tr, "grey90"))
image(in.raster.co, col=col.pal, axes=FALSE, add=T)
plot(params$shape, add=T, lwd=1, border="grey40",)
dev.off()
unlink(list.files(tempdir()),recursive=T)
}