[QGIS-Developer] Wrong legend for datadefined size points layer with pyQgis

SIGéal sigeal at sigeal.com
Wed Feb 13 23:59:15 PST 2019


Hi,

I am trying to generate a proportional symbol symbology using data 
defined symbol size in a QGIS 3 python plugin.

Here is the working code :

# Symbol creation
symbolLayer = QgsSimpleMarkerSymbolLayer()
symbolLayer.setShape(QgsSimpleMarkerSymbolLayer.Circle)
symbolLayer.setSize(2.0)
symbolLayer.setColor(QColor(31, 120, 180)
symbolLayer.setStrokeColor(QColor(255, 255, 255)
symbolLayer.setStrokeWidth(0.4)

symbol = QgsMarkerSymbol.createSimple({})
symbol.changeSymbolLayer(0, symbolLayer)

# Size definition (Flannery method)
# layer is a QgsSingleSymbolRenderer
strExp = 'coalesce(scale_exp("value", 0.0, 150, 1, 20, 0.57), 0)'
symbol.setDataDefinedSize(QgsProperty.fromExpression(strExp))
layer.renderer().setSymbol(symbol)

The problem is the legend. This code :

# Legend
lgd = QgsDataDefinedSizeLegend()
lgd.setLegendType(QgsDataDefinedSizeLegend.LegendCollapsed)
lgd.setClasses([
     QgsDataDefinedSizeLegend.SizeClass(150, '150'),
     QgsDataDefinedSizeLegend.SizeClass(75, '75'),
     QgsDataDefinedSizeLegend.SizeClass(15, '15')
])

layer.renderer().setDataDefinedSizeLegend(lgd)

produces an enormous legend symbol :
https://i.stack.imgur.com/xvTgU.png

However, if I open the layer properties dialog and update parameters 
with datadefined size assistant, the legend is refreshed with the 
correct size :
https://i.stack.imgur.com/h3Ok2.png

What am I missing ?

Thanks for any hint,

-- 
Christophe Damour


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20190214/d8dd9a06/attachment.html>


More information about the QGIS-Developer mailing list