-2

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 ?

  • Welcome to Stack Overflow! Please review [writing the perfect question](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question) to help you to ask a good question, and thus get a good answer. – Jeroen Heier Nov 23 '19 at 15:07
  • Hi! can you provide an example dataset ? – StupidWolf Nov 23 '19 at 15:27
  • dem1994<-raster("lake_dem_1994.asc") – Rismynightmare Nov 23 '19 at 15:46
  • Sorry I don't know how to use this website yet .. the raster is already existing – Rismynightmare Nov 23 '19 at 15:48
  • the only thing I can get is the attributes, and I have this for the plot : plot(dem1994,col=viridis(300,alpha=1),legend=FALSE,main="1994") points(x=2100,y=10975,pch=4) contour(dem1994, col = "white", add = TRUE, method = "flattest",nlevels = 5), and I want to know the value of the point that I have drawn on the plot – Rismynightmare Nov 23 '19 at 15:51

1 Answers1

1

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
Spacedman
  • 92,590
  • 12
  • 140
  • 224