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

Support for custom colormaps #223

Merged
merged 8 commits into from
Oct 4, 2024
Merged

Conversation

lopezvoliver
Copy link
Contributor

@lopezvoliver lopezvoliver commented Sep 12, 2024

This pull request adds support for custom colormaps (resolves #200), either as a matplotlib Colormap, or as a list of colors.

Hopefully I got it right, so far my tests (with leafmap.add_raster) seem to work:

Test using leafmap add_raster

Here's a test using the example from opengeos/leafmap#673

Test using leafmap add_raster

@giswqs
Copy link
Contributor

giswqs commented Sep 12, 2024

This would be a nice addition. Great work!

@banesullivan
Copy link
Owner

Fantastic! I'll give this a test tonight and see if I can write a few tests. Thank you so much for contributing this!

@codecov-commenter
Copy link

codecov-commenter commented Sep 13, 2024

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

Attention: Patch coverage is 77.77778% with 6 lines in your changes missing coverage. Please review.

Project coverage is 83.74%. Comparing base (33838b3) to head (04f4cd5).

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #223      +/-   ##
==========================================
- Coverage   83.93%   83.74%   -0.19%     
==========================================
  Files          26       26              
  Lines        1021     1046      +25     
==========================================
+ Hits          857      876      +19     
- Misses        164      170       +6     

@banesullivan
Copy link
Owner

This isn't quite working as expected for colormaps. I'm eager to incorporate this but we'll have to iterate on the serialization of the colormaps to account for different types

@lopezvoliver
Copy link
Contributor Author

This isn't quite working as expected for colormaps. I'm eager to incorporate this but we'll have to iterate on the serialization of the colormaps to account for different types

Can you share a couple of examples? I can give it a try

@banesullivan
Copy link
Owner

Sorry for my short comment, I'm unable to take a look immediately, but I was trying to do something like the following:

from localtileserver import TileClient, get_leaflet_tile_layer, examples
from matplotlib.colors import ListedColormap, Colormap
from ipyleaflet import Map

tile_client = examples.get_bahamas(debug=True)

cmap1 = ListedColormap(['red','blue'],name='test_1')
cmap2 = ListedColormap(['blue','green'],name='test_2')
cmap3 = ListedColormap(['green','red'],name='test_3')

t = get_leaflet_tile_layer(tile_client, colormap=cmap1)
# t = get_leaflet_tile_layer(tile_client, colormap=[cmap2,cmap1,cmap3])

m = Map(center=tile_client.center(), zoom=tile_client.default_zoom)
m.add_layer(t)
m

@lopezvoliver
Copy link
Contributor Author

@banesullivan can you explain a bit more what's supposed to happen with colormap in this line? is it going to be a concatenation of the three colormaps?

t = get_leaflet_tile_layer(tile_client, colormap=[cmap2,cmap1,cmap3])

What about the example with the single colormap? is that one working as expected?

@banesullivan
Copy link
Owner

can you explain a bit more what's supposed to happen with colormap in this line? is it going to be a concatenation of the three colormaps?

Sorry for my delay! Let's ignore this actually. this was a supported feature when the underlying tile server used to be large-image where it had some rather unique capabilities for color mapping individual bands. I don't think we need to support that here.

@banesullivan
Copy link
Owner

I've gone ahead and fixed the remaining issues in order to use ListedColormap and LinearSegmentedColormap instances as well. I think this is good to merge at this point. @lopezvoliver or @giswqs, do you want to make sure that this works with leafmap before I merge/release?

@banesullivan banesullivan changed the title Support for custom colormaps #200 Support for custom colormaps Oct 4, 2024
@banesullivan
Copy link
Owner

I think its worth noting that in this current implementation, there is a limit to how big these colormaps can be as we embed them in the URL parameters. I recommend just creating named colormaps and registering them with matplotlib in your environment to get around this so that they can be referred to by name, ref: https://matplotlib.org/stable/users/explain/colors/colormap-manipulation.html#registering-a-colormap

@banesullivan banesullivan merged commit 130087c into banesullivan:main Oct 4, 2024
8 checks passed
@giswqs
Copy link
Contributor

giswqs commented Oct 4, 2024

I tested it. Custom colormaps in the format of ListedColormap and list of colors (e.g., ['red', 'green']) work well with leafmap.

Registering the custom colormap with matplotlib does not seem to work. Anyway, this is not a big issue. No worries if you don't have time look into it now.

https://colab.research.google.com/drive/1JPgl3O-u1ufDEwb3f8fWxiXPqOQyPER7?usp=sharing

@banesullivan
Copy link
Owner

Registering the custom colormap with matplotlib does not seem to work

Whoops, I thought this would. I'll try to make sure this gets fixed

@nve-sak
Copy link

nve-sak commented Nov 11, 2024

FWIW, I added a failing test case here: opengeos/leafmap#966. No crash or error, but the colors are not as expected.

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

Successfully merging this pull request may close these issues.

Support custom colormaps
5 participants