[GIS] How to find the cell location index of a raster using Lat long information

rraster

I have a raster for which I want to find the Cell Location (Row, Col) if I have the Lat-long information.

I know how to get the value of a particular cell using lat long.

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

But I need the row-col of this cell, rather than it's value. I am using the raster package in R.

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

Best Answer

# Open tif file
ras <- raster("fdr_fb.tif")

# "extract" function can return the cell number, here 8766
extract(ras,SpatialPoints(cbind(-82.8,35.2)), cellnumbers=TRUE) 
# cells fdr_fb
#  8766      2

# We store the cell number
id.cell <- extract(ras,SpatialPoints(cbind(-82.8,35.2)), cellnumbers=TRUE)[1]

# We extract row and col indices with "rowColFromCell"
rowColFromCell(ras, id.cell)
#      row col
# [1,]  75  34

# Verification
ras[75, 34]
# 2