Always a good idea to make a simple test. Here's a 5x6 raster:
> r = raster(matrix(runif(30),5,6))
which has these values:
> r[]
[1] 0.45825245 0.68935493 0.18918221 0.32181772 0.90459237 0.47537165
[7] 0.38745306 0.57012344 0.21467928 0.24150341 0.54005789 0.09430709
[13] 0.52352994 0.01489950 0.54890148 0.04762656 0.83931822 0.14997015
[19] 0.02994982 0.89359828 0.22488212 0.55764499 0.30531830 0.13751166
[25] 0.90074801 0.94862034 0.88637761 0.13849934 0.64490219 0.67825152
and its in a (0,1) unit square:
> extent(r)
class : Extent
xmin : 0
xmax : 1
ymin : 0
ymax : 1
Now we use the coordinates
function which gets the coordinates of the grid points, and subset those that match whatever condition we want. For example, all grid points where the raster is over 0.9:
> coordinates(r)[r[]>0.9,]
x y
[1,] 0.75000000 0.9
[2,] 0.08333333 0.1
[3,] 0.25000000 0.1
Alternatively, mask out the condition with NA and convert to Spatial Points:
> r = raster(matrix(runif(30),5,6))
> r[r[] < 0.9] = NA
> (pts = as(r, "SpatialPoints"))
class : SpatialPoints
features : 3
extent : 0.25, 0.9166667, 0.5, 0.7 (xmin, xmax, ymin, ymax)
crs : NA
That's useful if you want to do further spatial work with the sp
package. If you just want the coordinates from that then:
> coordinates(as(r, "SpatialPoints"))
x y
[1,] 0.2500000 0.7
[2,] 0.9166667 0.7
[3,] 0.5833333 0.5