Search code examples
rrastergis

How can I get the cell number of a raster using extent information?


I have a raster and I am using the raster package.

class       : RasterLayer 
dimensions  : 103, 118, 12154  (nrow, ncol, ncell)
resolution  : 0.008333333, 0.008333333  (x, y)
extent      : -83.075, -82.09167, 34.95833, 35.81667  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
data source : C:\fb.tif 
names       : fdr_fb 
values      : 1, 128  (min, max)

I know how to subset and all. But how can I find the cellnumber (preferred) or cellvalue by using the Lat-Long value?

For example, I can find cell value using lat/long:

extract(ras,SpatialPoints(cbind(-82.8,35.2))) 

But I want to find the cell number (row,col) corresponding to (Say) Long= -82.1 and Lat= 35.0

Raster: https://www.dropbox.com/s/8nhfirxr2hm3l4v/fdr_fb.tif?dl=0


Solution

  • To get the cell number from a point, you can do:

    cellFromXY(ras, cbind(-82.8, 35.2))
    

    If you have an Extent object e you can do:

    cellsFromExtent(ras, e)