def testVectorBlending(self): """Test that blend modes work for vector layers.""" # Add vector layers to map myLayers = [self.mLineLayer, self.mPolygonLayer] self.mapSettings.setLayers(myLayers) self.mapSettings.setExtent(self.extent) # Set blending modes for both layers self.mLineLayer.setBlendMode(QPainter.CompositionMode_Difference) self.mPolygonLayer.setBlendMode(QPainter.CompositionMode_Difference) checker = QgsMultiRenderChecker() checker.setControlName("expected_vector_blendmodes") checker.setMapSettings(self.mapSettings) checker.setColorTolerance(1) myResult = checker.runTest("vector_blendmodes", 20) myMessage = ('vector blending failed') assert myResult, myMessage # Reset layers self.mLineLayer.setBlendMode(QPainter.CompositionMode_SourceOver) self.mPolygonLayer.setBlendMode(QPainter.CompositionMode_SourceOver)
def testIntersectionRuleBased(self): """ Test that rule based renderer using intersection clip paths correctly uses original feature area for rule evaluation, not clipped area """ poly_layer = QgsVectorLayer(os.path.join(TEST_DATA_DIR, 'polys.shp')) self.assertTrue(poly_layer.isValid()) sym1 = QgsFillSymbol.createSimple({ 'color': '#ff00ff', 'outline_color': '#000000', 'outline_width': '1' }) sym2 = QgsFillSymbol.createSimple({ 'color': '#00ffff', 'outline_color': '#000000', 'outline_width': '1' }) r1 = QgsRuleBasedRenderer.Rule(sym1, 0, 0, 'area($geometry)>25') r2 = QgsRuleBasedRenderer.Rule(sym2, 0, 0, 'ELSE') rootrule = QgsRuleBasedRenderer.Rule(None) rootrule.appendChild(r1) rootrule.appendChild(r2) renderer = QgsRuleBasedRenderer(rootrule) poly_layer.setRenderer(renderer) mapsettings = QgsMapSettings() mapsettings.setOutputSize(QSize(400, 400)) mapsettings.setOutputDpi(96) mapsettings.setDestinationCrs( QgsCoordinateReferenceSystem('EPSG:3857')) mapsettings.setExtent( QgsRectangle(-13875783.2, 2266009.4, -8690110.7, 6673344.5)) mapsettings.setLayers([poly_layer]) mapsettings.setEllipsoid('') region = QgsMapClippingRegion( QgsGeometry.fromWkt( 'Polygon ((-11725957 5368254, -12222900 4807501, -12246014 3834025, -12014878 3496059, -11259833 3518307, -10751333 3621153, -10574129 4516741, -10847640 5194995, -11105742 5325957, -11725957 5368254))' )) region.setFeatureClip( QgsMapClippingRegion.FeatureClippingType.ClipToIntersection) region2 = QgsMapClippingRegion( QgsGeometry.fromWkt( 'Polygon ((-11032549 5421399, -11533344 4693167, -11086481 4229112, -11167378 3742984, -10616504 3553984, -10161936 3925771, -9618766 4668482, -9472380 5620753, -10115709 5965063, -11032549 5421399))' )) region2.setFeatureClip( QgsMapClippingRegion.FeatureClippingType.ClipToIntersection) mapsettings.addClippingRegion(region) mapsettings.addClippingRegion(region2) renderchecker = QgsMultiRenderChecker() renderchecker.setMapSettings(mapsettings) renderchecker.setControlPathPrefix('vectorlayerrenderer') renderchecker.setControlName('expected_intersection_rule_based') result = renderchecker.runTest('expected_intersection_rule_based') self.report += renderchecker.report() self.assertTrue(result) # also try with symbol levels renderer.setUsingSymbolLevels(True) poly_layer.setRenderer(renderer) renderchecker = QgsMultiRenderChecker() renderchecker.setMapSettings(mapsettings) renderchecker.setControlPathPrefix('vectorlayerrenderer') renderchecker.setControlName('expected_intersection_rule_based') result = renderchecker.runTest('expected_intersection_rule_based') self.report += renderchecker.report() self.assertTrue(result)
def testElse(self): # Setup rendering check renderchecker = QgsMultiRenderChecker() renderchecker.setMapSettings(self.mapsettings) renderchecker.setControlName('expected_rulebased_else') self.assertTrue(renderchecker.runTest('rulebased_else'))
def testRenderMultipleRenderersSelection(self): """ Test that selection colors only apply to main renderer :return: """ poly_layer = QgsVectorLayer(os.path.join(TEST_DATA_DIR, 'polys.shp')) self.assertTrue(poly_layer.isValid()) poly_layer.selectAll() sym1 = QgsFillSymbol.createSimple({ 'color': '#ffaaff', 'outline_color': '#000000', 'outline_style': 'no' }) renderer = QgsSingleSymbolRenderer(sym1) poly_layer.setRenderer(renderer) # add secondary renderer, for rendering below class Gen1(QgsFeatureRendererGenerator): def id(self): return 'Gen1' def level(self): return -2 def createRenderer(self): renderer = QgsCategorizedSymbolRenderer() renderer.setClassAttribute('Name') sym1 = QgsFillSymbol.createSimple({ 'color': '#ffaaff', 'outline_color': '#33aa33', 'outline_width': '3' }) sym2 = QgsFillSymbol.createSimple({ 'color': '#ffaaff', 'outline_color': '#aa33aa', 'outline_width': '3' }) renderer.addCategory(QgsRendererCategory('Dam', sym1, 'Dam')) renderer.addCategory(QgsRendererCategory('Lake', sym2, 'Lake')) return renderer poly_layer.addFeatureRendererGenerator(Gen1()) mapsettings = QgsMapSettings() mapsettings.setOutputSize(QSize(400, 400)) mapsettings.setOutputDpi(96) mapsettings.setDestinationCrs( QgsCoordinateReferenceSystem('EPSG:3857')) mapsettings.setExtent( QgsRectangle(-13875783.2, 2266009.4, -8690110.7, 6673344.5)) mapsettings.setLayers([poly_layer]) renderchecker = QgsMultiRenderChecker() renderchecker.setMapSettings(mapsettings) renderchecker.setControlPathPrefix('vectorlayerrenderer') renderchecker.setControlName('expected_multiple_renderers_selection') result = renderchecker.runTest('expected_multiple_renderers_selection') self.report += renderchecker.report() self.assertTrue(result) # also try with symbol levels renderer.setUsingSymbolLevels(True) poly_layer.setRenderer(renderer) renderchecker = QgsMultiRenderChecker() renderchecker.setMapSettings(mapsettings) renderchecker.setControlPathPrefix('vectorlayerrenderer') renderchecker.setControlName('expected_multiple_renderers_selection') result = renderchecker.runTest('expected_multiple_renderers_selection') self.report += renderchecker.report() self.assertTrue(result)
def testRenderMultipleRenderersAboveAndBelow(self): poly_layer = QgsVectorLayer(os.path.join(TEST_DATA_DIR, 'polys.shp')) self.assertTrue(poly_layer.isValid()) sym1 = QgsFillSymbol.createSimple({ 'color': '#ffaaff', 'outline_color': '#000000', 'outline_width': '1' }) renderer = QgsSingleSymbolRenderer(sym1) poly_layer.setRenderer(renderer) # add secondary renderer, for rendering below class Gen1(QgsFeatureRendererGenerator): def id(self): return 'Gen1' def level(self): return 3 def createRenderer(self): renderer = QgsCategorizedSymbolRenderer() renderer.setClassAttribute('Name') cf1 = QgsCentroidFillSymbolLayer() cf1.setSubSymbol( QgsMarkerSymbol.createSimple({ 'color': '#33aa33', 'outline_style': 'no', 'size': '5' })) sym1 = QgsFillSymbol([cf1]) cf2 = QgsCentroidFillSymbolLayer() cf2.setSubSymbol( QgsMarkerSymbol.createSimple({ 'color': '#aa33aa', 'outline_style': 'no', 'size': '5' })) sym2 = QgsFillSymbol([cf2]) renderer.addCategory(QgsRendererCategory('Dam', sym1, 'Dam')) renderer.addCategory(QgsRendererCategory('Lake', sym2, 'Lake')) return renderer # add secondary renderer, for rendering below class Gen2(QgsFeatureRendererGenerator): def id(self): return 'Gen2' def level(self): return 2 def createRenderer(self): renderer = QgsCategorizedSymbolRenderer() renderer.setClassAttribute('Value < 12') cf1 = QgsCentroidFillSymbolLayer() cf1.setSubSymbol( QgsMarkerSymbol.createSimple({ 'color': '#aa1111', 'outline_style': 'no', 'size': '8' })) sym1 = QgsFillSymbol([cf1]) cf2 = QgsCentroidFillSymbolLayer() cf2.setSubSymbol( QgsMarkerSymbol.createSimple({ 'color': '#1111dd', 'outline_style': 'no', 'size': '8' })) sym2 = QgsFillSymbol([cf2]) renderer.addCategory(QgsRendererCategory('1', sym1, '1')) renderer.addCategory(QgsRendererCategory('0', sym2, '0')) return renderer # add secondary renderer, for rendering below class Gen1b(QgsFeatureRendererGenerator): def id(self): return 'Gen1b' def level(self): return -2 def createRenderer(self): renderer = QgsCategorizedSymbolRenderer() renderer.setClassAttribute('Name') sym1 = QgsFillSymbol.createSimple({ 'color': '#ffaaff', 'outline_color': '#33aa33', 'outline_width': '3' }) sym2 = QgsFillSymbol.createSimple({ 'color': '#ffaaff', 'outline_color': '#aa33aa', 'outline_width': '3' }) renderer.addCategory(QgsRendererCategory('Dam', sym1, 'Dam')) renderer.addCategory(QgsRendererCategory('Lake', sym2, 'Lake')) return renderer # add secondary renderer, for rendering below class Gen2b(QgsFeatureRendererGenerator): def id(self): return 'Gen2b' def level(self): return -3 def createRenderer(self): renderer = QgsCategorizedSymbolRenderer() renderer.setClassAttribute('Value < 12') sym1 = QgsFillSymbol.createSimple({ 'color': '#ffaaff', 'outline_color': '#aa1111', 'outline_width': '5' }) sym2 = QgsFillSymbol.createSimple({ 'color': '#ffaaff', 'outline_color': '#1111dd', 'outline_width': '5' }) renderer.addCategory(QgsRendererCategory('1', sym1, '1')) renderer.addCategory(QgsRendererCategory('0', sym2, '0')) return renderer poly_layer.addFeatureRendererGenerator(Gen1()) poly_layer.addFeatureRendererGenerator(Gen2()) poly_layer.addFeatureRendererGenerator(Gen1b()) poly_layer.addFeatureRendererGenerator(Gen2b()) mapsettings = QgsMapSettings() mapsettings.setOutputSize(QSize(400, 400)) mapsettings.setOutputDpi(96) mapsettings.setDestinationCrs( QgsCoordinateReferenceSystem('EPSG:3857')) mapsettings.setExtent( QgsRectangle(-13875783.2, 2266009.4, -8690110.7, 6673344.5)) mapsettings.setLayers([poly_layer]) renderchecker = QgsMultiRenderChecker() renderchecker.setMapSettings(mapsettings) renderchecker.setControlPathPrefix('vectorlayerrenderer') renderchecker.setControlName( 'expected_multiple_renderers_both_above_below') result = renderchecker.runTest( 'expected_multiple_renderers_both_above_below') self.report += renderchecker.report() self.assertTrue(result) # also try with symbol levels renderer.setUsingSymbolLevels(True) poly_layer.setRenderer(renderer) renderchecker = QgsMultiRenderChecker() renderchecker.setMapSettings(mapsettings) renderchecker.setControlPathPrefix('vectorlayerrenderer') renderchecker.setControlName( 'expected_multiple_renderers_both_above_below') result = renderchecker.runTest( 'expected_multiple_renderers_both_above_below') self.report += renderchecker.report() self.assertTrue(result)