1

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.

1

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.

  • Thank you very much! As a follow up - to only extract a part of the map (e.g. a polygon), I have created a polygon using polyTara <- SpatialPolygons(...). How would I apply this before extract tthe points? – Neviem Aug 4 '18 at 18:02
  • You can use raster::extract() to retrieve the raster values from any Spatial* object. For example, extract(your_raster, your_polgon) will return a dataframe of the pixel values. From here it is simple to create summary statistics (like those you would get from ArcGIS’s Zonal Statistics as Table). – Jackson Aug 4 '18 at 20:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.