0

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?

1 Answer 1

1

You can use xyFromCell

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))

or

xy <- coordinates(raster(r))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.