I'm an experienced R user new to GIS, but finding my way. I've recently run into an issue that I haven't been able to find any satisfactory documentation on (but could just be searching poorly). I'm attempting to extract data from a geotiff. However, when I load said file, all I can get are color values. For example:

aMap <- raster("./tiff/mymap.tiff")
extract(aMap, areaCoords, buffer=2000)

[1] 247 247 240 247 240 247 247 247 247 247 240 240 240 247 247 247 240 240 247
[20] 247

where areaCoords is a spatial points data frame. I'm using the sp and raster libraries. Now, I can add a layer argument to raster, and get other channels (I'm assuming this goes in RGB order), but, the data is a single value in different units alltogether.

1) Is there something I'm missing in loading in a geotiff for getting data rather than just colors?

2) If all there is to extract are colors, any suggestions on converting from colors to a known scale (e.g., perhaps I can get the RGB of the low and high values of the scale - perhaps).

  • Actually now I'm not sure what you mean by "the data is a single value in different units altogether". Can you edit to show the code (and give us the data file is poss)?
    – Spacedman
    Oct 6, 2014 at 16:09
  • So, the values should be scaled between 1-20, for example.
    – jebyrnes
    Oct 6, 2014 at 16:18
  • It would be odd for someone to write a GeoTIFF with 8-bit values (0 to 255) to code 1 to 20, but madder things have been done. Just linearly scale the values by multiplying by (19/255) and add one in that case (see my answer for similar).
    – Spacedman
    Oct 6, 2014 at 16:21
  • Nah, they're all colors. I've now tried stack, and, indeed, it's RGB bands. Now it's an issue of trying to move from RGB to the actual data values. Which, if they used a sensible scale, should be possible - although converting from RGB to a single number is new territory for me. Ah well - must be a formula somewhere!
    – jebyrnes
    Oct 6, 2014 at 16:27
  • 1
    If its a categorical map then you just need to convert the colours to hex strings (see rgb()) and build a lookup. Otherwise you need to make a function of r,g,b that returns a single V....
    – Spacedman
    Oct 7, 2014 at 8:44

1 Answer 1


They are the values in the file. How they correspond to measurements is unknown, and would have to be specified in external metadata.

A GeoTIFF can easily store decimal numbers in bands, and you can get the range simply enough. Here I read in a 3 band raster using stack and check the first band:

> dd = stack("d.tif")
> range(values(dd)[,1])
[1]   0.0000 801.1061

If you have information that says how you map your band-1 values to measurements then you just have to operate on the raster values. For example, to scale values from 0 to max(d1) to 0 to 2*pi:

d1 = dd[[1]] # get first band
d1 = d1 * 2 * pi / max(values(d1))

If your TIFF is one byte per value, and you want to scale it to 1 to 20, for example:

d1 = 1 + d1 * (19/255)

should do it.

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.