[英]Request the value of a grid point of raster knowing its coordinates in R
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.我有一张在 R 中打开的栅格地图(数字高程模型),我需要知道一个特定网格点的值(即高度),知道其地理坐标。 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.
我试过使用 extract() 函数,但它似乎不起作用,我试过将其转换为矩阵,但我只知道坐标,而不知道单元格的列号和行号。 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 ?知道如何得到它吗?
For any raster r
, eg:对于任何栅格
r
,例如:
> 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:对于栅格之外的位置,您将返回 NA:
> m = matrix(c(12,21),ncol=2)
> extract(r,m)
[1] NA
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.