Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/source/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ Result:
:height: 250px
:alt: WMS GetMap generated by OWSLib

To fetch the image with another HTTP client, ``buildGetMapURL`` takes the same parameters
and returns the URL without sending a request.

Comment thread
michaeldavie marked this conversation as resolved.
Resolved

WFS
---
Expand Down
14 changes: 14 additions & 0 deletions owslib/map/wms111.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,20 @@ def getmap(self, layers=None, styles=None, srs=None, bbox=None,
raise ServiceException(err_message)
return u

def buildGetMapURL(self, **kwargs):
"""Return the URL that getmap(method='Get') would request, without sending it.

Takes the same parameters as getmap(), except method and timeout.
"""
kwargs.setdefault('bgcolor', '#FFFFFF')
kwargs.setdefault('exceptions', 'application/vnd.ogc.se_xml')
try:
base_url = next((m.get('url') for m in self.getOperationByName('GetMap').methods
if m.get('type').lower() == 'get'))
except StopIteration:
base_url = self.url
return bind_url(base_url) + urlencode(self.__build_getmap_request(**kwargs))

def getfeatureinfo(self,
layers=None,
styles=None,
Expand Down
14 changes: 14 additions & 0 deletions owslib/map/wms130.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,20 @@ def getmap(self, layers=None,
raise ServiceException(err_message)
return u

def buildGetMapURL(self, **kwargs):
Comment thread
michaeldavie marked this conversation as resolved.
Resolved
"""Return the URL that getmap(method='Get') would request, without sending it.

Takes the same parameters as getmap(), except method and timeout.
"""
kwargs.setdefault('bgcolor', '#FFFFFF')
kwargs.setdefault('exceptions', 'XML')
try:
Comment thread
michaeldavie marked this conversation as resolved.
Resolved
base_url = next((m.get('url') for m in self.getOperationByName('GetMap').methods
if m.get('type').lower() == 'get'))
except StopIteration:
base_url = self.url
return bind_url(base_url) + urlencode(self.__build_getmap_request(**kwargs))

def getfeatureinfo(self, layers=None,
styles=None,
srs=None,
Expand Down
18 changes: 17 additions & 1 deletion tests/test_wms_getmap.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from unittest import mock

import pytest
from tests.utils import service_ok
from tests.utils import resource_file, service_ok

from owslib.wms import WebMapService
from owslib.map.wms130 import WebMapService_1_3_0
Expand Down Expand Up @@ -38,6 +38,22 @@ def test_build_getmap_request_bbox_precision(version):
assert request['bbox'] == ','.join(map(str, bbox_yx))


@pytest.mark.parametrize("version, caps", [("1.3.0", "wms_mesonet-caps-130.xml"), ("1.1.1", "wms_mesonet-caps.xml")])
def test_build_getmap_url(version, caps):
"""buildGetMapURL returns the URL getmap would request"""
with open(resource_file(caps), 'rb') as f:
wms = WebMapService(SERVICE_URL, version=version, xml=f.read())
params = dict(layers=['nexrad_base_reflect'], srs='EPSG:4326', bbox=(-126, 24, -66, 50),
size=(250, 250), format='image/png', time='2006-06-23T03:10:00Z')

response = mock.Mock()
response.info.return_value = {'Content-Type': 'image/png'}
with mock.patch(f"owslib.map.wms{version.replace('.', '')}.openURL", return_value=response):
wms.getmap(**params)

assert wms.buildGetMapURL(**params) == wms.request


@pytest.mark.online
@pytest.mark.skipif(not service_ok(SERVICE_URL),
reason="WMS service is unreachable")
Expand Down
Loading