R – How to Extract Values from .tiff Files

extractgeotiff-tiffrraster

Maybe a rather simple question, but I could not find an answer. I am trying to extract long, lat, value from a .tiff file of a map of Africa with long/lat and soil acidity as the values.

Here is some information:

class       : RasterLayer 
dimensions  : 9578, 8990, 86106220  (nrow, ncol, ncell)
resolution  : 1000, 1000  (x, y)
extent      : -4108621, 4881379, -5076086, 4501914  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=laea +lat_0=5 +lon_0=20 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0 
data source : C:\Users\M17GPPALT.tif 
names       : M17GPPALT 
values      : 0, 65535  (min, max)

I want to extract simple long, lat, ph value for each point as an array.

Best Answer

Use rasterToPoints - for example:

> r = raster(matrix(1:12,3,4))
> rasterToPoints(r)
          x         y layer
 [1,] 0.125 0.8333333     1
 [2,] 0.375 0.8333333     4
 [3,] 0.625 0.8333333     7
 [4,] 0.875 0.8333333    10
 [5,] 0.125 0.5000000     2
 [6,] 0.375 0.5000000     5
 [7,] 0.625 0.5000000     8
 [8,] 0.875 0.5000000    11
 [9,] 0.125 0.1666667     3
[10,] 0.375 0.1666667     6
[11,] 0.625 0.1666667     9
[12,] 0.875 0.1666667    12

you'll end up with 86 million rows, although cells with NA aren't returned.