From 140a7dc381c308f4071b02037a833118f185c02a Mon Sep 17 00:00:00 2001 From: Raphael Quast Date: Fri, 1 Dec 2023 19:06:51 +0100 Subject: [PATCH] allow passing kwargs to WebMap services with m.add_wms.get_service() --- eomaps/_webmap.py | 21 ++++++++++++--------- eomaps/webmap_containers.py | 13 +++++++++++-- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/eomaps/_webmap.py b/eomaps/_webmap.py index 8934b50da..3d7dccebe 100644 --- a/eomaps/_webmap.py +++ b/eomaps/_webmap.py @@ -584,12 +584,15 @@ def _do_add_layer(self, m, layer, **kwargs): class _WebServiceCollection: - def __init__(self, m, service_type="wmts", url=None): + def __init__(self, m, service_type="wmts", url=None, **kwargs): self._m = m self._service_type = service_type if url is not None: self._url = url + # additional kwargs that will be passed to owslib.WebMapService() + self._service_kwargs = kwargs.copy() + def __getitem__(self, key): return self.add_layer.__dict__[key] @@ -626,28 +629,28 @@ def findlayer(self, name): return [i for i in self.layers if name.lower() in i.lower()] @staticmethod - def _get_wmts(url): + def _get_wmts(url, **kwargs): # TODO expose useragent # lazy import used to avoid long import times from owslib.wmts import WebMapTileService - return WebMapTileService(url) + return WebMapTileService(url, **kwargs) @staticmethod - def _get_wms(url): + def _get_wms(url, **kwargs): # TODO expose useragent # lazy import used to avoid long import times from owslib.wms import WebMapService - return WebMapService(url) + return WebMapService(url, **kwargs) @property @lru_cache() def add_layer(self): if self._service_type == "wmts": - wmts = self._get_wmts(self._url) + wmts = self._get_wmts(self._url, **self._service_kwargs) layers = dict() for key in wmts.contents.keys(): layername = _sanitize(key) @@ -665,7 +668,7 @@ def add_layer(self): layers[layername] = wmtslayer elif self._service_type == "wms": - wms = self._get_wms(self._url) + wms = self._get_wms(self._url, **self._service_kwargs) layers = dict() for key in wms.contents.keys(): layername = _sanitize(key) @@ -821,14 +824,14 @@ def _fetch_layers(self): url = self._url if url is not None: if self._service_type == "wms": - wms = self._get_wms(url) + wms = self._get_wms(url, **self._service_kwargs) layer_names = list(wms.contents.keys()) for lname in layer_names: self._layers["layer_" + _sanitize(lname)] = _WMSLayer( self._m, wms, lname ) elif self._service_type == "wmts": - wmts = self._get_wmts(url) + wmts = self._get_wmts(url, **self._service_kwargs) layer_names = list(wmts.contents.keys()) for lname in layer_names: self._layers["layer_" + _sanitize(lname)] = _WMTSLayer( diff --git a/eomaps/webmap_containers.py b/eomaps/webmap_containers.py index 8162b2a5c..112970c4d 100644 --- a/eomaps/webmap_containers.py +++ b/eomaps/webmap_containers.py @@ -2422,7 +2422,9 @@ def Austria(self): WMS.__doc__ = type(self).Austria.__doc__ return WMS - def get_service(self, url, service_type="wms", rest_API=False, maxzoom=19): + def get_service( + self, url, service_type="wms", rest_API=False, maxzoom=19, **kwargs + ): """ Get a object that can be used to add WMS, WMTS or XYZ services based on a GetCapabilities-link or a link to a ArcGIS REST API @@ -2470,6 +2472,11 @@ def get_service(self, url, service_type="wms", rest_API=False, maxzoom=19): The maximum zoom-level available (to avoid http-request errors) for too high zoom levels. The default is 19. + kwargs : + Additional keyword arguments passed to `owslib.WebMapService()`. + (only relevant if type is "wms" or "wmts") + + For example: `version=1.3.0` Returns ------- @@ -2541,6 +2548,8 @@ def get_service(self, url, service_type="wms", rest_API=False, maxzoom=19): service_type=service_type, ) else: - service = _WebServiceCollection(self._m, service_type="wms", url=url) + service = _WebServiceCollection( + self._m, service_type="wms", url=url, **kwargs + ) return service