def testPointsUsedAttributes(self): points_shp = os.path.join(TEST_DATA_DIR, 'points.shp') points_layer = QgsVectorLayer(points_shp, 'Points', 'ogr') QgsProject.instance().addMapLayer(points_layer) # Create rulebased style sym1 = QgsMarkerSymbol() l1 = QgsSimpleMarkerSymbolLayer(QgsSimpleMarkerSymbolLayer.Triangle, 5) l1.setColor(QColor(255, 0, 0)) l1.setStrokeStyle(Qt.NoPen) l1.setDataDefinedProperty(QgsSymbolLayer.PropertyAngle, QgsProperty.fromField("Heading")) sym1.changeSymbolLayer(0, l1) sym2 = QgsMarkerSymbol() l2 = QgsSimpleMarkerSymbolLayer(QgsSimpleMarkerSymbolLayer.Triangle, 5) l2.setColor(QColor(0, 255, 0)) l2.setStrokeStyle(Qt.NoPen) l2.setDataDefinedProperty(QgsSymbolLayer.PropertyAngle, QgsProperty.fromField("Heading")) sym2.changeSymbolLayer(0, l2) sym3 = QgsMarkerSymbol() l3 = QgsSimpleMarkerSymbolLayer(QgsSimpleMarkerSymbolLayer.Triangle, 5) l3.setColor(QColor(0, 0, 255)) l3.setStrokeStyle(Qt.NoPen) l3.setDataDefinedProperty(QgsSymbolLayer.PropertyAngle, QgsProperty.fromField("Heading")) sym3.changeSymbolLayer(0, l3) r1 = QgsRuleBasedRenderer.Rule(sym1, 0, 0, '"Class" = \'B52\'') r2 = QgsRuleBasedRenderer.Rule(sym2, 0, 0, '"Class" = \'Biplane\'') r3 = QgsRuleBasedRenderer.Rule(sym3, 0, 0, '"Class" = \'Jet\'') rootrule = QgsRuleBasedRenderer.Rule(None) rootrule.appendChild(r1) rootrule.appendChild(r2) rootrule.appendChild(r3) renderer = QgsRuleBasedRenderer(rootrule) points_layer.setRenderer(renderer) ms = QgsMapSettings() ms.setOutputSize(QSize(400, 400)) ms.setOutputDpi(96) ms.setExtent(QgsRectangle(-133, 22, -70, 52)) ms.setLayers([points_layer]) ctx = QgsRenderContext.fromMapSettings(ms) ctx.expressionContext().appendScope(points_layer.createExpressionContextScope()) # for symbol layer self.assertCountEqual(l1.usedAttributes(ctx), {'Heading'}) # for symbol self.assertCountEqual(sym1.usedAttributes(ctx), {'Heading'}) # for symbol renderer self.assertCountEqual(renderer.usedAttributes(ctx), {'Class', 'Heading'}) QgsProject.instance().removeMapLayer(points_layer)
def testPointsUsedAttributes(self): points_shp = os.path.join(TEST_DATA_DIR, 'points.shp') points_layer = QgsVectorLayer(points_shp, 'Points', 'ogr') QgsProject.instance().addMapLayer(points_layer) cats = [] sym1 = QgsMarkerSymbol() l1 = QgsSimpleMarkerSymbolLayer(QgsSimpleMarkerSymbolLayer.Triangle, 5) l1.setColor(QColor(255, 0, 0)) l1.setStrokeStyle(Qt.NoPen) l1.setDataDefinedProperty(QgsSymbolLayer.PropertyAngle, QgsProperty.fromField("Heading")) sym1.changeSymbolLayer(0, l1) cats.append(QgsRendererCategory("B52", sym1, "B52")) sym2 = QgsMarkerSymbol() l2 = QgsSimpleMarkerSymbolLayer(QgsSimpleMarkerSymbolLayer.Triangle, 5) l2.setColor(QColor(0, 255, 0)) l2.setStrokeStyle(Qt.NoPen) l2.setDataDefinedProperty(QgsSymbolLayer.PropertyAngle, QgsProperty.fromField("Heading")) sym2.changeSymbolLayer(0, l2) cats.append(QgsRendererCategory("Biplane", sym2, "Biplane")) sym3 = QgsMarkerSymbol() l3 = QgsSimpleMarkerSymbolLayer(QgsSimpleMarkerSymbolLayer.Triangle, 5) l3.setColor(QColor(0, 0, 255)) l3.setStrokeStyle(Qt.NoPen) l3.setDataDefinedProperty(QgsSymbolLayer.PropertyAngle, QgsProperty.fromField("Heading")) sym3.changeSymbolLayer(0, l3) cats.append(QgsRendererCategory("Jet", sym3, "Jet")) renderer = QgsCategorizedSymbolRenderer("Class", cats) points_layer.setRenderer(renderer) ms = QgsMapSettings() ms.setOutputSize(QSize(400, 400)) ms.setOutputDpi(96) ms.setExtent(QgsRectangle(-133, 22, -70, 52)) ms.setLayers([points_layer]) ctx = QgsRenderContext.fromMapSettings(ms) ctx.expressionContext().appendScope( points_layer.createExpressionContextScope()) # for symbol layer self.assertCountEqual(l1.usedAttributes(ctx), {'Heading'}) # for symbol self.assertCountEqual(sym1.usedAttributes(ctx), {'Heading'}) # for symbol renderer self.assertCountEqual(renderer.usedAttributes(ctx), {'Class', 'Heading'}) QgsProject.instance().removeMapLayer(points_layer)
def testRenderMarkerLayerDataDefined(self): """ test that rendering a marker symbol with data defined enabled layer works""" points_shp = os.path.join(TEST_DATA_DIR, 'points.shp') points_layer = QgsVectorLayer(points_shp, 'Points', 'ogr') QgsProject.instance().addMapLayer(points_layer) layer = QgsSimpleMarkerSymbolLayer() layer.setDataDefinedProperty( QgsSymbolLayer.PropertyLayerEnabled, QgsProperty.fromExpression("Class='Biplane'")) layer.setColor(QColor(100, 150, 150)) layer.setSize(5) layer.setStrokeStyle(Qt.NoPen) symbol = QgsMarkerSymbol() symbol.changeSymbolLayer(0, layer) points_layer.setRenderer(QgsSingleSymbolRenderer(symbol)) ms = QgsMapSettings() ms.setOutputSize(QSize(400, 400)) ms.setOutputDpi(96) ms.setExtent(QgsRectangle(-133, 22, -70, 52)) ms.setLayers([points_layer]) # Test usedAttributes ctx = QgsRenderContext.fromMapSettings(ms) ctx.expressionContext().appendScope( points_layer.createExpressionContextScope()) # for symbol layer self.assertCountEqual(layer.usedAttributes(ctx), {'Class'}) # for symbol self.assertCountEqual(symbol.usedAttributes(ctx), {'Class'}) # for symbol renderer self.assertCountEqual(points_layer.renderer().usedAttributes(ctx), {'Class'}) # Test rendering renderchecker = QgsMultiRenderChecker() renderchecker.setMapSettings(ms) renderchecker.setControlPathPrefix('symbol_layer') renderchecker.setControlName('expected_markerlayer_ddenabled') self.assertTrue(renderchecker.runTest('markerlayer_ddenabled')) QgsProject.instance().removeMapLayer(points_layer)