[Raster-commits] r167 - in pkg/raster: R man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Wed Jan 21 12:47:01 CET 2009
Author: rhijmans
Date: 2009-01-21 12:47:01 +0100 (Wed, 21 Jan 2009)
New Revision: 167
Modified:
pkg/raster/R/export.R
pkg/raster/R/writeGDAL.R
pkg/raster/R/writeRaster.R
pkg/raster/man/export.Rd
Log:
Modified: pkg/raster/R/export.R
===================================================================
--- pkg/raster/R/export.R 2009-01-21 02:21:28 UTC (rev 166)
+++ pkg/raster/R/export.R 2009-01-21 11:47:01 UTC (rev 167)
@@ -1,80 +1,3 @@
-
-import <- function(raster, filename="", overwrite=FALSE) {
-# check extension
- if (.driver(raster) == "raster") {
- stop("file is a raster format file, it cannot be imported")
- }
- filename <- trim(filename)
- if (filename == "") {
- filename <- .setFileExtensionHeader(filename(raster))
- } else {
- filename <- .setFileExtensionHeader(filename)
- }
-
- rsout <- setRaster(raster, filename=filename)
- for (r in 1:nrow(raster)) {
- raster <- readRow(raster, r)
- rsout <- setValues(rsout, values(raster), r)
- rsout <- writeRaster(rsout, overwrite=overwrite)
- }
- rsout <- clearValues(rsout)
- return(rsout)
-}
-
-
-export <- function(raster, filename="", filetype="ascii", keepGRD=TRUE, overwrite=FALSE) {
- filename <- trim(filename)
- if (filename == "") {
- filename <- filename(raster)
- }
- if (filetype == "ascii") {
- filename <- setFileExtension(filename, ".asc")
- outras <- setRaster(raster, filename)
- for (r in 1:nrow(raster)) {
- outras <- setValues(outras, values(readRow(raster, r)), r)
- .writeAscii(outras, overwrite=overwrite)
- }
-
- } else if (filetype == "bil") {
- .exportToBil(raster, filename=filename, keepGRD=keepGRD, overwrite=overwrite)
-
-
- } else {
- stop("filetype not supported")
- }
-
- return(rasterFromFile(filename))
-}
-
-
-
-.exportToBil <- function(raster, filename="", keepGRD=TRUE, overwrite=FALSE) {
- sourcefile <- .setFileExtensionValues(filename(raster))
- targetfile <- setFileExtension(filename(raster), ".bil")
- if (keepGRD) {
- if (file.exists(targetfile)) {
- if (!(overwrite)) {
- stop(paste("File exists:", targetfile, " Use overwrite=TRUE, if you want to overwrite it"))
- }
- }
- file.copy(from=sourcefile, to=targetfile, overwrite=overwrite)
- } else {
- if (file.exists(targetfile)) {
- if (overwrite) {
- file.remove(targetfile)
- } else {
- stop(paste("File exists:", targetfile, " Use overwrite=TRUE, if you want to overwrite it"))
- }
- }
- file.rename(from=sourcefile, to=targetfile)
- }
- .writeBilHdr(raster)
- if (!(keepGRD)) { file.remove(filename(raster)) }
- return(rasterFromFile(targetfile))
-}
-
-
-
writeHeader <- function(raster, type) {
type <- toupper(type)
@@ -172,11 +95,11 @@
cat("MaxValue=", maxValue(raster), "\n", file = thefile)
close(thefile)
- writeWorldfile(raster, ".rww")
+ worldFile(raster, ".rww")
}
-writeWorldfile <- function(raster, extension=".wld") {
+worldFile <- function(raster, extension=".wld") {
hdrfile <- setFileExtension(filename(raster), extension)
thefile <- file(hdrfile, "w")
cat(xres(raster), "\n", file = thefile)
Modified: pkg/raster/R/writeGDAL.R
===================================================================
--- pkg/raster/R/writeGDAL.R 2009-01-21 02:21:28 UTC (rev 166)
+++ pkg/raster/R/writeGDAL.R 2009-01-21 11:47:01 UTC (rev 167)
@@ -54,65 +54,6 @@
}
-
-exportGDAL <- function(raster, filename, gdalfiletype = "GTiff", overwrite=FALSE, ForceIntOutput = FALSE ) {
- mvFlag = NA
- options = NULL
- nbands = nlayers(raster)
-
- outras <- setRaster(raster, filename)
- transient <- .getGDALtransient(raster, gdalfiletype, overwrite, ForceIntOutput, mvFlag, options)
-
- for (band in 1:nbands) {
- if (dataContent(raster)=='all') {
-# if (!is.na(mvFlag)) vals[is.na(vals)] = mvFlag
-# This would work, but could potentially lead to memory problems (making a copy of the values before writing)
-# x <- putRasterData(transient, t(values(raster, format='matrix')), band, c(0, 0))
- if (ForceIntOutput) {
- for (r in 1:nrow(raster)) {
- x <- putRasterData(transient, as.integer(round(valuesRow(raster, r))), band, c((r-1), 0))
- }
- } else {
- for (r in 1:nrow(raster)) {
- x <- putRasterData(transient, valuesRow(raster, r), band, c((r-1), 0))
- }
- }
- } else {
- if (dataSource(raster)=='ram') {
- stop("No data on disk, and not all values in memory. Cannot write the file")
- }
- if (ForceIntOutput) {
- for (r in 1:nrow(raster)) {
- x <- putRasterData(transient, as.integer(round(values(readRow(raster, r)))), band, c((r-1), 0))
- }
- } else {
- for (r in 1:nrow(raster)) {
- x <- putRasterData(transient, values(readRow(raster, r)), band, c((r-1), 0))
- }
- }
- }
-
-# if (!is.na(mvFlag)) {
-# transient_b <- getRasterBand(dataset = transient, band = band)
-# .Call("RGDAL_SetNoDataValue", transient_b, as.double(mvFlag), PACKAGE = "rgdal")
-# }
- }
- saveDataset(transient, outras at filename)
- GDAL.close(transient)
-
-# do NOT do this, it removes the driver for future use!!! ????
-# GDAL.close(driverobj)
- outras at data@min <- raster at data@min
- outras at data@max <- raster at data@max
- if (!is.na((outras at data@min))) { outras at data@haveminmax <- TRUE }
- return(outras)
-}
-
-
-
-
-
-
.writeGDALrow <- function(raster, gdalfiletype, overwrite, ForceIntOutput, mvFlag, options ) {
rownr <- rowFromCell(raster, dataIndices(raster)[1])
@@ -122,34 +63,21 @@
if ( rownr == 1) {
transient <- .getGDALtransient(raster, gdalfiletype, overwrite, ForceIntOutput, mvFlag, options)
attr(raster, "transient") <- transient
- raster at data@min <- 3e34
- raster at data@max <- -3e34
- raster at data@haveminmax <- FALSE
raster at file@driver <- 'gdal'
raster at file@gdalhandle <- list()
}
for (band in 1:nlayers(raster)) {
x <- putRasterData(raster at transient, values(raster, rownr), band, c((rownr-1), 0))
-
- rsd <- na.omit(raster at data@values) # min and max values; perhaps not worth it, as they won't be saved to file
- if (length(rsd) > 0) {
- raster at data@min <- min(raster at data@min, min(rsd))
- raster at data@max <- max(raster at data@max, max(rsd))
- }
- }
-# if (!is.na(mvFlag)) {
-# transient_b <- getRasterBand(dataset = transient, band = band)
-# .Call("RGDAL_SetNoDataValue", transient_b, as.double(mvFlag), PACKAGE = "rgdal")
-# }
+ }
if ( rownr == nrow(raster)) {
saveDataset(raster at transient, filename(raster) )
GDAL.close(raster at transient)
- raster <- rasterFromFile(filename(raster))
+ rasterout <- rasterFromFile(filename(raster))
- raster at data@haveminmax <- TRUE
- raster at data@source <- 'disk'
- raster at data@content <- 'nodata'
- raster at data@values <- vector(length=0)
+ rasterout at data@haveminmax <- raster at data@haveminmax
+ rasterout at data@min <- raster at data@min
+ rasterout at data@max <- raster at data@max
+
}
return(raster)
}
Modified: pkg/raster/R/writeRaster.R
===================================================================
--- pkg/raster/R/writeRaster.R 2009-01-21 02:21:28 UTC (rev 166)
+++ pkg/raster/R/writeRaster.R 2009-01-21 11:47:01 UTC (rev 167)
@@ -115,60 +115,66 @@
return(raster)
}
+ ..startWriting <- function(raster, overwrite) {
+ raster <- setFilename(raster, .setFileExtensionHeader(filename(raster)))
+ if (filename(raster) == "") {
+ stop('first provide a filename. E.g.: raster <- setFilename(raster, "c:/myfile")')
+ }
+ if (!overwrite & file.exists(filename(raster))) {
+ stop(paste(filename(raster),"exists.","use 'overwrite=TRUE' if you want to overwrite it"))
+ }
+ raster at file@name <- .setFileExtensionHeader(filename(raster))
+ binraster <- .setFileExtensionValues(filename(raster))
+ attr(raster, "filecon") <- file(binraster, "wb")
+ raster at data@min <- 3e34
+ raster at data@max <- -3e34
+ raster at data@haveminmax <- FALSE
+ raster at file@driver <- 'raster'
+ raster at file@gdalhandle <- list()
+ return(raster)
+}
+
+..stopWriting <- function(raster) {
+ .writeRasterHdr(raster)
+ close(raster at filecon)
+ raster at data@haveminmax <- TRUE
+ raster at data@source <- 'disk'
+ raster at data@content <- 'nodata'
+ raster at data@values <- vector(length=0)
+ return(raster)
+}
.writeRasterRow <- function(raster, overwrite=FALSE) {
- if (dataContent(raster) != 'row') { stop('raster does not contain a row') }
+ if (dataContent(raster) != 'row') {
+ stop('raster does not contain a row')
+ }
+ if (raster at file@datatype == "integer") {
+ raster at data@values <- as.integer(round(raster at data@values))
+ }
+ if (class(values(raster)) == "integer" & raster at file@datatype == "numeric") {
+ raster at data@values <- as.numeric(values(raster))
+ }
+ if (dataIndices(raster)[1] == 1) {
+ raster <- ..startWriting(raster, overwrite)
+ }
- if (dataIndices(raster)[1] == 1) {
- # FIRST ROW
- raster <- setFilename(raster, .setFileExtensionHeader(filename(raster)))
- if (filename(raster) == "") {
- stop('first provide a filename. E.g.: raster <- setFilename(raster, "c:/myfile")')
- }
- if (!overwrite & file.exists(filename(raster))) {
- stop(paste(filename(raster),"exists.","use 'overwrite=TRUE' if you want to overwrite it"))
- }
- raster at file@name <- .setFileExtensionHeader(filename(raster))
- binraster <- .setFileExtensionValues(filename(raster))
- attr(raster, "filecon") <- file(binraster, "wb")
- raster at data@min <- 3e34
- raster at data@max <- -3e34
- raster at data@haveminmax <- FALSE
- raster at file@driver <- 'raster'
- raster at file@gdalhandle <- list()
- }
-
- if (raster at file@datatype == "integer") { raster at data@values <- as.integer(round(raster at data@values)) }
- if (class(values(raster)) == "integer" & raster at file@datatype == "numeric") { raster at data@values <- as.numeric(values(raster)) }
-
raster at data@values[is.nan(raster at data@values)] <- NA
raster at data@values[is.infinite(raster at data@values)] <- NA
- rsd <- na.omit(raster at data@values) # min and max values
- if (length(rsd) > 0) {
- raster at data@min <- min(raster at data@min, min(rsd))
- raster at data@max <- max(raster at data@max, max(rsd))
- }
-# raster at data@values[is.na(raster at data@values)] <- raster at file@nodatavalue
- writeBin(as.vector(raster at data@values), raster at filecon, size = raster at file@datasize)
+ writeBin(raster at data@values, raster at filecon, size = raster at file@datasize)
- if (dataIndices(raster)[2] == ncell(raster)) {
- # LAST ROW
- .writeRasterHdr(raster)
- close(raster at filecon)
- raster at data@haveminmax <- TRUE
- raster at data@source <- 'disk'
- raster at data@content <- 'nodata'
- raster at data@values <- vector(length=0)
- }
- if (dataIndices(raster)[2] > ncell(raster)) {
- stop(paste('writing beyond end of file. last cell:', dataIndices(raster)[2], '>', ncell(raster)))
+ if (dataIndices(raster)[2] >= ncell(raster)) {
+ raster <- ..stopWriting(raster)
+ if (dataIndices(raster)[2] > ncell(raster)) {
+ warning(paste('You have written beyond the end of file. last cell:', dataIndices(raster)[2], '>', ncell(raster)))
+ }
}
return(raster)
}
+
.writeSparse <- function(raster, overwrite=FALSE) {
raster at file@driver <- 'raster'
Modified: pkg/raster/man/export.Rd
===================================================================
--- pkg/raster/man/export.Rd 2009-01-21 02:21:28 UTC (rev 166)
+++ pkg/raster/man/export.Rd 2009-01-21 11:47:01 UTC (rev 167)
@@ -1,42 +1,27 @@
-\name{export}
+\name{header files}
-\alias{import}
-\alias{export}
-\alias{exportGDAL}
\alias{writeHeader}
-\alias{writeWorldfile}
+\alias{worldFile}
\title{ Write raster data to a file }
\description{
- Export an existing raster file to an ASCII or BIL file. Or to many other formats via exportGDAL
Or create header files associated with other formats for use in different software (use ENVI or ErdasRaw for ArcGIS).
}
\usage{
-export(raster, filename="", filetype="ascii", keepGRD=TRUE, overwrite=FALSE)
-exportGDAL(raster, filename, gdalfiletype="GTiff", overwrite=FALSE, ForceIntOutput = FALSE)
writeHeader(raster, type)
-writeWorldfile(raster, extension=".wld")
-import(raster, filename="", overwrite=FALSE)
+worldFile(raster, extension=".wld")
}
\arguments{
\item{raster}{ RasterLayer object }
- \item{overwrite}{ Logical. If \code{TRUE} existing files are overwritten }
- \item{filename}{ The file resulting from import or export }
- \item{filetype}{ The type of file to export to ('ascii' or 'bil')}
- \item{gdalfiletype}{ GDAL file type (driver) name. See below for list of supported file types }
- \item{ForceIntOutput}{save as integer}
- \item{keepGRD}{ Logical. If \code{FALSE} the existing raster file (internal format) will not be kept after exporting; only applied to other formats that can use the same binary format. }
\item{type}{ Type of header file: 'BIL', 'ENVI', 'ErdasRaw', 'raster' }
\item{extension}{ extension of header file. Only implemented for worldfile. }
}
\details{
- data in a \code{RasterLayer} object is written to a file. Small datasets can be loaded into memory manipulated,
- and saved to disk again. For large datasets, you can read and write by row.
+ You can use writeRaster to save a existing file in another format. But if you have a file in a 'raster' format (or similar), you can also only export a header file, and use the data file that already exists.
writeHeader writes an ENVI, or BIL header (.hdr) file; or an Erdas Raw (.raw) header file. This (hopefully) allows for reading the binary data ("raster.gri") in other programs such as ENVI ArcMap.
- if format = "worldfile", the function writes an ESRI "world" file, that is sometimes used to georeference images.
-
+ if format = "worldfile", the function writes an ESRI "world" file, that is sometimes used to georeference images.
}
\author{Robert J. Hijmans \email{r.hijmans at gmail.com}}
@@ -45,12 +30,11 @@
\examples{
rs <- rasterFromFile(system.file("external/test.ag", package="sp"))
-rsimport <- import(rs, filename="imported", overwrite=TRUE)
-rsexport <- export(rsimport, filename="exported", filetype='ascii', overwrite=TRUE)
-writeHeader(rsexport, type="ENVI")
+rs <- readAll(rs)
+rs <- setFilename(rs, 'export.grd')
+rs <- writeRaster(rs, format='raster')
+writeHeader(rs, type="ENVI")
-rsimp <- import(rs, filename="imported", overwrite=TRUE)
-
}
\keyword{ spatial }
More information about the Raster-commits
mailing list