Properly set color interpretation after writing a colormap (#3136)

* Properly set color interpretation after writing a colormap

Resolves #3133

* Appease flake8
This commit is contained in:
Sean Gillies 2024-08-12 15:45:19 -06:00 committed by GitHub
parent 01abf63156
commit a467cbd786
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 23 additions and 16 deletions

View File

@ -1,6 +1,14 @@
Changes
=======
Next (TBD)
----------
Bug fixes:
- Color interpretation is correctly set to "palette" after a colormap is
written (#3133).
1.4b1 (2024-08-10)
------------------

View File

@ -2036,7 +2036,7 @@ cdef class DatasetWriterBase(DatasetReaderBase):
GDALSetColorEntry(hTable, i, &color)
# TODO: other color interpretations?
GDALSetRasterColorInterpretation(hBand, <GDALColorInterp>1)
GDALSetRasterColorInterpretation(hBand, GCI_PaletteIndex)
GDALSetRasterColorTable(hBand, hTable)
finally:

View File

@ -1,27 +1,26 @@
"""Colormap tests."""
import rasterio
from rasterio.enums import ColorInterp
def test_write_colormap_warn(tmpdir, recwarn):
with rasterio.open('tests/data/shade.tif') as src:
profile = src.meta
tiffname = str(tmpdir.join('foo.tif'))
with rasterio.open(tiffname, 'w', **profile) as dst:
dst.write_colormap(1, {0: (255, 0, 0, 255), 255: (0, 0, 0, 0)})
def test_write_colormap(tmpdir):
with rasterio.open('tests/data/shade.tif') as src:
def test_write_colormap(tmp_path):
with rasterio.open("tests/data/shade.tif") as src:
shade = src.read(1)
meta = src.meta
tiffname = str(tmpdir.join('foo.png'))
meta['driver'] = 'PNG'
with rasterio.open(tiffname, 'w', **meta) as dst:
profile = src.profile
profile["driver"] = "PNG"
with rasterio.open(tmp_path / "test.tif", "w", **profile) as dst:
dst.write(shade, indexes=1)
dst.write_colormap(1, {0: (255, 0, 0, 255), 255: (0, 0, 0, 0)})
assert dst.colorinterp == (ColorInterp.palette,)
cmap = dst.colormap(1)
assert cmap[0] == (255, 0, 0, 255)
assert cmap[255] == (0, 0, 0, 0)
with rasterio.open(tiffname) as src:
with rasterio.open(tmp_path / "test.tif") as src:
assert src.colorinterp == (ColorInterp.palette,)
cmap = src.colormap(1)
assert cmap[0] == (255, 0, 0, 255)
assert cmap[255] == (0, 0, 0, 0)