generating GeoTIFF colormaps
Asked Answered
H

2

6

I am generating 2D statistical maps and would like to also generate and save the image colormaps. The mean-size image has a data type of float32. The following example modified from online sources:

with rasterio.open(name,'w',**profile) as dst:
    dst.write(data.astype(np.float32), 1)
    dst.write_colormap(
        1, {
            0: (255, 0, 0, 255),
            255: (0, 0, 255, 255) })
    cmap = dst.colormap(1)
    print (cmp)

Does not work, and the call to colormap(1) returns a NULL table and terminates the program.

Changing the data type to uint8 or uint16, it works after a fashion, but I have been unable to find examples on how to change the color tables and maps for floating point and integer data types.

Could someone provide a snippet that shows how to generate different colormaps, colorinterps and/or color pallets for floating point images? While I am currently using rasterio, I can also convert this GDAL if someone can post a solution.

Histoid answered 20/9, 2018 at 13:4 Comment(0)
S
2

The reason why your program crashes is almost certainly because you are reading the file while it is still open. The format and syntax of your color table is correct. The following code will exit the first context manager to close the file, then reopen. Please try:

with rasterio.open(name,'w',**profile) as dst:
    dst.write(data.astype(np.float32), 1)
    dst.write_colormap(
        1, {
            0: (255, 0, 0, 255),
            255: (0, 0, 255, 255) })

with rasterio.open(name) as dst:
    cmap = dst.colormap(1)
    print (cmp)
Shiv answered 14/2, 2020 at 19:10 Comment(1)
I no longer have access to that work project, so cannot check the answer against the original problem. I will see if I can reproduce the problem elsewhere, but that will take some time. As a note, I gave you a +1 because this is one of the first leads I've had to solve the problem in a year and a half. Thank you.Histoid
D
0

To this point, it is not possible to store a color table for float values, neither with rasterio nor gdal. The write_colormap method of rasterio only targets the uint8 datatype (uint16 can be used, but values above 255 are skipped; tested with rasterio 1.2.10). A feasable way to store color information is probably to provide an external .sld styling file, and define value ranges for the float values. In case you use QGIS for visualization, you can add a .qld file with the same filename as the raster, which will be loaded and interpreted when opening the file in QGIS.

Debra answered 21/2, 2022 at 8:13 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Greatgrandaunt

© 2022 - 2024 — McMap. All rights reserved.