[gdal-dev] How to map Int32 or Float32 GTiff to color map?

Hare, Trent thare at usgs.gov
Thu May 15 12:45:21 PDT 2014


Steve,
   You can think of this very much *as a hack* but I had some success. I
was also testing how to colorize a DEM (or 32bit floating point image)
outside of gdaldem. You would need a script to configure the "lut" line for
each file min/max range though. It does seem to work and you can convert to
other colorized graphical formats (e.g. PNG) using gdal_translate.

example:
(1) create a 3 band VRT of your original file.
> ls dem.tif > mylist
> ls dem.tif >> mylist
> ls dem.tif >> mylist
> gdalbuildvrt -separate -input_file_list mylist DEM_3bands.vrt

(2) now manually add or script a <LUT> section for each band (see below for
color mapping or [dest value]. Original DEM range was from -8.296 to 4.022)

<LUT>[src value 1]:[dest value 1],[src value 2]:[dest value 2],...</LUT>

example:
<LUT>-8.296:75,-7:0,-3:0,-1.5:204,2:255,3.5:255,3.9:150,4.022:255</LUT>
from: http://www.gdal.org/gdal_vrttut.html


kind of crazy and really not tested much... Shows how convenient VRTs
continue to be.

Good luck,
Trent


example VRT and tiny DEM (1 MB, odd blip on the right):
https://www.dropbox.com/s/kb8x2vlzmwtl52v/ColorMapping_DEM_toRainbow-BrownWhite_test.zip

screenshot of DEM as converted to PNG:
> gdal_translate -ot byte -of PNG DEM_3bands.vrt DEM_3bands.png
https://www.dropbox.com/s/i1kldn7th7w5910/1MM3298IOLBYDEM08P2905_colorize.png


-------------------

The above "rainbow to brown to white" color-ramp in percent would be mapped
at:
# Red stretch pairs. The pattern is percentage:DN
0:75 10:0 40:0 55:204 70:255 85:255 99:150 100:255

# Green stretch pairs. The pattern is percentage:DN
0:0 10:0 30:151 40:204 55:204 70:102 85:0 99:75 100:255

# Blue stretch pairs. The pattern is percentage:DN
0:102 10:204 25:151 40:0 99:0 100:255

------------------

Example VRT:
<VRTDataset rasterXSize="373" rasterYSize="625">
  <GeoTransform>-5.0000000000000000e-001, 1.0000000000000000e+000,
0.0000000000000000e+000, 5.0000000000000000e-001,
0.0000000000000000e+000,-1.0000000000000000e+000</GeoTransform>
  <VRTRasterBand dataType="Float32" band="1">
    <NoDataValue>-3.40282265508890E+038</NoDataValue>
    <ColorInterp>Red</ColorInterp>
    <ComplexSource>
      <SourceFilename
relativeToVRT="1">1MM3298IOLBYDEM08P2905.tif</SourceFilename>
      <SourceBand>1</SourceBand>
      <SourceProperties RasterXSize="373" RasterYSize="625"
DataType="Float32" BlockXSize="373" BlockYSize="5" />
      <SrcRect xOff="0" yOff="0" xSize="373" ySize="625" />
      <DstRect xOff="0" yOff="0" xSize="373" ySize="625" />

<LUT>-8.296:75,-7:0,-3:0,-1.5:204,2:255,3.5:255,3.9:150,4.022:255</LUT>
    </ComplexSource>
  </VRTRasterBand>
  <VRTRasterBand dataType="Float32" band="2">
    <NoDataValue>-3.40282265508890E+038</NoDataValue>
    <ColorInterp>Green</ColorInterp>
    <ComplexSource>
      <SourceFilename
relativeToVRT="1">1MM3298IOLBYDEM08P2905.tif</SourceFilename>
      <SourceBand>1</SourceBand>
      <SourceProperties RasterXSize="373" RasterYSize="625"
DataType="Float32" BlockXSize="373" BlockYSize="5" />
      <SrcRect xOff="0" yOff="0" xSize="373" ySize="625" />
      <DstRect xOff="0" yOff="0" xSize="373" ySize="625" />

<LUT>-8.296:0,-7:0,-5:151,-3:204,-1.5:204,2:102,3.5:0,3.9:75,4.022:255</LUT>
    </ComplexSource>
  </VRTRasterBand>
  <VRTRasterBand dataType="Float32" band="3">
    <NoDataValue>-3.40282265508890E+038</NoDataValue>
    <ColorInterp>Blue</ColorInterp>
    <ComplexSource>
      <SourceFilename
relativeToVRT="1">1MM3298IOLBYDEM08P2905.tif</SourceFilename>
      <SourceBand>1</SourceBand>
      <SourceProperties RasterXSize="373" RasterYSize="625"
DataType="Float32" BlockXSize="373" BlockYSize="5" />
      <SrcRect xOff="0" yOff="0" xSize="373" ySize="625" />
      <DstRect xOff="0" yOff="0" xSize="373" ySize="625" />
      <LUT>-8.296:102,-6.5:204,-4.5:151,-3:0,3.9:0,4.022:255</LUT>
    </ComplexSource>
  </VRTRasterBand>
</VRTDataset>




On Thu, May 15, 2014 at 12:08 PM, Stephen Woodbridge <
woodbri at swoodbridge.com> wrote:

> On 5/15/2014 1:56 PM, Even Rouault wrote:
>
>> Le jeudi 15 mai 2014 16:25:12, Stephen Woodbridge a écrit :
>>
>>> Hi all,
>>>
>>> I'm trying to create a VRT file for a GTiff that is Int32 or Float32 to
>>> add a ColorTable but I seem to be missing a key piece of information.
>>> How are the pixel values mapped to the color table entries? via the
>>> histogram?
>>>
>>> My GTiff has noData=-32767 and good values the range from say -5 to 100.
>>> So do I create a color table that is based on the 256 buckets in the
>>> histogram?
>>>
>>
>> No way you can use a GDAL color table to map negative values...
>> Perhaps you should use gdaldem color-relief to generate a RGB output. Note
>> that gdaldem color-relief is compatible with VRT output : it uses the LUT
>> mechanism to do so.
>>
>
> OK, I will look at gdaldem, but not wanting to give up on this yet, is
> there a way to change the NoData pixels from -32767 to say -20 then scale
> 1.0 and offset the value by 10.
>
> This would make NoData=0
>
> old   => new
>  -5        5
>   0       10
>   1       11
> 100      110
>
> then I would have all positive values and the range would be manageable?
>
> The scale and offset are already supported in VRT, but I'm not sure if it
> is possible to remap -32767 values to a new value.
>
> Thanks for your response.
>
> -Steve
>
>
>
>>> So for example I get:
>>>
>>> Band 1 Block=256x256 Type=Float32, ColorInterp=Gray
>>>     Min=-32767.000 Max=33.980   Computed Min/Max=-54.985,34.320
>>>     Minimum=-32767.000, Maximum=33.980, Mean=-15887.345, StdDev=16386.592
>>>     256 buckets from -32831.1 to 98.0444:
>>>     1545169 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
>>> 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
>>> 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
>>> 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
>>> 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
>>> 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
>>> 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
>>> 0 0 0 0 0 0 27375 1924356
>>>     NoData Value=-32767
>>>     Metadata:
>>>       STATISTICS_MAXIMUM=33.979999542236
>>>       STATISTICS_MEAN=-15887.344614501
>>>       STATISTICS_MINIMUM=-32767
>>>       STATISTICS_STDDEV=16386.59208246
>>>
>>>
>>> It seems that the NoData value is included in the stats which really
>>> skews the numbers compressing all my values into two buckets! How can I
>>> avoid that? I want to ignore the NoData values and spread my good values
>>> into the remaining buckets. How can I do this?
>>>
>>
>> You can't. There's a ticket I think about the fact we should ignore nodata
>> value in stats.
>>
>>
>>> Thanks,
>>>     -Steve
>>>
>>> _______________________________________________
>>> gdal-dev mailing list
>>> gdal-dev at lists.osgeo.org
>>> http://lists.osgeo.org/mailman/listinfo/gdal-dev
>>>
>>
>>
> _______________________________________________
> gdal-dev mailing list
> gdal-dev at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/gdal-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20140515/d1808ef6/attachment-0001.html>


More information about the gdal-dev mailing list