From 18f4238a1072275aceb7eaf9baa22a1399c7c3c1 Mon Sep 17 00:00:00 2001 From: rldhont Date: Mon, 10 Oct 2016 17:46:46 +0200 Subject: [PATCH] [BUGFIX][QGIS Server] GetLegendGraphic: if LAYERTITLE is false disable layer name in legend If the layer has only one legend node, it is embedded in parent. In QGIS Server the user can specify no layer title, so the layer title has not be displayed. --- src/server/qgswmsserver.cpp | 8 ++ tests/src/python/test_qgsserver.py | 80 +++++++++++++++++- .../WMS_GetLegendGraphic_test.png | Bin 0 -> 2962 bytes .../WMS_GetLegendGraphic_test_mask.png | Bin 0 -> 265 bytes ...GetLegendGraphic_test_layertitle_false.png | Bin 0 -> 355 bytes ...gendGraphic_test_layertitle_false_mask.png | Bin 0 -> 166 bytes 6 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 tests/testdata/control_images/qgis_server/WMS_GetLegendGraphic_test/WMS_GetLegendGraphic_test.png create mode 100644 tests/testdata/control_images/qgis_server/WMS_GetLegendGraphic_test/WMS_GetLegendGraphic_test_mask.png create mode 100644 tests/testdata/control_images/qgis_server/WMS_GetLegendGraphic_test_layertitle_false/WMS_GetLegendGraphic_test_layertitle_false.png create mode 100644 tests/testdata/control_images/qgis_server/WMS_GetLegendGraphic_test_layertitle_false/WMS_GetLegendGraphic_test_layertitle_false_mask.png diff --git a/src/server/qgswmsserver.cpp b/src/server/qgswmsserver.cpp index 345805fc5bb8..bf28c1f5ed85 100644 --- a/src/server/qgswmsserver.cpp +++ b/src/server/qgswmsserver.cpp @@ -939,6 +939,14 @@ QImage* QgsWMSServer::getLegendGraphics() legendNode->setUserLabel( " " ); // empty string = no override, so let's use one space } } + else if ( !mDrawLegendLayerLabel ) + { + Q_FOREACH ( QgsLayerTreeModelLegendNode* legendNode, legendModel.layerLegendNodes( nodeLayer ) ) + { + if ( legendNode->isEmbeddedInParent() ) + legendNode->setEmbeddedInParent( false ); + } + } } } diff --git a/tests/src/python/test_qgsserver.py b/tests/src/python/test_qgsserver.py index 0d2ed4a3d47a..63b9b9db7bbe 100644 --- a/tests/src/python/test_qgsserver.py +++ b/tests/src/python/test_qgsserver.py @@ -19,10 +19,13 @@ from mimetools import Message from StringIO import StringIO from qgis.server import QgsServer -from qgis.core import QgsMessageLog +from qgis.core import QgsMessageLog, QgsRenderChecker from qgis.testing import unittest +from qgis.PyQt.QtCore import QSize from utilities import unitTestDataPath import osgeo.gdal +import tempfile +import base64 # Strip path and content length because path may vary RE_STRIP_PATH = r'MAP=[^&]+|Content-Length: \d+' @@ -383,6 +386,81 @@ def test_getLegendGraphics(self): self.assertEqual(-1, h.find('Content-Type: text/xml; charset=utf-8'), "Header: %s\nResponse:\n%s" % (h, r)) self.assertNotEquals(-1, h.find('Content-Type: image/png'), "Header: %s\nResponse:\n%s" % (h, r)) + def test_getLegendGraphics_layertitle(self): + """Test that does not return an exception but an image""" + parms = { + 'MAP': self.testdata_path + "test%2Bproject.qgs", + 'SERVICE': 'WMS', + 'VERSION': '1.3.0', + 'REQUEST': 'GetLegendGraphic', + 'FORMAT': 'image/png', + #'WIDTH': '20', # optional + #'HEIGHT': '20', # optional + 'LAYER': u'testlayer+èé', + 'LAYERTITLE': 'TRUE', + } + qs = '&'.join([u"%s=%s" % (k, v) for k, v in parms.iteritems()]) + r, h = self._result(self.server.handleRequest(qs)) + self._img_diff_error(r, h, "WMS_GetLegendGraphic_test", 250, QSize(10, 10)) + + parms = { + 'MAP': self.testdata_path + "test%2Bproject.qgs", + 'SERVICE': 'WMS', + 'VERSION': '1.3.0', + 'REQUEST': 'GetLegendGraphic', + 'FORMAT': 'image/png', + #'WIDTH': '20', # optional + #'HEIGHT': '20', # optional + 'LAYER': u'testlayer+èé', + 'LAYERTITLE': 'FALSE', + } + qs = '&'.join([u"%s=%s" % (k, v) for k, v in parms.iteritems()]) + r, h = self._result(self.server.handleRequest(qs)) + self._img_diff_error(r, h, "WMS_GetLegendGraphic_test_layertitle_false", 250, QSize(10, 10)) + + def _result(self, data): + headers = {} + for line in data[0].decode('UTF-8').split("\n"): + if line != "": + header = line.split(":") + self.assertEqual(len(header), 2, line) + headers[str(header[0])] = str(header[1]).strip() + + return data[1], headers + + def _img_diff(self, image, control_image, max_diff, max_size_diff=QSize()): + temp_image = os.path.join(tempfile.gettempdir(), "%s_result.png" % control_image) + + with open(temp_image, "wb") as f: + f.write(image) + + control = QgsRenderChecker() + control.setControlPathPrefix("qgis_server") + control.setControlName(control_image) + control.setRenderedImage(temp_image) + if max_size_diff.isValid(): + control.setSizeTolerance(max_size_diff.width(), max_size_diff.height()) + return control.compareImages(control_image), control.report() + + def _img_diff_error(self, response, headers, image, max_diff=10, max_size_diff=QSize()): + self.assertEqual( + headers.get("Content-Type"), "image/png", + "Content type is wrong: %s" % headers.get("Content-Type")) + test, report = self._img_diff(response, image, max_diff, max_size_diff) + + with open(os.path.join(tempfile.gettempdir(), image + "_result.png"), "rb") as rendered_file: + encoded_rendered_file = base64.b64encode(rendered_file.read()) + message = "Image is wrong\n%s\nImage:\necho '%s' | base64 -d >%s/%s_result.png" % ( + report, encoded_rendered_file.strip(), tempfile.gettempdir(), image + ) + + with open(os.path.join(tempfile.gettempdir(), image + "_result_diff.png"), "rb") as diff_file: + encoded_diff_file = base64.b64encode(diff_file.read()) + message += "\nDiff:\necho '%s' | base64 -d > %s/%s_result_diff.png" % ( + encoded_diff_file.strip(), tempfile.gettempdir(), image + ) + + self.assertTrue(test, message) if __name__ == '__main__': unittest.main() diff --git a/tests/testdata/control_images/qgis_server/WMS_GetLegendGraphic_test/WMS_GetLegendGraphic_test.png b/tests/testdata/control_images/qgis_server/WMS_GetLegendGraphic_test/WMS_GetLegendGraphic_test.png new file mode 100644 index 0000000000000000000000000000000000000000..163ae4e7b49d44bbe167557b490808d08cb9afc6 GIT binary patch literal 2962 zcmai0c{o&iA3uf2@|q-D%1(C(S)vJr!PupV$==xavW$Hx!MjSF!&Ly_dT0#SMTQrw=EqoN8C~t7a-8=PKbs8q;Q43 z|FH8B|AgqR<)1jT^t3C>SheakBT|YH7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MJQ4c|QB}(&dIGBtk z&Mz(!9@fZ8OP4e@=IP;4mq)17EQEB3$L;P^J^W;Q9Bs3E$L-;{^R~qMA1W$(dwZ1} zn`=i>G+B-uqyfOp(lQNR434VjfI_Gu^KNv;4@g$G8Vq)LKz{>zD5^Xd$Bs54v2@z~ z|BRox@Bj(LW^gO8#_4d`JaMr)qzcvt9xcn#C~?M!lqj>fatFm+gmhlRY1lELrlv+9 zZGn7vdMlQiRIaIfE!{Nz(P_*zR8viHIY z<=bxOeup#AK@1Qf~3?Fug89vX7{dc-QCcwbodlb9vmE`Qp5cG z6l7%F*Me#nZk!N+`0eeGOav?NzOvHH+&nZm`16Z{b@zp?LRXqwY#i9 zpsC-uaYISTvK*NTmz;0xd@(*bdAt#8+v;Q~nfY@4Ym@LLy}#!+VKCTG<`);`p=FKS zUdr+g$SoF&1tTO_DQ^7=h+W*&m7Iy!C5 z&4-}Zfp{=mz!LyR0p*#^7C)+P@VNT1`BTsii-BRDD(00S>@K(*Kce*?!(y)l7i_2Y^|gW!U_%O?XxI14-? ziy0WWg+Z8+Vb&Z8pdfpRr>`sf11=$URj%|%$=5)kX`U{QAr-gY-r2~>pupqmxPQn0 zQ-utx6~qq3>T-PY5cLVY+Q7a{-65uf`$+2!0r3v*LPb3R@kdTE3VJ{Rka%GuQ2Y@S zQ2YohP#mlfA>MTPm5g6e>CE@xzG(Vj=6&3M?NsknNgud*2j}wsOXDi6*R?nSbRmPM LtDnm{r-UW|EDTuk literal 0 HcmV?d00001 diff --git a/tests/testdata/control_images/qgis_server/WMS_GetLegendGraphic_test_layertitle_false/WMS_GetLegendGraphic_test_layertitle_false.png b/tests/testdata/control_images/qgis_server/WMS_GetLegendGraphic_test_layertitle_false/WMS_GetLegendGraphic_test_layertitle_false.png new file mode 100644 index 0000000000000000000000000000000000000000..69cb22e94f2be998ac0d0b58690cbac0e3eb27dd GIT binary patch literal 355 zcmeAS@N?(olHy`uVBq!ia0vp^dO)nq!2~2zT-WboU|`hpba4!cXifg}|G)j=2Mj*U zZy8u5O1aDoUWw!+T+zI7U`6nT#=zwX%!~aFvboJ|;@vP|;q#xLpD*{D`{>!TvVwwy zM8)GQZywlpCOp3O)oq*AEh(#UpK#Qc2*!gF};m@-=4n{Kkj<+YwB!`axTu3-@d)Q z?epgU#JdL%98l0XUi|)F?5Vqsvw}lHL-l3%cB=h&_;dUG`}LP^YxeZ?6rMDkzTMVQ zY|qx`p?P6)p>v#^q8b}M{8*4)pI_L$`OlwUzds+|A9%0R)$%FxV-YsCHqBu5_x4++ y&zxzgq++F^(V_KD;j*R)Q}c=k@@Rp;$gr&D!lYS!=ly_z$KdJe=d#Wzp$Pzljit^2 literal 0 HcmV?d00001 diff --git a/tests/testdata/control_images/qgis_server/WMS_GetLegendGraphic_test_layertitle_false/WMS_GetLegendGraphic_test_layertitle_false_mask.png b/tests/testdata/control_images/qgis_server/WMS_GetLegendGraphic_test_layertitle_false/WMS_GetLegendGraphic_test_layertitle_false_mask.png new file mode 100644 index 0000000000000000000000000000000000000000..4b118d9d43843ca7adcf5166ebd9ea8b1fa7e257 GIT binary patch literal 166 zcmeAS@N?(olHy`uVBq!ia0vp^dO)nq!2~2zT-WadQk(@Ik;Opy&mhdG|JU{(P>{XE z)7O>#0hb`ZzM}4$Z{Cf_`w5GI1~PcM`njxgN@xNA DebqGV literal 0 HcmV?d00001