[Raster-commits] r344 - pkg/raster/R
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Thu Mar 12 07:24:28 CET 2009
Author: rhijmans
Date: 2009-03-12 07:24:27 +0100 (Thu, 12 Mar 2009)
New Revision: 344
Added:
pkg/raster/R/stackAdd.R
Removed:
pkg/raster/R/addToStack.R
Modified:
pkg/raster/R/map.R
pkg/raster/R/raster.create.R
pkg/raster/R/stackFile.R
Log:
Deleted: pkg/raster/R/addToStack.R
===================================================================
--- pkg/raster/R/addToStack.R 2009-03-12 02:16:55 UTC (rev 343)
+++ pkg/raster/R/addToStack.R 2009-03-12 06:24:27 UTC (rev 344)
@@ -1,132 +0,0 @@
-# Author: Robert J. Hijmans, r.hijmans at gmail.com
-# International Rice Research Institute
-# Date : June 2008
-# Version 0.8
-# Licence GPL v3
-
-
-
-addFiles <- function(rstack, rasterfiles, bands=rep(1, length(rasterfiles))) {
- if (length(bands) == 1) {
- bands=rep(bands, length(rasterfiles))
- }
- rasters <- list()
- for (i in 1:length(rasterfiles)) {
- if (bands[[i]] < 1) {
- r <- rasterFromFile(rasterfiles[[i]], band=1)
- rasters <- c(rasters, r)
- if (nbands(r) > 1) {
- for (j in 2:nbands(r)) {
- r <- rasterFromFile(rasterfiles[[i]], band=j)
- rasters <- c(rasters, r)
- }
- }
- } else {
- rasters <- c(rasters, rasterFromFile(rasterfiles[[i]], FALSE, band=bands[[i]]))
- }
- }
- rstack <- addRasters(rstack, rasters)
- return(rstack)
-}
-
-
-
-addRasters <- function(rstack, rasters) {
-#rasters is a list of raster objects
- if (class(rstack) != "RasterStack") {
- stop("rstack should be a RasterStack object")
- }
- if (class(rasters) == 'RasterLayer') {
- rasters <- list(rasters)
- }
-
- for (i in 1:length(rasters)) {
- raster <- rasters[[i]]
-
- if (dataContent(raster) != 'all' & dataSource(raster) == 'ram') {
- stop("Cannot add a memory based RasterLayer object without values to a Rasterstack object")
- }
-
- if (nlayers(rstack) == 0) {
- if (class(raster) == 'RasterStack') {
- rstack <- raster
- } else {
- rstack <- setRowCol(rstack, nrow(raster), ncol(raster))
- rstack <- setExtent(rstack, raster, snap=FALSE)
- rstack <- setProjection(rstack, projection(raster))
-
- nl <- rstack at data@nlayers + nlayers(raster)
- rstack at data@nlayers <- as.integer(nl)
- rstack at layers[nl] <- raster
- rstack at data@min[nl] <- raster at data@min
- rstack at data@max[nl] <- raster at data@max
-
- if (trim(raster at file@shortname) != "") {
- cname <- trim(raster at file@shortname)
- } else {
- cname <- "layer1"
- }
- rstack at data@colnames[1] <- cname
- if (dataContent(raster) == 'all') {
- rstack at data@values <- as.matrix(values(raster))
- rstack at data@content <- 'all'
- raster <- clearValues(raster)
- } else {
- if (dataSource(raster) == 'ram' & dataContent(raster) != "all") {
- stop("Cannot add a memory based RasterLayer object without values to a Rasterstack object")
- }
- }
-
- }
-
- } else {
-
- if (class(raster) == 'RasterStack') {
- rasterlist <- unstack(raster)
- } else {
- rasterlist <- list(raster)
- }
-
- for (k in 1:length(rasterlist)) {
- nl <- as.integer( rstack at data@nlayers + nlayers(raster) )
- rstack at data@nlayers <- nl
- rstack at layers[nl] <- raster
- rstack at data@min[nl] <- raster at data@min
- rstack at data@max[nl] <- raster at data@max
-
- raster <- rasterlist[[k]]
- if (!compare(c(rstack, raster))) {
- stop(paste("could not add raster:", filename(raster)))
- }
- count <- 1
- cname <- trim(raster at file@shortname)
- if (cname == "") {
- cname <- paste("layer", nl, sep="")
- }
- cn <- cname
- for (j in 1:(nl-1)) {
- if ( cn == rstack at data@colnames[j] ) {
- count <- count + 1
- cn <- paste(cname, "_", count, sep="")
- }
- }
- rstack at data@colnames[nl] <- cn
- if (dataContent(rstack)=='all') {
- if (dataContent(raster) != 'all') {
- raster <- readAll(raster)
- }
- rstack at data@values <- cbind(rstack at data@values, values(raster))
- raster <- clearValues(raster)
- } else {
- if (dataSource(raster)=='disk') {
- raster <- clearValues(raster)
- }
- }
-
- }
- }
- }
- return(rstack)
-}
-
-
Modified: pkg/raster/R/map.R
===================================================================
--- pkg/raster/R/map.R 2009-03-12 02:16:55 UTC (rev 343)
+++ pkg/raster/R/map.R 2009-03-12 06:24:27 UTC (rev 344)
@@ -18,7 +18,7 @@
if (i != index) { stop("index should be >= 1 and <=", nlayers(object), " =nlayers(object)") }
raster2 <- asRasterLayer(object, i)
if (dataContent(object) == 'all') {
- raster2 <- setValues(raster2, values(object)[i,])
+ raster2 <- setValues(raster2, values(object)[,i])
}
object <- raster2
}
Modified: pkg/raster/R/raster.create.R
===================================================================
--- pkg/raster/R/raster.create.R 2009-03-12 02:16:55 UTC (rev 343)
+++ pkg/raster/R/raster.create.R 2009-03-12 06:24:27 UTC (rev 344)
@@ -29,15 +29,17 @@
return(rs)
}
-#if (!isGeneric("values")) {
-# setGeneric("values", function(object, ...)
-# standardGeneric("values"))
+#if (!isGeneric("raster")) {
+# setGeneric("raster", function(x, ...)
+# standardGeneric("raster"))
#}
-#setMethod('values', signature(object='Raster'),
+#setMethod('raster', signature(x='Raster'),
+# function(x, ...) {
+# return(setRaster(x))
+#}
-
rasterFromBbox <- function(bndbox, nrows=10, ncols=10) {
crs <- newCRS('NA')
try(crs <- projection(bndbox, asText=F), silent = T)
@@ -54,7 +56,7 @@
rasterFromFile <- function(filename, values=FALSE, band=1) {
fileext <- toupper(fileExtension(filename))
if ( fileext == ".GRD" | fileext == ".GRI" ) {
- raster <- .rasterFromFile(filename, band)
+ raster <- .rasterFromRasterFile(filename, band)
} else {
raster <- .rasterFromGDAL(filename, band)
}
@@ -119,7 +121,7 @@
-.rasterFromFile <- function(filename, band=1) {
+.rasterFromRasterFile <- function(filename, band=1) {
if (!file.exists( .setFileExtensionValues(filename)) ){
warning("no '.gri' file. Assuming this is a Surfer file")
return(.readSurfer6(filename))
Added: pkg/raster/R/stackAdd.R
===================================================================
--- pkg/raster/R/stackAdd.R (rev 0)
+++ pkg/raster/R/stackAdd.R 2009-03-12 06:24:27 UTC (rev 344)
@@ -0,0 +1,132 @@
+# Author: Robert J. Hijmans, r.hijmans at gmail.com
+# International Rice Research Institute
+# Date : June 2008
+# Version 0.8
+# Licence GPL v3
+
+
+
+addFiles <- function(rstack, rasterfiles, bands=rep(1, length(rasterfiles))) {
+ if (length(bands) == 1) {
+ bands=rep(bands, length(rasterfiles))
+ }
+ rasters <- list()
+ for (i in 1:length(rasterfiles)) {
+ if (bands[[i]] < 1) {
+ r <- rasterFromFile(rasterfiles[[i]], band=1)
+ rasters <- c(rasters, r)
+ if (nbands(r) > 1) {
+ for (j in 2:nbands(r)) {
+ r <- rasterFromFile(rasterfiles[[i]], band=j)
+ rasters <- c(rasters, r)
+ }
+ }
+ } else {
+ rasters <- c(rasters, rasterFromFile(rasterfiles[[i]], FALSE, band=bands[[i]]))
+ }
+ }
+ rstack <- addRasters(rstack, rasters)
+ return(rstack)
+}
+
+
+
+addRasters <- function(rstack, rasters) {
+#rasters is a list of raster objects
+ if (class(rstack) != "RasterStack") {
+ stop("rstack should be a RasterStack object")
+ }
+ if (class(rasters) == 'RasterLayer') {
+ rasters <- list(rasters)
+ }
+
+ for (i in 1:length(rasters)) {
+ raster <- rasters[[i]]
+
+ if (dataContent(raster) != 'all' & dataSource(raster) == 'ram') {
+ stop("Cannot add a memory based RasterLayer object without values to a Rasterstack object")
+ }
+
+ if (nlayers(rstack) == 0) {
+ if (class(raster) == 'RasterStack') {
+ rstack <- raster
+ } else {
+ rstack <- setRowCol(rstack, nrow(raster), ncol(raster))
+ rstack <- setExtent(rstack, raster, snap=FALSE)
+ rstack <- setProjection(rstack, projection(raster))
+
+ nl <- rstack at data@nlayers + nlayers(raster)
+ rstack at data@nlayers <- as.integer(nl)
+ rstack at layers[nl] <- raster
+ rstack at data@min[nl] <- raster at data@min
+ rstack at data@max[nl] <- raster at data@max
+
+ if (trim(raster at file@shortname) != "") {
+ cname <- trim(raster at file@shortname)
+ } else {
+ cname <- "layer1"
+ }
+ rstack at data@colnames[1] <- cname
+ if (dataContent(raster) == 'all') {
+ rstack at data@values <- as.matrix(values(raster))
+ rstack at data@content <- 'all'
+ raster <- clearValues(raster)
+ } else {
+ if (dataSource(raster) == 'ram' & dataContent(raster) != "all") {
+ stop("Cannot add a memory based RasterLayer object without values to a Rasterstack object")
+ }
+ }
+
+ }
+
+ } else {
+
+ if (class(raster) == 'RasterStack') {
+ rasterlist <- unstack(raster)
+ } else {
+ rasterlist <- list(raster)
+ }
+
+ for (k in 1:length(rasterlist)) {
+ nl <- as.integer( rstack at data@nlayers + nlayers(raster) )
+ rstack at data@nlayers <- nl
+ rstack at layers[nl] <- raster
+ rstack at data@min[nl] <- raster at data@min
+ rstack at data@max[nl] <- raster at data@max
+
+ raster <- rasterlist[[k]]
+ if (!compare(c(rstack, raster))) {
+ stop(paste("could not add raster:", filename(raster)))
+ }
+ count <- 1
+ cname <- trim(raster at file@shortname)
+ if (cname == "") {
+ cname <- paste("layer", nl, sep="")
+ }
+ cn <- cname
+ for (j in 1:(nl-1)) {
+ if ( cn == rstack at data@colnames[j] ) {
+ count <- count + 1
+ cn <- paste(cname, "_", count, sep="")
+ }
+ }
+ rstack at data@colnames[nl] <- cn
+ if (dataContent(rstack)=='all') {
+ if (dataContent(raster) != 'all') {
+ raster <- readAll(raster)
+ }
+ rstack at data@values <- cbind(rstack at data@values, values(raster))
+ raster <- clearValues(raster)
+ } else {
+ if (dataSource(raster)=='disk') {
+ raster <- clearValues(raster)
+ }
+ }
+
+ }
+ }
+ }
+ return(rstack)
+}
+
+
Modified: pkg/raster/R/stackFile.R
===================================================================
--- pkg/raster/R/stackFile.R 2009-03-12 02:16:55 UTC (rev 343)
+++ pkg/raster/R/stackFile.R 2009-03-12 06:24:27 UTC (rev 344)
@@ -9,9 +9,9 @@
stackOpen <- function(stackfile) {
st <- read.table(stackfile, as.is=FALSE, strip.white=TRUE)
if (dim(st)[2] > 1) {
- rst <- stackFromFiles(st[,1], st[,2])
+ rst <- stackFromFiles(as.vector(st[,1]), as.vector(st[,2]))
} else {
- rst <- stackFromFiles(st[,1])
+ rst <- stackFromFiles(as.vector(st[,1]))
}
rst <- setFilename(rst, stackfile)
return(rst)
More information about the Raster-commits
mailing list