Skip to content

Commit

Permalink
Serialize color table dict as yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
weiji14 committed Nov 6, 2021
1 parent 4ebf216 commit 5b07d7f
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 3 deletions.
3 changes: 2 additions & 1 deletion rioxarray/_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import numpy as np
import rasterio
import yaml
from packaging import version
from rasterio.errors import NotGeoreferencedWarning
from rasterio.vrt import WarpedVRT
Expand Down Expand Up @@ -535,7 +536,7 @@ def _get_rasterio_attrs(riods):
if hasattr(riods, "colormap"):
# A dict containing the colormap for a band
try:
attrs["colormap"] = riods.colormap(1)
attrs["colormap"] = yaml.safe_dump(riods.colormap(1))
except ValueError: # NULL color table
pass
if hasattr(riods, "colorinterp") and any(riods.colorinterp):
Expand Down
3 changes: 2 additions & 1 deletion rioxarray/raster_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import numpy
import rasterio
import yaml
from rasterio.windows import Window
from xarray.conventions import encode_cf_variable

Expand Down Expand Up @@ -60,7 +61,7 @@ def _write_metatata_to_raster(raster_handle, xarray_dataset, tags):
# see https://rasterio.readthedocs.io/en/latest/topics/color.html
try:
raster_handle.colorinterp = tags["colorinterp"]
colormap = tags["colormap"]
colormap = yaml.safe_load(tags["colormap"])
raster_handle.write_colormap(1, colormap)
except KeyError:
pass
Expand Down
2 changes: 1 addition & 1 deletion test/integration/test_integration__io.py
Original file line number Diff line number Diff line change
Expand Up @@ -1052,7 +1052,7 @@ def test_geotiff_attr_loading(open_rasterio):
assert rds.shape == (1, 180, 360)
# assert rds.attrs["scale_factor"] == 1.0
# assert rds.attrs["add_offset"] == 0.0
assert isinstance(rds.attrs["colormap"], dict)
assert isinstance(rds.attrs["colormap"], str)
assert rds.attrs["colorinterp"] == (rasterio.enums.ColorInterp.palette,)


Expand Down

0 comments on commit 5b07d7f

Please sign in to comment.