R/writeAllGDAL.R

Defines functions writeGDALall

# Author: Robert J. Hijmans
# Date : January 2009
# Version 0.9
# Licence GPL v3


.writeGDALall <- function(x, filename, options=NULL, setStatistics=TRUE, ...) {

	stat <- cbind(NA, NA)
	if (nlayers(x) > 1) {
		y <- brick(x, values=FALSE)
		levels(y) <- levels(x)
		x <- getValues(x)
		if (setStatistics) { 
			stat <- t(apply(x, 2, function(z, ...) cbind(mean(z, na.rm=TRUE), stats::sd(z, na.rm=TRUE))))
		}
	} else {
		y <- raster(x)
		levels(y) <- levels(x)
		y@legend@colortable <- colortable(x)
		x <- getValues(x)
		if (setStatistics) { 
			stat <- cbind(mean(x, na.rm=TRUE), stats::sd(x, na.rm=TRUE))
		}
	}
	
	y <- .startGDALwriting(y, filename, options, setStatistics=setStatistics, ...)
	x <- writeValues(y, x, start=1)
	.stopGDALwriting(x, stat)
}
	

Try the raster package in your browser

Any scripts or data that you put into this service are public.

raster documentation built on April 6, 2019, 3:05 a.m.