6

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

0

1 Answer 1

10
# 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
2
  • 1
    Can simplify to rowColFromCell(ras, cellFromXY(ras, cbind(-82.8,35.2)))
    – mdsumner
    Commented Dec 26, 2015 at 1:12
  • @mdsumner yes it can
    – user32309
    Commented Dec 26, 2015 at 14:15

Your Answer

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

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