Skip to content
Permalink
Browse files

Merge pull request #3642 from rldhont/fix_server_legend_layertitle-re…

…lease-2_14

[BUGFIX][QGIS Server] GetLegendGraphic: if LAYERTITLE is false disable layer name in legend
  • Loading branch information
rldhont committed Oct 20, 2016
2 parents deb3dc7 + d185929 commit 961f6be518183a55e207f38c84a1c5829a9498ea
@@ -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 );
}
}
}
}

@@ -18,10 +18,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+'
@@ -372,6 +375,82 @@ def test_getLegendGraphics(self):
self.assertEquals(-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)

# The following code was used to test type conversion in python bindings
# def test_qpair(self):
# """Test QPair bindings"""
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit 961f6be

Please sign in to comment.
You can’t perform that action at this time.