Skip to content

Commit

Permalink
Fix export legend as JSON when a rule based does not have a symbol
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustry authored and nyalldawson committed Jul 25, 2023
1 parent 282d7e1 commit 8950c79
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 12 deletions.
23 changes: 12 additions & 11 deletions src/core/layertree/qgslayertreemodellegendnode.cpp
Expand Up @@ -781,10 +781,21 @@ QSizeF QgsSymbolLegendNode::drawSymbol( const QgsLegendSettings &settings, ItemC

QJsonObject QgsSymbolLegendNode::exportSymbolToJson( const QgsLegendSettings &settings, const QgsRenderContext &context ) const
{
QJsonObject json;
if ( mItem.scaleMaxDenom() > 0 )
{
json[ QStringLiteral( "scaleMaxDenom" ) ] = mItem.scaleMaxDenom();
}
if ( mItem.scaleMinDenom() > 0 )
{
json[ QStringLiteral( "scaleMinDenom" ) ] = mItem.scaleMinDenom();
}
mItem.scaleMaxDenom();

const QgsSymbol *s = mCustomSymbol ? mCustomSymbol.get() : mItem.symbol();
if ( !s )
{
return QJsonObject();
return json;
}


Expand Down Expand Up @@ -826,17 +837,7 @@ QJsonObject QgsSymbolLegendNode::exportSymbolToJson( const QgsLegendSettings &se
img.save( &buffer, "PNG" );
const QString base64 = QString::fromLatin1( byteArray.toBase64().data() );

QJsonObject json;
json[ QStringLiteral( "icon" ) ] = base64;
if ( mItem.scaleMaxDenom() > 0 )
{
json[ QStringLiteral( "scaleMaxDenom" ) ] = mItem.scaleMaxDenom();
}
if ( mItem.scaleMinDenom() > 0 )
{
json[ QStringLiteral( "scaleMinDenom" ) ] = mItem.scaleMinDenom();
}
mItem.scaleMaxDenom();
return json;
}

Expand Down
58 changes: 57 additions & 1 deletion tests/src/python/test_qgsserver_wms_getlegendgraphic.py
Expand Up @@ -33,6 +33,8 @@
from test_qgsserver_wms import TestQgsServerWMSTestBase
from qgis.core import (
QgsProject,
QgsSymbol,
QgsWkbTypes,
QgsMarkerSymbol,
QgsRuleBasedRenderer,
QgsVectorLayer,
Expand All @@ -42,7 +44,6 @@
QgsBufferServerRequest,
QgsBufferServerResponse,
QgsServer,
QgsServerRequest,
)

# Strip path and content length because path may vary
Expand Down Expand Up @@ -1286,6 +1287,61 @@ def testJsonSymbolMaxMinScale(self):
self.assertEqual(node['scaleMaxDenom'], 1000)
self.assertEqual(node['scaleMinDenom'], 10000)

def test_json_rule_based_max_min_scale_without_symbol(self):
""" Test min/max scale in rule based json export when a rule doesn't have a symbol. """
root_rule = QgsRuleBasedRenderer.Rule(None)

# Rule with symbol
high_scale_rule = QgsRuleBasedRenderer.Rule(
QgsSymbol.defaultSymbol(QgsWkbTypes.PointGeometry),
minimumScale=25000, maximumScale=1000, label='high-scale')
root_rule.appendChild(high_scale_rule)

# Rule without symbol
low_scale_rule = QgsRuleBasedRenderer.Rule(None, minimumScale=100000, maximumScale=25000, label='low-scale')

# Sub-rule with a symbol
sub_rule = QgsRuleBasedRenderer.Rule(
QgsSymbol.defaultSymbol(QgsWkbTypes.PointGeometry), label='low-scale-sub')

low_scale_rule.appendChild(sub_rule)
root_rule.appendChild(low_scale_rule)

layer = QgsVectorLayer("Point?field=fldtxt:string", "layer1", "memory")
layer.setRenderer(QgsRuleBasedRenderer(root_rule))

project = QgsProject()
project.addMapLayer(layer)

server = QgsServer()
request = QgsBufferServerRequest(
"/?"
"SERVICE=WMS&"
"VERSION=1.3.0&"
"REQUEST=GetLegendGraphic&"
"LAYERS=layer1&"
"FORMAT=application/json"
)
response = QgsBufferServerResponse()
server.handleRequest(request, response, project)
result = json.loads(bytes(response.body()))

node = result['nodes'][0]['symbols']

# With icon
first_rule = node[0]
self.assertEqual(first_rule['scaleMaxDenom'], 25000)
self.assertEqual(first_rule['scaleMinDenom'], 1000)
self.assertEqual(first_rule['title'], 'high-scale')
self.assertIn('icon', first_rule)

# Without icon
second_rule = node[1]
self.assertEqual(second_rule['scaleMaxDenom'], 100000)
self.assertEqual(second_rule['scaleMinDenom'], 25000)
self.assertEqual(second_rule['title'], 'low-scale')
self.assertNotIn('icon', second_rule)

def testLegendPlaceholderIcon(self):
qs = "?" + "&".join(["%s=%s" % i for i in list({
"MAP": self.testdata_path + 'test_project_legend_placeholder_image.qgs',
Expand Down

0 comments on commit 8950c79

Please sign in to comment.