[R-sig-Geo] Using RGDAL to "copy" header information... (Jonathan Greenberg)

Robert Hijmans r.hijmans at gmail.com
Thu Nov 6 14:56:30 CET 2008


Sorry, I hit the send button unintentionally

Next try:

> Jonathan
>
> You might be interested in the raster package available from r-forge
> (within the Rgis project). You would need to dwnload, build and
> install it 'manually'
>
> The below code example reads a line from an input raster, puts the values
>
> library(raster)
> infile="d:/input.tif"
> outfile="d:/test.grd"
> rin <- raster.from.file(infile)
> rout <- set.raster(rin, outfile)
>
> for (r in 1:nrow(rin)) {
>  rin <- read.row(rin, r)
>
>  rout <- set.values.row(rout, values(rin) + 10, r)
>  rout <- write.row(rout)
> }
>

The output file is a binary file with a text (ini type) header. It is
fairly trivial to write other, more standard, hdr files, and I will
add that over the coming weeks.
If you do not want to build the package, just have a look at the
raster.write.R file in the code repository. You will able to see how
we do it.

Robert

>
>
>
>
>>
>> Message: 2
>> Date: Wed, 05 Nov 2008 09:18:47 -0800
>> From: Jonathan Greenberg <greenberg at ucdavis.edu>
>> Subject: [R-sig-Geo] Using RGDAL to "copy" header information...
>> To: gdal-dev at lists.osgeo.org, R-sig-Geo at stat.math.ethz.ch,
>>        r-help at r-project.org
>> Cc: Solomon Dobrowski <solomon.dobrowski at cfc.umt.edu>
>> Message-ID: <4911D577.2050300 at ucdavis.edu>
>> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>>
>> R-geographers...
>>
>> I'm trying to solve a problem to implement a line-by-line tiled
>> processing using RGDAL (read 1 line of an image, process the one line,
>> write one line of the image to a binary file).  Everything except for
>> the final step I'm able to do using a combination of RGDAL and r-base
>> commands.  Below is the basic structure, the input file "elev" can be
>> any image file GDAL supports -- the code below just "copies" the image
>> one line at a time:
>>
>> ###
>>
>> library(rgdal)
>> infile='elev'
>> outfile_base='testout'
>> outfile_ext='.bil'
>> outfile=paste(outfile_base,outfile_ext,sep='')
>> outcon <- file(outfile, "wb")
>>
>> infile_info=GDALinfo(infile)
>> nl=infile_info[[1]]
>> ns=infile_info[[2]]
>>
>> for (row in 1:nl) {
>>    templine <- readGDAL(infile,region.dim=c(1,ns),offset=c(row-1,0))
>>    writeBin(templine[[1]], outcon,size=4)
>> }
>> close(outcon)
>> # Below doesn't work
>> # writeGDAL(templine,outfile_base,drivername='EHdr',type="Float32")
>>
>> ###
>>
>> The issue is this: I need to be able to effectively copy the
>> geographic/header information from the input file (the last line almost
>> works, but as you can see it sets the line #s to 1, not "ns"), and parse
>> it over to the output file (which is some form of a flat binary file --
>> in this case, an Arc Binary Raster, but an ENVI binary output would also
>> be good) -- ideally I'd like to be able to modify this header,
>> particularly the number type (e.g. if the input file is an integer, and
>> I'm writing out a floating point binary, I need that reflected in the
>> output header).  I can do this by *manually* creating the output header
>> via a series of ascii-writes, but I was wondering if there is a more
>> effective way of doing this using RGDAL that might apply generically to
>> the header of any binary image file I might write?  Thanks!
>>
>> --j
>>
>>
>> --
>>
>> Jonathan A. Greenberg, PhD
>> Postdoctoral Scholar
>> Center for Spatial Technologies and Remote Sensing (CSTARS)
>> University of California, Davis
>> One Shields Avenue
>> The Barn, Room 250N
>> Davis, CA 95616
>> Cell: 415-794-5043
>> AIM: jgrn307, MSN: jgrn307 at hotmail.com, Gchat: jgrn307
>>
>>
>>
>




More information about the R-sig-Geo mailing list