Skip to content
Permalink
Browse files
When a geometry generator symbol layer is used as part of a
SUBSYMBOL, the $geometry part of the expression must refer to
the shape which is being rendered by the parent symbol, and
not the original feature's geometry
  • Loading branch information
nyalldawson committed Oct 24, 2021
1 parent 0175782 commit 2588bb2e8ddcba9725f7611c1e31a54ada79b95d
@@ -333,10 +333,12 @@ void QgsGeometryGeneratorSymbolLayer::render( QgsSymbolRenderContext &context, Q
QgsExpressionContext &expressionContext = context.renderContext().expressionContext();
QgsFeature f = expressionContext.feature();

if ( !context.feature() && points )
if ( ( !context.feature() || context.renderContext().flags() & Qgis::RenderContextFlag::RenderingSubSymbol ) && points )
{
// oh dear, we don't have a feature to work from... but that's ok, we are probably being rendered as a plain old symbol!
// in this case we need to build up a feature which represents the points being rendered
// in this case we need to build up a feature which represents the points being rendered.
// note that we also do this same logic when we are rendering a subsymbol. In that case the $geometry part of the
// expression should refer to the shape of the subsymbol being rendered, NOT the feature's original geometry
QgsGeometry drawGeometry;

// step 1 - convert points and rings to geometry
@@ -44,7 +44,8 @@
QgsRenderContext,
QgsRenderChecker,
QgsCoordinateReferenceSystem,
QgsCoordinateTransform
QgsCoordinateTransform,
QgsArrowSymbolLayer
)

from qgis.testing import start_app, unittest
@@ -327,6 +328,47 @@ def test_no_feature_coordinate_transform(self):

self.assertTrue(self.imageCheck('geometrygenerator_nofeature', 'geometrygenerator_nofeature', image))

def test_subsymbol(self):
"""
Test rendering a generator in a subsymbol of another symbol
"""
sym = QgsLineSymbol()
arrow = QgsArrowSymbolLayer()
arrow.setIsRepeated(False)
arrow.setArrowStartWidth(10)
arrow.setArrowWidth(5)
arrow.setHeadLength(20)
arrow.setHeadThickness(10)

sym.changeSymbolLayer(0, arrow)

self.lines_layer.renderer().setSymbol(sym)

# here "$geometry" must refer to the created ARROW shape, NOT the original feature line geometry!
generator_layer = QgsGeometryGeneratorSymbolLayer.create(
{'geometryModifier': 'buffer($geometry, 3)'})
generator_layer.setSymbolType(QgsSymbol.Fill)
generator_layer.setUnits(QgsUnitTypes.RenderMillimeters)
self.assertIsNotNone(generator_layer.subSymbol())

generator_layer.subSymbol().symbolLayer(0).setColor(QColor(255, 255, 255))
generator_layer.subSymbol().symbolLayer(0).setStrokeColor(QColor(0, 0, 0))
generator_layer.subSymbol().symbolLayer(0).setStrokeWidth(2)

sub_symbol = QgsFillSymbol()
sub_symbol.changeSymbolLayer(0, generator_layer)
arrow.setSubSymbol(sub_symbol)

rendered_layers = [self.lines_layer]
self.mapsettings.setLayers(rendered_layers)

renderchecker = QgsMultiRenderChecker()
renderchecker.setMapSettings(self.mapsettings)
renderchecker.setControlName('expected_geometrygenerator_subsymbol')
res = renderchecker.runTest('geometrygenerator_subsymbol')
self.report += renderchecker.report()
self.assertTrue(res)

def imageCheck(self, name, reference_image, image):
self.report += "<h2>Render {}</h2>\n".format(name)
temp_dir = QDir.tempPath() + '/'
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 2588bb2

Please sign in to comment.