Friday 24 November 2017

raster - Extracting data, not colors, from geotiffs in R


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]]
[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).



Answer



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.



No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...