gpt4 book ai didi

python - 如何在 ipyleaflet 中为 WMS 层设置额外选项?

转载 作者:太空宇宙 更新时间:2023-11-03 14:39:27 25 4
gpt4 key购买 nike

我可以使用下面的代码检索和设置 WMSLayer,但我想将一些额外的选项传递给服务器(特别是使用对数标度并设置颜色标度范围)。 WMSLayer 构造函数采用 options 参数,但这必须是 (unicode) 字符串列表,而且我无法设置与参数对应的值。
也就是说,我可以传递 options=['logscale'](如下所示),但尝试将其设置为 True 或 False(或“true”或“false”)失败,尝试了这样的变体作为 options=['logscale', 'true']options=['logscale=true']

检查发送到服务器的参数(JS 控制台将显示错误)表明使用了“logscale”,但始终设置为未定义。也就是说,类似于 https://some-server.domain.tld/thredds/wms/somefile.nc?service=WMS&request=...&logscale=undefined&...

如何在 WMSLayer 构造函数中传递额外选项的值?
是否确实有一种方法可以从 ipyleaflet 到底层的 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

最佳答案

您可以继承 WMSLayer 以添加新选项,如下所示:

from ipyleaflet import WMSLayer
from traitlets import Bool

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

然后不使用 ipyleaflet.WMSLayer,而是使用子类 WMSLayerCustom:

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

生成的 url 将包含:

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


options 关键字参数没有记录,而且它似乎不能用于设置其他选项。

我还注意到构造函数中接受任何关键字参数,因为 WMSLayer 类继承自 traitlets 模块中的 HasTraits。设置参数 options=['logscale'] 只是覆盖 WMSLayer 实例的 options 属性。

例如,如果您调用:

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

你得到一个带有 &layers=&...&transparent=false&... 的 url,这是 layertransparent 的默认值选项,从而在调用 WMSLayer 时忽略 layers=wms_layers[0]transparent=True

关于python - 如何在 ipyleaflet 中为 WMS 层设置额外选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54482665/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com