Search code examples
gdalgeoservergeotiff

Converting 1-band DEM GeoTiff to 3-band (RGB) GeoTiff with storing full elevation value in RBG + GeoServer ready


My goal is to host tiles with elevation values in GeoServer.

I downloaded the mapzen tiles (link here: https://www.opentopodata.org/datasets/mapzen/)

Originally downloaded for use in opentopodata. I set up everything and launched it successfully.

But now I want to use the same GeoTIFF with GeoServer. The problem is that if you just put this GeoTIFF in a layer, then GeoServer cannot return the tile and writes an error in the log. As I understand it, GeoServer cannot work with GeoTIFF, which has 1-band and values in which exceed one byte per band as in RGB.

As a result, I came up with the idea to convert these 1-band Geotiff to RGB. I want to encode the full elevation value in RGB.

The essence of the transformation is as follows: RGB (R = 1 byte) (G = 1 byte)(B = 1 byte)

  • zero level elevation is (0 0 0) RGB
  • 1 meter of elevation is (0 0 1) RGB
  • 256 meters elevation is (0 1 0) RGB
  • 10000 meters of elevation is (0 39 16) RBG

https://www.checkyourmath.com/convert/color/decimal_rgb.php

I tried using gdaldem color-relief and at first this utility seemed like a salvation, but in fact it turned out that this utility is not designed to accurately replace colors, it does it only approximately, and loses colors that go in order. It works more or less normally with a set of brightly highlighted colors, but at the same time the idea is lost that you can simply calculate the value of the elevation by the RGB value, which goes linearly according to the above.

Since gdaldem color-relief did not justify itself for the task described above, unfortunately, I am thinking of writing a utility that will replace each pixel in the original GeoTIFF (1-band) with RGB with a lift value encrypted in 3 bytes. But I don't know how to approach it yet.


Solution

  • As often happens, I wrote a topic in the forum and almost immediately found the answer.

    Solution: still gdaldem color-relief. But exclude interpolation, which loses a lot of data.

    1. Take information from GeoTiff using gdalinfo -hist.
    2. Read Min= and Max= from the console response. This will be the minimum and maximum height of the points in this file.
    3. Generate ramp.txt by going through all the heights from min to max, and for each height determine the color (linearly with translation to rgb, as described in the question above)
    4. Save the result in a text file
    5. Use the text file in the gdaldem color-relief command.

    As a result, everything turns out clear and all the colors are in place.