3

I have singleband raster with min 0 and max 0.82 values. For raster I applied next color scheme in QGIS:

enter image description here enter image description here

I want colorize raster using GDAL command, but receive raster with another color: enter image description here

GDAL command:

gdaldem color-relief -nearest_color_entry -alpha input.tif color.txt output.tif

Example color.txt file:

0.18 215 25 28 255
0.36 253 174 97 255
0.53 255 255 192 255
0.71 166 217 106 255
0.89 26 150 65 255
nv 0 0 0 0

How I can create colorized raster like in QGIS (no manually in QGIS or other desktop app. I can use only command line tool (python script or GDAL command. Color.txt file I can generate automatically))?

1 Answer 1

3

The main problem you are experiencing is related to the difference between the color palettes you are applying within QGIS and gdaldem color-relief. The use of the -nearest_color_entry flag makes the 'raster value' specification in colors.txt not behave like a lower or upper bound, but more like a midpoint in that band of the color bar. You have a few options going forward:

(1) Adjust the 'raster values' in colors.txt to be midpoints (less desirable).

(2) Add more rows to colors.txt and set two values for each color, lower (inclusive) and upper (exclusive) bounds. It would look like:

0.0000000001 215 25 28 255
0.1799999999 215 25 28 255
0.18 253 174 97 255
0.3599999999 253 174 97 255
0.36 255 255 192 255
0.5299999999 255 255 192 255
0.53 166 217 106 255
0.7099999999 166 217 106 255
0.71 26 150 65 255
nv 0 0 0 0

You would still use the -nearest_color_entry option in this case.

(3) Use a different type of color table, namely a GMT .cpt as permitted for GDAL > 1.8.0. Such a CPT file would look like:

# COLOR_MODEL = RGB
0 000 000 000 0 000 000 000
0 215 025 028 0.18 215 025 028
0.18 253 174 097 0.36 253 174 097
0.36 255 255 192 0.53 255 255 192
0.53 166 217 106 0.71 166 217 106
0.71 026 150 065 0.89 026 150 065

The syntax of this CPT file is

low_bound RRR GGG BBB high_bound RRR GGG BBB

with an interpolation between the low_bound and high_bound. Setting the colors the same makes it a block of color. No need to use -nearest_color_entry here. Your nodata value of 0 explains the first entry.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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