r - Function that don't create SpatialGridDataFrame object -


i try create function transform digital number of satellite imagery in radiance, don't know why final object numeric , not spatialgriddataframe object, if specified in function class(results) == "spatialgriddataframe". code is:

packages

require(raster) require(sp) 

rasterlayer simulated

r <- raster(nrows=10, ncols=10) r <- setvalues(r, 1:ncell(r)) plot(r)  band2<- as(r, 'spatialgriddataframe')  ### convert in spatialgriddataframe 

function dn radiance

radconvl<-function(x, band = 2) {      lmax <- switch(as.character(band),                      "2" = 120.64,                     "3" = 151.31,                     "4" = 157.57,                     "5" = 69.03,                     na)       if (is.na(lmax)) stop("invalid band")       lmin = 0      qmax = 127      x <- as.vector(as.matrix(x))      results <- x       x <- lmin + ((lmax-lmin)*x)/qmax      if (class(results) == "spatialgriddataframe")          results@data[, 1] <- x      else if (is.data.frame(x))          results <- data.frame(matrix(x, nrow = nrow(results),              ncol = ncol(results)))      else results <- x      print(paste(band, lmax))      print(results)      results } 
--

try function

teste2<-radconvl(band2, band = 2) str(test2)## numeric!!!! why??? 

could me?

thanks,

alexandre

i'll show how can make work:

radconvl <- function(x, band = 2) {      lmax <- switch(band,                      "2" = 120.64,                     "3" = 151.31,                     "4" = 157.57,                     "5" = 69.03,                     na)       if (is.na(lmax)) stop("invalid band")      lmin = 0      qmax = 127      lmin + ((lmax-lmin)*x)/qmax }  library(raster) b <- brick(system.file("external/rlogo.grd", package="raster")) test <- radconvl(b[[2]], band = 2) 

test rasterlayer, if need spatialgriddataframe (why?) use:

sptest <- as(test, 'spatialgriddataframe') 

this not direct answer of question, difficult understand why doing of things in function. example, do:

 x <- as.vector(as.matrix(x))  results <- x  x <- lmin + ((lmax-lmin)*x)/qmax 

so results , x vector, do:

if (class(results) == "spatialgriddataframe") #(...) else if (is.data.frame(x)) #(...) else results <- x 

how relevant, when know x vector (and not spatialgriddataframe or data.frame)? going make results equal x. obvious result numeric.

you state do: class(results) == "spatialgriddataframe", not that. either way, not work (that akin putting post-it "car" on bicycle; won't magically give 4 wheels , engine of sudden).

if want speed things loading values memory, can do:

radconvl <- function(x, band = 2) {      lmax <- switch(band,                      "2" = 120.64,                     "3" = 151.31,                     "4" = 157.57,                     "5" = 69.03,                     na)       if (is.na(lmax)) stop("invalid band")      lmin = 0      qmax = 127      setvalues(x, lmin + ((lmax-lmin)*values(x))/qmax) } 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -