Skip to content

Commit

Permalink
allow passing kwargs to WebMap services with m.add_wms.get_service()
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelquast committed Dec 4, 2023
1 parent eaeecf9 commit 140a7dc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
21 changes: 12 additions & 9 deletions eomaps/_webmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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(
Expand Down
13 changes: 11 additions & 2 deletions eomaps/webmap_containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
-------
Expand Down Expand Up @@ -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

0 comments on commit 140a7dc

Please sign in to comment.