Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

generating GeoTIFF colormaps

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.

like image 323
EBo Avatar asked Sep 20 '18 13:09

EBo


1 Answers

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)
like image 91
David Bernat Avatar answered Oct 16 '22 07:10

David Bernat