Skip to content

Commit

Permalink
[BUGFIX][QGIS Server] GetLegendGraphic: if LAYERTITLE is false disabl…
Browse files Browse the repository at this point in the history
…e 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.
  • Loading branch information
rldhont committed Oct 18, 2016
1 parent 743a5cd commit a51ef42
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/server/qgswmsserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -943,6 +943,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 );
}
}
}
}

Expand Down
80 changes: 79 additions & 1 deletion tests/src/python/test_qgsserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@
import email
from io import StringIO
from qgis.server import QgsServer
from qgis.core import QgsMessageLog, QgsApplication
from qgis.core import QgsMessageLog, QgsRenderChecker, QgsApplication
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_UNCHECKABLE = b'MAP=[^"]+|Content-Length: \d+'
Expand Down Expand Up @@ -431,6 +434,81 @@ def test_getLegendGraphics(self):
self.assertEqual(-1, h.find(b'Content-Type: text/xml; charset=utf-8'), "Header: %s\nResponse:\n%s" % (h, r))
self.assertNotEqual(-1, h.find(b'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_project.qgs",
'SERVICE': 'WMS',
'VERSION': '1.3.0',
'REQUEST': 'GetLegendGraphic',
'FORMAT': 'image/png',
#'WIDTH': '20', # optional
#'HEIGHT': '20', # optional
'LAYER': u'testlayer%20èé',
'LAYERTITLE': 'TRUE',
}
qs = '&'.join([u"%s=%s" % (k, v) for k, v in parms.items()])
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_project.qgs",
'SERVICE': 'WMS',
'VERSION': '1.3.0',
'REQUEST': 'GetLegendGraphic',
'FORMAT': 'image/png',
#'WIDTH': '20', # optional
#'HEIGHT': '20', # optional
'LAYER': u'testlayer%20èé',
'LAYERTITLE': 'FALSE',
}
qs = '&'.join([u"%s=%s" % (k, v) for k, v in parms.items()])
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()
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit a51ef42

Please sign in to comment.