[R-sig-Geo] Coercing multiband readGDAL objects to a matrix/vector...
Michael Sumner
mdsumner at utas.edu.au
Fri Jan 23 21:50:58 CET 2009
Can you provide more detail as to what you mean?
If you want more than one band in the matrix you'll need to pre-allocate
the result and use sub-indexing into that matrix (2D).
In one step you could access the bands from the Spatial*DataFrame
directly as columns/vectors, but you'd have to handle the orientation
specially.
Perhaps you actually want the 3 bands as a 3D array?
Here's an example of that.
library(sp)
data(meuse.grid)
m = SpatialPixelsDataFrame(points = meuse.grid[c("x", "y")], data =
meuse.grid)
dimXY <- getGridTopology(m)@cells.dim ## the X/Y dimensions of the result
## the 3D result
result <- array(NA, c(dimXY, 3))
## slot each band matrix into the 3D array
for (i in 1:3) {
result[,,i] <- as.image.SpatialGridDataFrame(m[i])$z
}
## check the result
image(result[,,1])
image(result[,,2])
image(result[,,3])
Regards, Mike.
Jonathan Greenberg wrote:
> Michael:
>
> Thank you for your prompt reply -- if I wanted to extract multiple
> bands in a single call, how would I do that? I suppose I could do it
> with a for-next loop, but it seems there is probably some simple way
> of doing this:
>
> as.matrix(x[1:3]) (doesn't work, but the idea is I want bands 1-3 in
> this matrix).
>
> --j
>
>
>
>
> Michael Sumner wrote:
>> Hello,
>> You can use either as.imageSpatialGridDataFrame(x[1]) or
>> as.matrix(x[1]), where "x" is a SpatialGridDataFrame or
>> SpatialPixelsDataFrame.
>>
>> The "1" index assumes you want the first band, replace as appropriate.
>> The first gives you a list with x, y, z components in the convention
>> expected by image(), so
>> as.imageSpatialGridDataFrame(x[1])$z is the matrix.
>> The second returns the matrix directly but in a different orientation.
>> HTH
>>
>> Regards, Mike.
>>
>> ==============Original message text===============
>> On Fri, 23 Jan 2009 8:59:02 +1100 Jonathan Greenberg wrote:
>>
>> I was hoping I could get some info on how to coerce the type of
>> object readGDAL produces, assuming its a multiband object, into an R
>> matrix (I just want the raster values in the matrix)? Thanks!
>>
>> --j
>>
>>
> ------------------------------------------------------------------------
>
>
> No virus found in this incoming message.
> Checked by AVG - http://www.avg.com
> Version: 8.0.176 / Virus Database: 270.10.12/1910 - Release Date: 22/01/2009 6:28 PM
>
>
More information about the R-sig-Geo
mailing list