[GRASS-stats] Raster maps from GRASS to R and back to GRASS?

Nikos Alexandris nikos.alexandris at felis.uni-freiburg.de
Sat Apr 11 19:46:31 EDT 2009


Hi all!


Dylan: 
> > Does this help:
> > http://casoilresource.lawr.ucdavis.edu/drupal/node/664

I need a confirmation that I am doing the NA-work-around the _right_
way. I get several NULL's in the raster maps I write back in GRASS in
locations (=pixels) were the input raster map(s) have a NON-NULL value.


# launch grass; launch R from within grass
# load libraries and projection information
library(spgrass6); G <- gmeta6()

# load data
raw <- readRAST6 (c('a', 'b', 'c', 'd', 'e', 'f'))

# create vector pointing to values
values <- which(!is.na(raw at data$a) & !is.na(raw at data$b) & !
is.na(raw at data$c) & !is.na(raw at data$d) & !is.na(raw at data$e) & !
is.na(raw at data$f))

# get data
data <- raw at data[values, ]

# check structure
str(data)


# perform pca with prcomp(): SVD - SCALED
prcomp.data <- prcomp(data, scale=TRUE)
prcomp.data$rotation
summary(prcomp.data)


# add new columns to the original sp object #
raw at data$pc1 <- NA
raw at data$pc2 <- NA
raw at data$pc3 <- NA
raw at data$pc4 <- NA
raw at data$pc5 <- NA
raw at data$pc6 <- NA
str(raw)

# fill-in the PCs
raw at data$pc1[values] <- prcomp.data$x[,"PC1"]
raw at data$pc1[values] <- prcomp.data$x[,"PC1"]
raw at data$pc1[values] <- prcomp.data$x[,"PC1"]
raw at data$pc1[values] <- prcomp.data$x[,"PC1"]
raw at data$pc1[values] <- prcomp.data$x[,"PC1"]
mod_prepost_b267.raw at data$pc2[mod_prepost_b267.values] <-
prcomp.modis0607.nonas$x[,"PC2"]
mod_prepost_b267.raw at data$pc3[mod_prepost_b267.values] <-
prcomp.modis0607.nonas$x[,"PC3"]
mod_prepost_b267.raw at data$pc4[mod_prepost_b267.values] <-
prcomp.modis0607.nonas$x[,"PC4"]
mod_prepost_b267.raw at data$pc5[mod_prepost_b267.values] <-
prcomp.modis0607.nonas$x[,"PC5"]
mod_prepost_b267.raw at data$pc6[mod_prepost_b267.values] <-
prcomp.modis0607.nonas$x[,"PC6"]
str(mod_prepost_b267.raw)

# write grass raster maps
writeRAST6(mod_prepost_b267.raw, zcol=7, vname="prcomp1_0607",
overwrite=TRUE)
writeRAST6(mod_prepost_b267.raw, zcol=7, vname="prcomp2_0607",
overwrite=TRUE)
writeRAST6(mod_prepost_b267.raw, zcol=7, vname="prcomp3_0607",
overwrite=TRUE)
writeRAST6(mod_prepost_b267.raw, zcol=7, vname="prcomp4_0607",
overwrite=TRUE)
writeRAST6(mod_prepost_b267.raw, zcol=7, vname="prcomp5_0607",
overwrite=TRUE)
writeRAST6(mod_prepost_b267.raw, zcol=7, vname="prcomp6_0607",
overwrite=TRUE)



More information about the grass-stats mailing list