Request the value of a grid point of raster knowing its coordinates in R

518 Views Asked by At

I have a raster map (a digital elevation model) that i opened in R and I need to know the value (which is the altitude) of one specific grid point knowing its geographical coordinates. I've tried to use the extract() function but it seems not to work, and I've tried to convert it into a matrix but I only know the coordinates, not the column and row numbers of the cell. this is what my raster data looks like I think this is really simple but I'm stuck here.

Any idea how to get it ?

1

There are 1 best solutions below

0
Spacedman On

For any raster r, eg:

> r = raster(matrix(runif(100),10,10),xmn=3,xmx=13,ymn=10,ymx=20)

create a matrix with two columns with your coordinates:

> m = matrix(c(12,16),ncol=2)
> m
     [,1] [,2]
[1,]   12   16

and extract:

> extract(r,m)
[1] 0.9597013

add extra rows to your matrix if you have more points.

For locations outside your raster you get NA returned:

> m = matrix(c(12,21),ncol=2)
> extract(r,m)
[1] NA