diff --git a/rasterio/_io.pyx b/rasterio/_io.pyx index f7291165..3ce31a60 100644 --- a/rasterio/_io.pyx +++ b/rasterio/_io.pyx @@ -1680,14 +1680,26 @@ cdef class RasterUpdater(RasterReader): # GPI_Gray=0, GPI_RGB=1, GPI_CMYK=2, GPI_HLS=3 hTable = _gdal.GDALCreateColorTable(1) vals = range(256) + for i, rgba in colormap.items(): + + if len(rgba) == 4 and self.driver in ('GTiff'): + raise ValueError( + "Format '%s' doesn't support 4 component colormap entries" + % self.driver) + + elif len(rgba) == 3: + rgba = tuple(rgba) + (255,) + if i not in vals: log.warn("Invalid colormap key %d", i) continue + color.c1, color.c2, color.c3, color.c4 = rgba _gdal.GDALSetColorEntry(hTable, i, &color) + # TODO: other color interpretations? - _gdal.GDALSetRasterColorInterpretation(hBand, 2) + _gdal.GDALSetRasterColorInterpretation(hBand, 1) _gdal.GDALSetRasterColorTable(hBand, hTable) _gdal.GDALDestroyColorTable(hTable) @@ -1696,9 +1708,9 @@ cdef class RasterUpdater(RasterReader): mask. The optional `window` argument takes a tuple like: - + ((row_start, row_stop), (col_start, col_stop)) - + specifying a raster subset to write into. """ cdef void *hband diff --git a/tests/test_colormap.py b/tests/test_colormap.py index e5899fba..112ebfad 100644 --- a/tests/test_colormap.py +++ b/tests/test_colormap.py @@ -7,6 +7,21 @@ import rasterio logging.basicConfig(stream=sys.stderr, level=logging.DEBUG) + +def test_write_colormap_err(tmpdir): + + with rasterio.drivers(): + + with rasterio.open('tests/data/shade.tif') as src: + meta = src.meta + + tiffname = str(tmpdir.join('foo.tif')) + + with rasterio.open(tiffname, 'w', **meta) as dst: + with pytest.raises(ValueError): + dst.write_colormap(1, {0: (255, 0, 0, 255), 255: (0, 0, 0, 0)}) + + def test_write_colormap(tmpdir): with rasterio.drivers(): @@ -15,19 +30,20 @@ def test_write_colormap(tmpdir): shade = src.read_band(1) meta = src.meta - tiffname = str(tmpdir.join('foo.tif')) - + tiffname = str(tmpdir.join('foo.png')) + meta['driver'] = 'PNG' + with rasterio.open(tiffname, 'w', **meta) as dst: dst.write_band(1, shade) - dst.write_colormap(1, {0: (255, 0, 0, 255), 255: (0, 0, 255, 255)}) + dst.write_colormap(1, {0: (255, 0, 0, 255), 255: (0, 0, 0, 0)}) cmap = dst.colormap(1) assert cmap[0] == (255, 0, 0, 255) - assert cmap[255] == (0, 0, 255, 255) + assert cmap[255] == (0, 0, 0, 0) with rasterio.open(tiffname) as src: cmap = src.colormap(1) assert cmap[0] == (255, 0, 0, 255) - assert cmap[255] == (0, 0, 255, 255) + assert cmap[255] == (0, 0, 0, 0) # subprocess.call(['open', tiffname])