[Raster-commits] r367 - in pkg/raster: R man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Tue Mar 17 12:54:56 CET 2009
Author: rhijmans
Date: 2009-03-17 12:54:55 +0100 (Tue, 17 Mar 2009)
New Revision: 367
Added:
pkg/raster/R/gdalFormats.R
Modified:
pkg/raster/R/Merge.R
pkg/raster/R/aggregate.R
pkg/raster/R/calc.R
pkg/raster/R/calcStack.R
pkg/raster/R/cover.R
pkg/raster/R/crop.R
pkg/raster/R/disaggregate.R
pkg/raster/R/expand.R
pkg/raster/R/init.R
pkg/raster/R/linesToRaster.R
pkg/raster/R/neighborhood.R
pkg/raster/R/overlayList.R
pkg/raster/R/pointsToRaster.R
pkg/raster/R/polygonToRaster.R
pkg/raster/R/project.R
pkg/raster/R/reclass.R
pkg/raster/R/resample.R
pkg/raster/R/showTrack.R
pkg/raster/R/write.R
pkg/raster/R/writeGDAL.R
pkg/raster/R/writeRaster.R
pkg/raster/R/writeRasterRow.R
pkg/raster/man/init.Rd
pkg/raster/man/writeRaster.Rd
Log:
Modified: pkg/raster/R/Merge.R
===================================================================
--- pkg/raster/R/Merge.R 2009-03-17 07:02:01 UTC (rev 366)
+++ pkg/raster/R/Merge.R 2009-03-17 11:54:55 UTC (rev 367)
@@ -53,10 +53,9 @@
outraster <- setFilename(outraster, filename )
if (options('verbose')[[1]]) { cat('writing raster to:', filename(raster)) }
}
- starttime <- proc.time()
+ starttime <- proc.time()
-
for (r in 1:nrow(outraster)) {
rd <- as.vector(matrix(NA, nrow=1, ncol=ncol(outraster)))
for (i in length(rasters):1) { #reverse order so that the first raster covers the second etc.
@@ -84,7 +83,7 @@
v <- c(v, rd)
}
- if (r %in% track) { .showTrack(r, track, starttime) }
+ if (r %in% track) { .showTrack(r, outraster at nrows, track, starttime) }
}
if (filename(outraster) == '') {
Modified: pkg/raster/R/aggregate.R
===================================================================
--- pkg/raster/R/aggregate.R 2009-03-17 07:02:01 UTC (rev 366)
+++ pkg/raster/R/aggregate.R 2009-03-17 11:54:55 UTC (rev 367)
@@ -97,7 +97,7 @@
outRaster <- writeRaster(outRaster, overwrite=overwrite, filetype=filetype)
}
- if (r %in% track) { .showTrack(r, track, starttime) }
+ if (r %in% track) { .showTrack(r, outraster at nrows, track, starttime) }
}
if (filename(outRaster) == "") {
Modified: pkg/raster/R/calc.R
===================================================================
--- pkg/raster/R/calc.R 2009-03-17 07:02:01 UTC (rev 366)
+++ pkg/raster/R/calc.R 2009-03-17 11:54:55 UTC (rev 367)
@@ -41,6 +41,7 @@
}
v <- vector(length=0)
starttime <- proc.time()
+
for (r in 1:nrow(x)) {
x <- readRow(x, r)
if (filename(outraster)=="") {
@@ -50,7 +51,7 @@
outraster <- writeRaster(outraster, overwrite=overwrite, filetype=filetype)
}
- if (r %in% track) { .showTrack(r, track, starttime) }
+ if (r %in% track) { .showTrack(r, raster at nrows, track, starttime) }
}
if (filename(outraster) == "") {
Modified: pkg/raster/R/calcStack.R
===================================================================
--- pkg/raster/R/calcStack.R 2009-03-17 07:02:01 UTC (rev 366)
+++ pkg/raster/R/calcStack.R 2009-03-17 11:54:55 UTC (rev 367)
@@ -39,7 +39,7 @@
outraster <- writeRaster(outraster, filetype=filetype, overwrite=overwrite)
}
- if (r %in% track) { .showTrack(r, track, starttime) }
+ if (r %in% track) { .showTrack(r, outraster at nrows, track, starttime) }
}
if (filename(outraster) == "") {
Modified: pkg/raster/R/cover.R
===================================================================
--- pkg/raster/R/cover.R 2009-03-17 07:02:01 UTC (rev 366)
+++ pkg/raster/R/cover.R 2009-03-17 11:54:55 UTC (rev 367)
@@ -41,7 +41,7 @@
if (options('verbose')[[1]]) { cat('writing raster to:', filename(raster)) }
}
starttime <- proc.time()
-
+
v <- vector(length=0)
for (r in 1:nrow(outRaster)) {
x <- readRow(x, r)
@@ -55,7 +55,7 @@
outRaster <- writeRaster(outRaster, filetype=filetype, overwrite=overwrite)
}
- if (r %in% track) { .showTrack(r, track, starttime) }
+ if (r %in% track) { .showTrack(r, outraster at nrows, track, starttime) }
}
if (filename(outRaster) == "") {
Modified: pkg/raster/R/crop.R
===================================================================
--- pkg/raster/R/crop.R 2009-03-17 07:02:01 UTC (rev 366)
+++ pkg/raster/R/crop.R 2009-03-17 11:54:55 UTC (rev 367)
@@ -25,10 +25,11 @@
outraster <- setMinMax(outraster)
if (filename(outraster) != "" ) {
outraster <- writeRaster(outraster, filetype=filetype, overwrite=overwrite)
- }
+ }
} else if ( dataSource(raster) == 'disk') {
starttime <- proc.time()
+
first_col <- colFromX(raster, xmin(outraster) + 0.5 * xres(outraster))
first_row <- rowFromY(raster, ymax(outraster) - 0.5 * yres(outraster))
last_row <- first_row + nrow(outraster) - 1
@@ -44,7 +45,7 @@
}
rownr <- rownr + 1
- if (r %in% track) { .showTrack(r, track, starttime) }
+ if (r %in% track) { .showTrack(r, outraster at nrows, track, starttime) }
}
if (filename(outraster) == '') {
outraster <- setValues(outraster, v)
Modified: pkg/raster/R/disaggregate.R
===================================================================
--- pkg/raster/R/disaggregate.R 2009-03-17 07:02:01 UTC (rev 366)
+++ pkg/raster/R/disaggregate.R 2009-03-17 11:54:55 UTC (rev 367)
@@ -36,7 +36,7 @@
} else if ( dataSource(raster) == 'disk') {
starttime <- proc.time()
-
+
v <- vector(length=0)
cols <- rep(1:ncol(raster), each=xfact)
for (r in 1:nrow(raster)) {
@@ -55,7 +55,7 @@
outraster <- setValues(outraster, v)
}
- if (r %in% track) { .showTrack(r, track, starttime) }
+ if (r %in% track) { .showTrack(r, outraster at nrows, track, starttime) }
}
return(outraster)
}
Modified: pkg/raster/R/expand.R
===================================================================
--- pkg/raster/R/expand.R 2009-03-17 07:02:01 UTC (rev 366)
+++ pkg/raster/R/expand.R 2009-03-17 11:54:55 UTC (rev 367)
@@ -50,7 +50,7 @@
if (options('verbose')[[1]]) { cat('writing raster to:', filename(raster)) }
}
starttime <- proc.time()
-
+
v <- vector(length=0)
d <- vector(length=ncol(outraster))
for (r in 1:nrow(raster)) {
@@ -68,7 +68,7 @@
v <- c(v, d)
}
- if (r %in% track) { .showTrack(r, track, starttime) }
+ if (r %in% track) { .showTrack(r, outraster at nrows, track, starttime) }
}
if (filename(outraster) == '') {
Added: pkg/raster/R/gdalFormats.R
===================================================================
--- pkg/raster/R/gdalFormats.R (rev 0)
+++ pkg/raster/R/gdalFormats.R 2009-03-17 11:54:55 UTC (rev 367)
@@ -0,0 +1,45 @@
+# Author: Robert J. Hijmans, r.hijmans at gmail.com
+# International Rice Research Institute
+# Date : June 2008
+# Version 0.8
+# Licence GPL v3
+
+
+.isSupportedGDALFormat <- function(dname) {
+ gd <- gdalDrivers()
+ gd <- as.matrix(subset(gd, gd[,3] == T))
+ res <- dname %in% gd
+ if (!res) { stop(paste(dname, "is not a supported file format. See writeFormats()" ) ) }
+ return(res)
+}
+
+
+#.GDALDataTypes <- c('Unknown', 'Byte', 'UInt16', 'Int16', 'UInt32','Int32', 'Float32', 'Float64', '
+# what are these? CInt16', 'CInt32', 'CFloat32', 'CFloat64')
+# this needs to get fancier; depending on object and the abilties of the drivers
+.getGdalDType <- function(dtype) {
+ if (!(dtype %in% c('LOG1S', 'INT1S', 'INT2S', 'INT4S', 'INT8S', 'INT1U', 'INT2U', 'INT4U', 'INT8U', 'FLT4S', 'FLT8S'))) {
+ stop('not a valid data type')
+ }
+ type <- .shortDataType(dtype)
+ size <- dataSize(dtype) * 8
+ if (type == 'LOG') {
+ return('Byte')
+ }
+ if (type == 'INT') {
+ type <- 'Int'
+ if (size == 64) {
+ size <- 32
+ warning('8 byte integer values not supported by rgdal, changed to 4 byte integer values')
+ }
+ if (size == 8) {
+ return('Byte')
+ } else if (dataSigned(dtype)) {
+ type <- paste('U', type, sep='')
+ }
+ } else {
+ type <- 'Float'
+ }
+ return(paste(type, size, sep=''))
+}
+
Modified: pkg/raster/R/init.R
===================================================================
--- pkg/raster/R/init.R 2009-03-17 07:02:01 UTC (rev 366)
+++ pkg/raster/R/init.R 2009-03-17 11:54:55 UTC (rev 367)
@@ -19,6 +19,7 @@
} else if (dataSource(raster) == 'disk') {
starttime <- proc.time()
+
n <- length(ncol(raster))
v <- vector(length=0)
@@ -30,7 +31,7 @@
outraster <- writeRaster(outraster, filetype=filetype, overwrite=overwrite)
}
- if (r %in% track) { .showTrack(r, track, starttime) }
+ if (r %in% track) { .showTrack(r, outraster at nrows, track, starttime) }
}
if (filename(outraster) == "") {
Modified: pkg/raster/R/linesToRaster.R
===================================================================
--- pkg/raster/R/linesToRaster.R 2009-03-17 07:02:01 UTC (rev 366)
+++ pkg/raster/R/linesToRaster.R 2009-03-17 11:54:55 UTC (rev 367)
@@ -147,8 +147,10 @@
v <- vector(length=0)
rxmn <- xmin(raster) + 0.1 * xres(raster)
rxmx <- xmax(raster) - 0.1 * xres(raster)
+
+ starttime <- proc.time()
+
for (r in 1:nrow(raster)) {
- starttime <- proc.time()
rv <- rep(NA, ncol(raster))
ly <- yFromRow(raster, r)
@@ -196,7 +198,7 @@
raster <- writeRaster(raster, filetype=filetype)
}
- if (r %in% track) { .showTrack(r, track, starttime) }
+ if (r %in% track) { .showTrack(r, raster at nrows, track, starttime) }
}
if (filename == "") {
Modified: pkg/raster/R/neighborhood.R
===================================================================
--- pkg/raster/R/neighborhood.R 2009-03-17 07:02:01 UTC (rev 366)
+++ pkg/raster/R/neighborhood.R 2009-03-17 11:54:55 UTC (rev 367)
@@ -57,9 +57,9 @@
rr <- 1
v <- vector(length=0)
- for (r in 1:nrow(raster)) {
- starttime <- proc.time()
+ starttime <- proc.time()
+ for (r in 1:nrow(raster)) {
if (dataContent(raster)=='all') {
rowdata <- valuesRow(raster, r)
} else {
@@ -77,7 +77,7 @@
rr <- rr + 1
}
- if (r %in% track) { .showTrack(r, track, starttime) }
+ if (r %in% track) { .showTrack(r, raster at nrows, track, starttime) }
}
Modified: pkg/raster/R/overlayList.R
===================================================================
--- pkg/raster/R/overlayList.R 2009-03-17 07:02:01 UTC (rev 366)
+++ pkg/raster/R/overlayList.R 2009-03-17 11:54:55 UTC (rev 367)
@@ -69,7 +69,7 @@
outraster <- writeRaster(outraster, filetype=filetype, overwrite=overwrite)
}
- if (r %in% track) { .showTrack(r, track, starttime) }
+ if (r %in% track) { .showTrack(r, raster at nrows, track, starttime) }
}
if (filename(outraster) == "") {
Modified: pkg/raster/R/pointsToRaster.R
===================================================================
--- pkg/raster/R/pointsToRaster.R 2009-03-17 07:02:01 UTC (rev 366)
+++ pkg/raster/R/pointsToRaster.R 2009-03-17 11:54:55 UTC (rev 367)
@@ -29,7 +29,7 @@
v <- vector(length=0)
starttime <- proc.time()
-
+
for (r in 1:rs at nrows) {
d <- dna
if (r %in% urows) {
@@ -48,7 +48,7 @@
v <- c(v, d)
}
- if (r %in% track) { .showTrack(r, track, starttime) }
+ if (r %in% track) { .showTrack(r, rs at nrows, track, starttime) }
}
if (filename == "") {
Modified: pkg/raster/R/polygonToRaster.R
===================================================================
--- pkg/raster/R/polygonToRaster.R 2009-03-17 07:02:01 UTC (rev 366)
+++ pkg/raster/R/polygonToRaster.R 2009-03-17 11:54:55 UTC (rev 367)
@@ -62,7 +62,6 @@
polygonsToRaster <- function(spPolys, raster, field=0, updateRaster=FALSE, updateValue="NA", filename="", overwrite=FALSE, filetype='raster', datatype='FLT4S', track=-1) {
filename <- trim(filename)
- starttime <- proc.time()
if (updateRaster) {
oldraster <- raster
@@ -73,6 +72,8 @@
raster <- raster(raster, filename)
raster <- setDatatype(raster, datatype)
+ starttime <- proc.time()
+
# check if bbox of raster and spPolys overlap
spbb <- bbox(spPolys)
rsbb <- bbox(raster)
@@ -207,7 +208,7 @@
raster <- writeRaster(raster, overwrite=overwrite, filetype=filetype)
}
- if (r %in% track) { .showTrack(r, track, starttime) }
+ if (r %in% track) { .showTrack(r, raster at nrows, track, starttime) }
}
if (filename == "") {
Modified: pkg/raster/R/project.R
===================================================================
--- pkg/raster/R/project.R 2009-03-17 07:02:01 UTC (rev 366)
+++ pkg/raster/R/project.R 2009-03-17 11:54:55 UTC (rev 367)
@@ -60,7 +60,6 @@
}
starttime <- proc.time()
-
for (r in 1:nrow(to)) {
cells <- rowCells + (r-1) * ncol(to)
xy <- xyFromCell(to, cells)
@@ -87,7 +86,7 @@
to <- writeRaster(to, overwrite=overwrite)
}
- if (r %in% track) { .showTrack(r, track, starttime) }
+ if (r %in% track) { .showTrack(r, to at nrows, track, starttime) }
}
if (inMemory) {
Modified: pkg/raster/R/reclass.R
===================================================================
--- pkg/raster/R/reclass.R 2009-03-17 07:02:01 UTC (rev 366)
+++ pkg/raster/R/reclass.R 2009-03-17 11:54:55 UTC (rev 367)
@@ -47,6 +47,7 @@
} else {
starttime <- proc.time()
+
for (r in 1:nrow(raster)) {
raster <- readRow(raster, r)
res <- values(raster)
@@ -60,7 +61,7 @@
outraster <- setValues(outraster, res, r)
outraster <- writeRaster(outraster, overwrite=overwrite, filetype=filetype)
}
- if (r %in% track) { .showTrack(r, track, starttime) }
+ if (r %in% track) { .showTrack(r, raster at nrows, track, starttime) }
}
return(outraster)
}
Modified: pkg/raster/R/resample.R
===================================================================
--- pkg/raster/R/resample.R 2009-03-17 07:02:01 UTC (rev 366)
+++ pkg/raster/R/resample.R 2009-03-17 11:54:55 UTC (rev 367)
@@ -25,6 +25,7 @@
v <- vector(length=0)
rowCells <- 1:ncol(to)
starttime <- proc.time()
+
for (r in 1:nrow(to)) {
cells <- rowCells + (r-1) * ncol(to)
xy <- xyFromCell(to, cells)
@@ -40,7 +41,7 @@
to <- writeRaster(to, overwrite=overwrite, filetype=filetype)
}
- if (r %in% track) { .showTrack(r, track, starttime) }
+ if (r %in% track) { .showTrack(r, to at nrows, track, starttime) }
}
if (inMemory) {
Modified: pkg/raster/R/showTrack.R
===================================================================
--- pkg/raster/R/showTrack.R 2009-03-17 07:02:01 UTC (rev 366)
+++ pkg/raster/R/showTrack.R 2009-03-17 11:54:55 UTC (rev 367)
@@ -4,8 +4,8 @@
# Version 0.8
# Licence GPL v3
-.showTrack <- function(r, track, starttime) {
+.showTrack <- function(r, nrows, track, starttime) {
elapsed <- (proc.time() - starttime)[3]
tpr <- round((elapsed /r), digits=2)
- print(paste('row', r, '--', tpr, 'seconds/row --', nrow(raster)+1-r, " rows to go"))
+ print(paste('row', r, '--', tpr, 'seconds/row --', nrows+1-r, " rows to go"))
}
Modified: pkg/raster/R/write.R
===================================================================
--- pkg/raster/R/write.R 2009-03-17 07:02:01 UTC (rev 366)
+++ pkg/raster/R/write.R 2009-03-17 11:54:55 UTC (rev 367)
@@ -6,8 +6,6 @@
# Licence GPL v3
-
-
writeFormats <- function() {
gd <- gdalDrivers()
gd <- as.matrix(subset(gd, gd[,3] == T))
@@ -18,19 +16,12 @@
return(m)
}
-
-.isSupportedGDALFormat <- function(dname) {
- gd <- gdalDrivers()
- gd <- as.matrix(subset(gd, gd[,3] == T))
- res <- dname %in% gd
- if (!res) { stop(paste(dname, "is not a supported file format. See writeFormats()" ) ) }
- return(res)
-}
-
writeRaster <- function(raster, filetype='raster', overwrite=FALSE) {
+ raster_name <- deparse(substitute(raster))
+
if (dataContent(raster) != 'row' & dataContent(raster) != 'all' & dataContent(raster) != 'sparse' ) {
stop('No data available for writing. First use setValues()')
}
@@ -51,7 +42,12 @@
raster <- .writeGDALall(raster, gdalfiletype=filetype, overwrite=overwrite, mvFlag=NA, options=NULL)
}
}
+
+ assign(raster_name, raster, envir=parent.frame())
+# return(invisible())
+
return(raster)
+
}
writeStack <- function(rstack, overwrite=FALSE) {
Modified: pkg/raster/R/writeGDAL.R
===================================================================
--- pkg/raster/R/writeGDAL.R 2009-03-17 07:02:01 UTC (rev 366)
+++ pkg/raster/R/writeGDAL.R 2009-03-17 11:54:55 UTC (rev 367)
@@ -10,37 +10,6 @@
# authors: Timothy H. Keitt, Roger Bivand, Edzer Pebesma, Barry Rowlingson
-#.GDALDataTypes <- c('Unknown', 'Byte', 'UInt16', 'Int16', 'UInt32','Int32', 'Float32', 'Float64', '
-# what are these? CInt16', 'CInt32', 'CFloat32', 'CFloat64')
-# this needs to get fancier; depending on object and the abilties of the drivers
-.getGdalDType <- function(dtype) {
- if (!(dtype %in% c('LOG1S', 'INT1S', 'INT2S', 'INT4S', 'INT8S', 'INT1U', 'INT2U', 'INT4U', 'INT8U', 'FLT4S', 'FLT8S'))) {
- stop('not a valid data type')
- }
- type <- .shortDataType(dtype)
- size <- dataSize(dtype) * 8
- if (type == 'LOG') {
- return('Byte')
- }
- if (type == 'INT') {
- type <- 'Int'
- if (size == 64) {
- size <- 32
- warning('8 byte integer values not supported by rgdal, changed to 4 byte integer values')
- }
- if (size == 8) {
- return('Byte')
- } else if (dataSigned(dtype)) {
- type <- paste('U', type, sep='')
- }
- } else {
- type <- 'Float'
- }
- return(paste(type, size, sep=''))
-}
-
-
-
.getGDALtransient <- function(raster, gdalfiletype, overwrite, mvFlag, options) {
# .isSupportedFormat(gdalfiletype)
Modified: pkg/raster/R/writeRaster.R
===================================================================
--- pkg/raster/R/writeRaster.R 2009-03-17 07:02:01 UTC (rev 366)
+++ pkg/raster/R/writeRaster.R 2009-03-17 11:54:55 UTC (rev 367)
@@ -17,6 +17,7 @@
.writeRasterAll <- function(raster, overwrite=FALSE) {
+
filename <- trim(raster at file@name)
if (filename == "") {
stop('first provide a filename. E.g.: raster <- setFilename(raster, "c:/myfile")')
@@ -78,6 +79,9 @@
raster at data@values[raster at data@values <= raster at file@nodatavalue] <- NA
raster at data@values <- as.logical(values(raster))
}
+
+ close(raster at file@con)
+ attr(raster at file, "con") <- file(filename, "rb")
return(raster)
}
Modified: pkg/raster/R/writeRasterRow.R
===================================================================
--- pkg/raster/R/writeRasterRow.R 2009-03-17 07:02:01 UTC (rev 366)
+++ pkg/raster/R/writeRasterRow.R 2009-03-17 11:54:55 UTC (rev 367)
@@ -33,6 +33,7 @@
.stopRowWriting <- function(raster) {
.writeRasterHdr(raster)
close(raster at file@con)
+ attr(raster at file, "con") <- file(filename(raster), "rb")
raster at data@haveminmax <- TRUE
raster at data@source <- 'disk'
raster at data@content <- 'nodata'
Modified: pkg/raster/man/init.Rd
===================================================================
--- pkg/raster/man/init.Rd 2009-03-17 07:02:01 UTC (rev 366)
+++ pkg/raster/man/init.Rd 2009-03-17 11:54:55 UTC (rev 367)
@@ -42,7 +42,7 @@
# there are different ways to set all values to 1
# with a function (this will work for very large files if a file name is supplied)
set1function <- function(x){rep(1, x)}
- r <- init(r, fun=set1function, filename='test.grd')
+ r <- init(r, fun=set1function, filename='test.grd', overwrite=TRUE)
# "manually", if all the values can be kept in memory.
r <- setValues(r, rep(1, ncell(r)))
# or
Modified: pkg/raster/man/writeRaster.Rd
===================================================================
--- pkg/raster/man/writeRaster.Rd 2009-03-17 07:02:01 UTC (rev 366)
+++ pkg/raster/man/writeRaster.Rd 2009-03-17 11:54:55 UTC (rev 367)
@@ -43,6 +43,10 @@
}
}
+\value{
+This function is used for the side-effect of writing values to a file.
+}
+
\author{Robert J. Hijmans}
\seealso{ writeFormats \code{\link[raster]{writeFormats}} }
More information about the Raster-commits
mailing list