Skip to content
Prev 3852 / 29559 Next

Spatial analysis - matrix algebra

Hi Paulo,

You can read ESRI ASCII grids (amongst many others) using rgdal package:

library(rgdal)
d <- readGDAL("file1.asc")

See gdalDrivers() for a listing of the available file formats for your 
package build.

The read above would result in a single-colum SpatialGridDataFrame - 
named "band1". To add further files as a new column (assuming the 
subsequent files have exactly the same grid topology):

d$band2 <- readGDAL("file2.asc")$band1

. . . and so on.

To obtain the resulting raster bands as individual matrices you can use 
as.image.SpatialGridDataFrame:

m1 <- as.image.SpatialGridDataFrame(d[1])$z

m2 <- as.image.SpatialGriddataFrame(d[2])$z

. . .

Then use your matrices as needed.  If you only want the matrices and not 
the intermediate SGDF objects, use readGDAL directly:

m1 <- as.image.SpatialGridDataFrame(readGDAL("file1.asc")[1])
. . .

These could be collected in a single 3-way array, or a list of matrices 
as needed.

HTH,

Mike.
Paulo Cardoso wrote: