Skip to content

Commit 2655ced

Browse files
authoredOct 18, 2016
Merge pull request #3595 from rldhont/fix_server_legend_layertitle
[BUGFIX][QGIS Server] GetLegendGraphic: if LAYERTITLE is false disable layer name in legend
2 parents 8c37370 + a51ef42 commit 2655ced

File tree

6 files changed

+87
-1
lines changed

6 files changed

+87
-1
lines changed
 

‎src/server/qgswmsserver.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,14 @@ QImage* QgsWmsServer::getLegendGraphics()
943943
legendNode->setUserLabel( " " ); // empty string = no override, so let's use one space
944944
}
945945
}
946+
else if ( !mDrawLegendLayerLabel )
947+
{
948+
Q_FOREACH ( QgsLayerTreeModelLegendNode* legendNode, legendModel.layerLegendNodes( nodeLayer ) )
949+
{
950+
if ( legendNode->isEmbeddedInParent() )
951+
legendNode->setEmbeddedInParent( false );
952+
}
953+
}
946954
}
947955
}
948956

‎tests/src/python/test_qgsserver.py

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,13 @@
2121
import email
2222
from io import StringIO
2323
from qgis.server import QgsServer
24-
from qgis.core import QgsMessageLog, QgsApplication
24+
from qgis.core import QgsMessageLog, QgsRenderChecker, QgsApplication
2525
from qgis.testing import unittest
26+
from qgis.PyQt.QtCore import QSize
2627
from utilities import unitTestDataPath
2728
import osgeo.gdal
29+
import tempfile
30+
import base64
2831

2932
# Strip path and content length because path may vary
3033
RE_STRIP_UNCHECKABLE = b'MAP=[^"]+|Content-Length: \d+'
@@ -431,6 +434,81 @@ def test_getLegendGraphics(self):
431434
self.assertEqual(-1, h.find(b'Content-Type: text/xml; charset=utf-8'), "Header: %s\nResponse:\n%s" % (h, r))
432435
self.assertNotEqual(-1, h.find(b'Content-Type: image/png'), "Header: %s\nResponse:\n%s" % (h, r))
433436

437+
def test_getLegendGraphics_layertitle(self):
438+
"""Test that does not return an exception but an image"""
439+
parms = {
440+
'MAP': self.testdata_path + "test_project.qgs",
441+
'SERVICE': 'WMS',
442+
'VERSION': '1.3.0',
443+
'REQUEST': 'GetLegendGraphic',
444+
'FORMAT': 'image/png',
445+
#'WIDTH': '20', # optional
446+
#'HEIGHT': '20', # optional
447+
'LAYER': u'testlayer%20èé',
448+
'LAYERTITLE': 'TRUE',
449+
}
450+
qs = '&'.join([u"%s=%s" % (k, v) for k, v in parms.items()])
451+
r, h = self._result(self.server.handleRequest(qs))
452+
self._img_diff_error(r, h, "WMS_GetLegendGraphic_test", 250, QSize(10, 10))
453+
454+
parms = {
455+
'MAP': self.testdata_path + "test_project.qgs",
456+
'SERVICE': 'WMS',
457+
'VERSION': '1.3.0',
458+
'REQUEST': 'GetLegendGraphic',
459+
'FORMAT': 'image/png',
460+
#'WIDTH': '20', # optional
461+
#'HEIGHT': '20', # optional
462+
'LAYER': u'testlayer%20èé',
463+
'LAYERTITLE': 'FALSE',
464+
}
465+
qs = '&'.join([u"%s=%s" % (k, v) for k, v in parms.items()])
466+
r, h = self._result(self.server.handleRequest(qs))
467+
self._img_diff_error(r, h, "WMS_GetLegendGraphic_test_layertitle_false", 250, QSize(10, 10))
468+
469+
def _result(self, data):
470+
headers = {}
471+
for line in data[0].decode('UTF-8').split("\n"):
472+
if line != "":
473+
header = line.split(":")
474+
self.assertEqual(len(header), 2, line)
475+
headers[str(header[0])] = str(header[1]).strip()
476+
477+
return data[1], headers
478+
479+
def _img_diff(self, image, control_image, max_diff, max_size_diff=QSize()):
480+
temp_image = os.path.join(tempfile.gettempdir(), "%s_result.png" % control_image)
481+
482+
with open(temp_image, "wb") as f:
483+
f.write(image)
484+
485+
control = QgsRenderChecker()
486+
control.setControlPathPrefix("qgis_server")
487+
control.setControlName(control_image)
488+
control.setRenderedImage(temp_image)
489+
if max_size_diff.isValid():
490+
control.setSizeTolerance(max_size_diff.width(), max_size_diff.height())
491+
return control.compareImages(control_image), control.report()
492+
493+
def _img_diff_error(self, response, headers, image, max_diff=10, max_size_diff=QSize()):
494+
self.assertEqual(
495+
headers.get("Content-Type"), "image/png",
496+
"Content type is wrong: %s" % headers.get("Content-Type"))
497+
test, report = self._img_diff(response, image, max_diff, max_size_diff)
498+
499+
with open(os.path.join(tempfile.gettempdir(), image + "_result.png"), "rb") as rendered_file:
500+
encoded_rendered_file = base64.b64encode(rendered_file.read())
501+
message = "Image is wrong\n%s\nImage:\necho '%s' | base64 -d >%s/%s_result.png" % (
502+
report, encoded_rendered_file.strip(), tempfile.gettempdir(), image
503+
)
504+
505+
with open(os.path.join(tempfile.gettempdir(), image + "_result_diff.png"), "rb") as diff_file:
506+
encoded_diff_file = base64.b64encode(diff_file.read())
507+
message += "\nDiff:\necho '%s' | base64 -d > %s/%s_result_diff.png" % (
508+
encoded_diff_file.strip(), tempfile.gettempdir(), image
509+
)
510+
511+
self.assertTrue(test, message)
434512

435513
if __name__ == '__main__':
436514
unittest.main()

0 commit comments

Comments
 (0)
Please sign in to comment.