4

I can retrieve and set a WMSLayer with the code below, but I'd like to pass some extra options to the server (in particular, to use a logscale and set a colorscale range). The WMSLayer constructor takes an options argument, but this is required to be a list of (unicode) strings, and I'm not able to set the values corresponding to arguments.
That is, I can pass options=['logscale'] (as below), but trying to set it to True or False (or "true" or "false") fail, with variants tried such as options=['logscale', 'true'] or options=['logscale=true'].

Examining the arguments sent to the server (the JS console will show errors) shows that "logscale" is used, but always set to be undefined. That is, something like, https://some-server.domain.tld/thredds/wms/somefile.nc?service=WMS&request=...&logscale=undefined&....

How do I pass values for the extra options in the WMSLayer constructor?
Is there indeed a way to get extra options with their values from ipyleaflet to the underlying leaflet.js?

import ipyleaflet
from owslib.wms import WebMapService

wms_url = "https://some-server.domain.tld/thredds/wms/somefile.nc"
wms_layers = list(WebMapService(wms_url).contents.keys())
wms = ipyleaflet.WMSLayer(url=wms_url, layers=wms_layers[0], transparent=True, 
                      format='image/png', opacity=0.33, options=['logscale'])

m = ipyleaflet.Map(zoom=3)
m.add_layer(wms)
# fetch map and display in Jupyter cell
m
9769953
  • 10,344
  • 3
  • 26
  • 37
  • Have you tried `options=[{'logscale': 'True'}]`? Or perhaps `options=[{'logscale': True}]`, `options=['logscale', True]` or `options=['logscale'=True]` Speaking off the cuff here, but if it's parsed in Python, perhaps the actual value True, rather than the string 'true' or 'True' is what you need to be passing in. – Chris Larson Feb 06 '19 at 06:23
  • @ChrisLarson Thanks for the response. Yes, I've tried several variants you suggest. The first two will raise an error (since the array elements have to be a unicode strings, not a dict), the third one results in URL parameters such as `...logscale=undefined&True=undefined&...`, and the fourth one isn't legal Python, and will just raise a `SyntaxError` (I didn't want to list all possible attempts and results in my question to not make it too cumbersome, but I've also tried for example `['logscale=true']`, which results in `...&logscale%3Dtrue=undefined&...`). – 9769953 Feb 06 '19 at 13:08

1 Answers1

5

You can subclass WMSLayer to add a new option like so:

from ipyleaflet import WMSLayer
from traitlets import Bool

class WMSLayerCustom (WMSLayer):
    # Additional options
    logscale = Bool(True).tag(sync=True, o=True)

Then instead of using ipyleaflet.WMSLayer, you use your subclass WMSLayerCustom:

wms = WMSLayerCustom(url=wms_url, layers=wms_layers[0], transparent=True, 
                       format='image/png', opacity=0.33, logscale=True)

and the resulting url will contain:

...&version=1.1.1&logscale=true&width=256&...


The options keyword argument isn't documented and it doesn't seem it can be used to set additional options.

I have also noted that any keyword argument is accepted in the constructor as the WMSLayer class inherits from HasTraits in the traitlets module. Setting the argument options=['logscale'] simply overrides the options attribute of the WMSLayer instance.

For example, if you call:

wms = ipyleaflet.WMSLayer(url=wms_url, layers=wms_layers[0], transparent=True, 
                  format='image/png', opacity=0.33, options=['max_zoom'])

You get an url with &layers=&...&transparent=false&... which are the default for the layer and transparent options, thereby ignoring layers=wms_layers[0] and transparent=True in the call to WMSLayer.

Jacques Gaudin
  • 15,779
  • 10
  • 54
  • 75
  • I am trying to reproduce your advice with my own WMS layer, but I can't get it to work - I opened an issue: https://github.com/jupyter-widgets/ipyleaflet/issues/746 on ipyleaflet seeking for help - I guess it should not be necessary to open a new SO question - but I can do that if another user can benefit fro it. Thanks for any advice. – epifanio Nov 13 '20 at 10:51
  • @epifanio I just had a looks the GH issue. I must admit I don't understand the issue you are having. Maybe a new question on SO would help? – Jacques Gaudin Nov 13 '20 at 11:33