I have a rasterStack object in R and a set of polygons that represent different landcover classes in that area. For all the cells that fall in those polygons, I have extracted pixel values for each band and saved these in a dataframe. However, I can't get the centroid coordinates for these cells and I need them. Any advice on how to achieve that?
How do I get a raster layer containing cell centroid coordinates (x,y) of another raster using R?
You can use
library(raster) r <- raster(ncols=5, nrows=5) cells <- c(3, 7, 10) xyFromCell(r, cells) # x y #[1,] 0 72 #[2,] -72 36 #[3,] 144 36
If you wanted the coordinates for all cells, you could do
xy <- xyFromCell(r, 1:ncell(r))
xy <- coordinates(raster(r))