[R-sig-Geo] rasters of cell numbers & centroid coordinates

Jordan Chamberlin jordan.chamberlin at gmail.com
Wed Aug 28 10:02:50 CEST 2013


Hi all,

For some processing that I'm doing with raster data, I need to keep 
track of cell centroid coordinate values, and I would like to do this in 
terms of a set of reference rasters which hold cell values, x and y 
coordinates, respectively, for the raster being analyzed. I can do this 
(code below) but my question is: am I going about this in the most 
efficient way? Here is what I'm doing (using a small test raster since 
mine are humongous):

#get test raster
r <- raster(system.file("external/test.grd", package="raster"))

#make copies which will hold cell identifiers, x & y values
ry <- rx <- rc <- r

#define raster of cell numbers for the non-NA cells
rc[] <- seq(1:length(r))
## for some reason, this is not working:  rc <- mask[rc,r]
## but this works fine:
rc[is.na(r)] <-NA

#assign coordinate values to corresponding rasters
ry[] <- yFromCell(rc, getValues(rc))
rx[] <- xFromCell(rc, getValues(rc))

# stack em up and view
s <- stack(r,rc,rx,ry)
names(s) <- c("values", "cell #", "x-coord", "y-coord")
plot(s)

So this works fine, but I have a feeling there are some shortcuts I'm 
missing.

Thanks for guidance,

Jordan



More information about the R-sig-Geo mailing list