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

Verbesselt, Jan jan.verbesselt at wur.nl
Wed Aug 28 15:49:23 CEST 2013


Hi Jordan,

Nice reproducible example.

The following should work:
rc <- mask(rc,r) # instead of mask[rc,r]

Only if your raster's are very large you'll have to watch out overusing
the RAM:
If possible in that case you could avoid creating new data (doubling data)
and potentially use the following only for specific rows/columns to
extract 
cells, and coordinates:
r <- raster(system.file("external/test.grd", package="raster"))
cells <- cellFromRowColCombine(r, 1:10, 1:10)
xy <- xyFromCell(r, cells)


Hope this helps,
Jan
http://goo.gl/1mBC5F





On 28/08/13 10:02, "Jordan Chamberlin" <jordan.chamberlin at gmail.com> wrote:

>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
>
>_______________________________________________
>R-sig-Geo mailing list
>R-sig-Geo at r-project.org
>https://stat.ethz.ch/mailman/listinfo/r-sig-geo



More information about the R-sig-Geo mailing list