Skip to content

Commit

Permalink
fix: always use wms url for legend graphic
Browse files Browse the repository at this point in the history
  • Loading branch information
simonseyock committed Jul 17, 2024
1 parent 99644d3 commit 546202f
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 8 deletions.
10 changes: 6 additions & 4 deletions mapproxy/config/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import warnings
from copy import deepcopy, copy
from functools import partial
from packaging.version import Version

import logging
from urllib.parse import urlparse
Expand Down Expand Up @@ -1952,10 +1953,11 @@ def tile_layers(self, grid_name_as_path=False):
md['extent'] = extent
legend_version = None
if 'legendurl' in self.conf:
wms_conf = self.context.services.conf.get('wms', None)
# GetLegendGraphic with legendurl does not seem to work in non 1.3.0 versions
if wms_conf is not None and '1.3.0' in wms_conf.get('versions', ['1.3.0']):
legend_version = '1.3.0'
wms_conf = self.context.services.conf.get('wms')
if wms_conf is not None:
versions = wms_conf.get('versions', ['1.3.0'])
versions.sort(key=Version)
legend_version = versions[-1]
tile_layers.append(
TileLayer(
self.conf['name'], self.conf['title'],
Expand Down
2 changes: 1 addition & 1 deletion mapproxy/service/templates/wmts100capabilities.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
{{if layer.legend_version}}
<LegendURL
format="image/png"
xlink:href="{{service.url}}?service=WMS&amp;request=GetLegendGraphic&amp;version={{layer.legend_version}}&amp;format=image%2Fpng&amp;layer={{layer.name}}"
xlink:href="{{wms_service_url}}?service=WMS&amp;request=GetLegendGraphic&amp;version={{layer.legend_version}}&amp;format=image%2Fpng&amp;layer={{layer.name}}"
/>
{{endif}}
</Style>
Expand Down
11 changes: 8 additions & 3 deletions mapproxy/service/wmts.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"""
from __future__ import print_function

import re
from functools import partial

from mapproxy.request.wmts import (
Expand Down Expand Up @@ -273,11 +274,13 @@ def render(self, _map_request):
return self._render_template(_map_request.capabilities_template)

def template_context(self):
return dict(service=bunch(default='', **self.service),
service = bunch(default='', **self.service)
return dict(service=service,
restful=False,
layers=self.layers,
info_formats=self.info_formats,
tile_matrix_sets=self.matrix_sets)
tile_matrix_sets=self.matrix_sets,
wms_service_url=service.url)

def _render_template(self, template):
template = get_template(template)
Expand All @@ -294,7 +297,8 @@ def __init__(self, server_md, layers, matrix_sets, url_converter, fi_url_convert
self.fi_url_converter = fi_url_converter

def template_context(self):
return dict(service=bunch(default='', **self.service),
service = bunch(default='', **self.service)
return dict(service=service,
restful=True,
layers=self.layers,
info_formats=self.info_formats,
Expand All @@ -306,6 +310,7 @@ def template_context(self):
dimension_keys=dict((k.lower(), k) for k in self.url_converter.dimensions),
format_resource_template=format_resource_template,
format_info_resource_template=format_info_resource_template,
wms_service_url=re.sub(r'wmts$', 'service', service.url)
)


Expand Down
2 changes: 2 additions & 0 deletions mapproxy/test/system/fixture/legendgraphic.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ services:
access_constraints:
Here be dragons.
wmts:
restful: true
kvp: true

layers:
- name: wms_legend
Expand Down
17 changes: 17 additions & 0 deletions mapproxy/test/system/test_legendgraphic.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import json

from mapproxy.compat.image import Image
from mapproxy.request.base import BaseRequest
from mapproxy.request.wms import (
WMS111MapRequest,
WMS111CapabilitiesRequest,
Expand Down Expand Up @@ -70,6 +71,9 @@ def setup_method(self):
url="/service?",
param=dict(service="WMTS", version="1.0.0", request="GetCapabilities"),
)
self.common_wmts_rest_cap_req = BaseRequest(
url="/wmts/1.0.0/WMTSCapabilities.xml"
)

# test_00, test_01, test_02 need to run first in order to run the other tests properly
def test_00_get_legendgraphic_multiple_sources_111(self, app):
Expand Down Expand Up @@ -325,3 +329,16 @@ def test_wmts_legend_url(self, app):
)
assert xml.xpath('count(//wmts:Layer)', namespaces=ns_wmts) == 2
assert xml.xpath('count(//wmts:LegendURL)', namespaces=ns_wmts) == 1

def test_wmts_rest_legend_url(self, app):
resp = app.get(self.common_wmts_rest_cap_req)
assert resp.content_type == "application/xml"
xml = resp.lxml
assert_xpath_wmts(
xml,
'//wmts:Layer[1]/wmts:Style/wmts:LegendURL/@xlink:href',
'http://localhost/service?service=WMS&request=GetLegendGraphic&version=1.3.0&format=image%2Fpng'
'&layer=wmts_layer_legendurl'
)
assert xml.xpath('count(//wmts:Layer)', namespaces=ns_wmts) == 2
assert xml.xpath('count(//wmts:LegendURL)', namespaces=ns_wmts) == 1

0 comments on commit 546202f

Please sign in to comment.