Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to describe color interpretation #191

Open
atharen opened this issue Nov 24, 2020 · 5 comments
Open

Add ability to describe color interpretation #191

atharen opened this issue Nov 24, 2020 · 5 comments
Labels
proposal Idea for a new feature.

Comments

@atharen
Copy link

atharen commented Nov 24, 2020

Ability to handle color interpretation as enabled by Rasterio

Primarily ability to read existing color with Dataset.colorinterp, return the sequence of ColorInterp.<enum>, and the modify a Dataset by overriding it, similar to how descriptions work.

Put example of opening a dataset in r+ just to change description of bands and their respective color implementations:

with rasterio.open(dataset_path, 'r+') as src:
    src.descriptions = band_descriptions
    src.colorinterp = [rasterio.enums.ColorInterp.gray] * len(band_descriptions)

Probably a minor priority issue given that if you have rioxarray you have rasterio as well, but combining them both might be more readable/polished.

To be honest, have not looked as the source code to see if it is easy to implement, but would consider doing it if relatively minor.

For reference, I assume it would be similar for how descriptions work, by modifying the xarray attrs['long_name'].

Not sure if an alternative approach exists (recently started using rioxarray).

Could not find a related issue in either the issues page nor on gis stackexchange

@atharen atharen added the proposal Idea for a new feature. label Nov 24, 2020
@snowman2
Copy link
Member

snowman2 commented Nov 24, 2020

When reading the raster, the photometric option might be available in the profile and you could add that to the attributes.
Then, you could add a method to convert the rasterio.enums.ColorInterp to a photometric option and write that attribute to the xarray object's attributes and update to_raster to look for that.

Without modifying rioxarray, you can use the photometric option when writing to raster.

rds.rio.to_raster(...,photometric="RGB")

Otherwise, you will have to use rasterio to update the colorinterp as rioxarray doesn't support the update mode.

@snowman2
Copy link
Member

Another option would be to add a colorinterp kwarg to to_raster:

rds.rio.to_raster(...,colorinterp=[rasterio.enums.ColorInterp.gray] * len(band_descriptions))

@weiji14
Copy link

weiji14 commented Oct 1, 2021

Hi there 👋, I'm interested in adding the color interpretation feature too. We're thinking of integrating rioxarray in PyGMT (see thread at GenericMappingTools/pygmt#1555) and would be keen to help out 😄

If it's just a matter of applying pydata/xarray#3136 to the _get_rasterio_attrs function, I could probably open a quick PR for this

But to open a can of worms, is it ok to just store the color table in the xarray.DataArray's attr? Or should the RGB colors be read as separate bands? Just thinking how it will work with plotting the image later using .plot.imshow() (e.g. with the https://corteva.github.io/rioxarray/latest/examples/COG.html example).

@snowman2
Copy link
Member

snowman2 commented Oct 1, 2021

Hi @weiji14, it would definitely be great if you would like to add the functionality.

The main changes that need to happen are:

  1. Load in the attributes when reading a file from rasterio ref. These will need to be converted into string/numeric types as necessary. This is so you can call .to_netcdf() without serialization issues.
  2. Write the attributes to the file ref. These will need to be converted to rasterio types as necessary.

If you are feeling extra motivated, you could add methods to the rio accessor to read/write color interpretation to the xarray object.

@snowman2
Copy link
Member

snowman2 commented Oct 1, 2021

Side note, to plot RGB bands, here is an example ref

rds.astype("int").plot.imshow(rgb="band")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
proposal Idea for a new feature.
Projects
None yet
Development

No branches or pull requests

3 participants