Пример #1
2
    def test_AddPALToVectorLayer(self):
        """Check if we can set a label field, verify that PAL is assigned
            and that output is rendered correctly"""
        # TODO: add UTM PAL-specific shps, with 4326 as on-the-fly cross-check
#        setCanvasCrs(26913)

        myShpFile = os.path.join(TEST_DATA_DIR, 'lines.shp')
        myVectorLayer = QgsVectorLayer(myShpFile, 'Lines', 'ogr')

        self._MapRegistry.addMapLayer(myVectorLayer)

        myLayers = QStringList()
        myLayers.append(myVectorLayer.id())
        self._MapRenderer.setLayerSet(myLayers)
        self._MapRenderer.setExtent(myVectorLayer.extent())
        self._Canvas.zoomToFullExtent()

        # check layer labeling is PAL with customProperty access
        # should not be activated on layer load
        myPalSet = myVectorLayer.customProperty( "labeling" ).toString()
        myMessage = '\nExpected: Empty QString\nGot: %s' % (str(myPalSet))
        assert str(myPalSet) == '', myMessage

        # simulate clicking checkbox, setting label field and clicking apply
        self._testFont.setPointSize(20)
        myPalLyr = QgsPalLayerSettings()

        myPalLyr.enabled = True
        myPalLyr.fieldName = 'Name'
        myPalLyr.placement = QgsPalLayerSettings.Line
        myPalLyr.placementFlags = QgsPalLayerSettings.AboveLine
        myPalLyr.xQuadOffset = 0
        myPalLyr.yQuadOffset = 0
        myPalLyr.xOffset = 0
        myPalLyr.yOffset = 0
        myPalLyr.angleOffset = 0
        myPalLyr.centroidWhole = False
        myPalLyr.textFont = self._testFont
        myPalLyr.textNamedStyle = QString("Medium")
        myPalLyr.textColor = Qt.black
        myPalLyr.textTransp = 0
        myPalLyr.previewBkgrdColor = Qt.white
        myPalLyr.priority = 5
        myPalLyr.obstacle = True
        myPalLyr.dist = 0
        myPalLyr.scaleMin = 0
        myPalLyr.scaleMax = 0
        myPalLyr.bufferSize = 1
        myPalLyr.bufferColor = Qt.white
        myPalLyr.bufferTransp = 0
        myPalLyr.bufferNoFill = False
        myPalLyr.bufferJoinStyle = Qt.RoundJoin
        myPalLyr.formatNumbers = False
        myPalLyr.decimals = 3
        myPalLyr.plusSign = False
        myPalLyr.labelPerPart = False
        myPalLyr.displayAll = True
        myPalLyr.mergeLines = False
        myPalLyr.minFeatureSize = 0.0
        myPalLyr.vectorScaleFactor = 1.0
        myPalLyr.rasterCompressFactor = 1.0
        myPalLyr.addDirectionSymbol = False
        myPalLyr.upsidedownLabels = QgsPalLayerSettings.Upright
        myPalLyr.fontSizeInMapUnits = False
        myPalLyr.bufferSizeInMapUnits = False
        myPalLyr.labelOffsetInMapUnits = True
        myPalLyr.distInMapUnits = False
        myPalLyr.wrapChar = ""
        myPalLyr.preserveRotation = True

        myPalLyr.writeToLayer(myVectorLayer)

        # check layer labeling is PAL with customProperty access
        myPalSet = myVectorLayer.customProperty( "labeling" ).toString()
        myMessage = '\nExpected: pal\nGot: %s' % (str(myPalSet))
        assert str(myPalSet) == 'pal', myMessage

        # check layer labeling is PAL via engine interface
        myMessage = '\nCould not get whether PAL enabled from labelingEngine'
        assert self._PalEngine.willUseLayer(myVectorLayer), myMessage

        #
        myChecker = QgsRenderChecker()
        myChecker.setControlName("expected_pal_aboveLineLabeling")
        myChecker.setMapRenderer(self._MapRenderer)

        myResult = myChecker.runTest("pal_aboveLineLabeling_python");
        myMessage = ('\nVector layer \'above line\' label engine '
                     'rendering test failed')
        assert myResult, myMessage

        # compare against a straight rendering/save as from QgsMapCanvasMap
        # unnecessary? works a bit different than QgsRenderChecker, though
#        myImage = os.path.join(unicode(QDir.tempPath()),
#                               'render_pal_aboveLineLabeling.png')
#        self._Map.render()
#        self._Canvas.saveAsImage(myImage)
#        myChecker.setRenderedImage(myImage)
#        myResult = myChecker.compareImages("pal_aboveLineLabeling_python")
#        myMessage = ('\nVector layer \'above line\' label engine '
#                     'comparison to QgsMapCanvasMap.render() test failed')
#        assert myResult, myMessage

        self._MapRegistry.removeMapLayer(myVectorLayer.id())
Пример #2
0
    def postProcessLayer(self, layer, context, feedback):
        _ = feedback, context
        layer.setLabelsEnabled(True)

        pal = QgsPalLayerSettings()
        # Expression combinant les nom d'habitat et du faciès
        pal.fieldName = "|| ' - ' || ".join(self.fields)
        pal.isExpression = True
        # Mais ce label est trop long
        # utilisation du faciès pour le label
        # et du nom pour le rendu
        pal.fieldName = self.fields[1]
        pal.isExpression = False
        pal.labelPerPart = True
        pal.placement = QgsPalLayerSettings.OverPoint

        txt_buff = QgsTextBufferSettings()
        txt_buff.setEnabled(True)

        txt_format = pal.format()
        txt_format.setBuffer(txt_buff)
        pal.setFormat(txt_format)

        layer.setLabeling(QgsVectorLayerSimpleLabeling(pal))

        renderer = layer.renderer()
        symbol = renderer.symbol()
        symbol.setSize(6)

        # Symbologie à partir du champs nom de l'habitat
        index = layer.fields().indexOf(self.fields[0])
        values = layer.uniqueValues(index)

        color = QgsRandomColorRamp()
        color.setTotalColorCount(len(values))

        categories = []
        for i, value in enumerate(values):
            symbol = QgsSymbol.defaultSymbol(QgsWkbTypes.PointGeometry)
            symbol.setColor(color.color(i))
            symbol.setSize(5)
            category = QgsRendererCategory(value, symbol, value)
            categories.append(category)

        renderer = QgsCategorizedSymbolRenderer(self.fields[0], categories)
        layer.setRenderer(renderer)

        layer.triggerRepaint()
Пример #3
0
    def test_AddPALToVectorLayer(self):
        """Check if we can set a label field, verify that PAL is assigned
            and that output is rendered correctly"""
        # TODO: add UTM PAL-specific shps, with 4326 as on-the-fly cross-check
        #        setCanvasCrs(26913)

        myShpFile = os.path.join(TEST_DATA_DIR, 'lines.shp')
        myVectorLayer = QgsVectorLayer(myShpFile, 'Lines', 'ogr')

        self._MapRegistry.addMapLayer(myVectorLayer)

        myLayers = QStringList()
        myLayers.append(myVectorLayer.id())
        self._MapRenderer.setLayerSet(myLayers)
        self._MapRenderer.setExtent(myVectorLayer.extent())
        self._Canvas.zoomToFullExtent()

        # check layer labeling is PAL with customProperty access
        # should not be activated on layer load
        myPalSet = myVectorLayer.customProperty("labeling").toString()
        myMessage = '\nExpected: Empty QString\nGot: %s' % (str(myPalSet))
        assert str(myPalSet) == '', myMessage

        # simulate clicking checkbox, setting label field and clicking apply
        self._testFont.setPointSize(20)
        myPalLyr = QgsPalLayerSettings()

        myPalLyr.enabled = True
        myPalLyr.fieldName = 'Name'
        myPalLyr.placement = QgsPalLayerSettings.Line
        myPalLyr.placementFlags = QgsPalLayerSettings.AboveLine
        myPalLyr.xQuadOffset = 0
        myPalLyr.yQuadOffset = 0
        myPalLyr.xOffset = 0
        myPalLyr.yOffset = 0
        myPalLyr.angleOffset = 0
        myPalLyr.centroidWhole = False
        myPalLyr.textFont = self._testFont
        myPalLyr.textNamedStyle = QString("Medium")
        myPalLyr.textColor = Qt.black
        myPalLyr.textTransp = 0
        myPalLyr.previewBkgrdColor = Qt.white
        myPalLyr.priority = 5
        myPalLyr.obstacle = True
        myPalLyr.dist = 0
        myPalLyr.scaleMin = 0
        myPalLyr.scaleMax = 0
        myPalLyr.bufferSize = 1
        myPalLyr.bufferColor = Qt.white
        myPalLyr.bufferTransp = 0
        myPalLyr.bufferNoFill = False
        myPalLyr.bufferJoinStyle = Qt.RoundJoin
        myPalLyr.formatNumbers = False
        myPalLyr.decimals = 3
        myPalLyr.plusSign = False
        myPalLyr.labelPerPart = False
        myPalLyr.displayAll = True
        myPalLyr.mergeLines = False
        myPalLyr.minFeatureSize = 0.0
        myPalLyr.vectorScaleFactor = 1.0
        myPalLyr.rasterCompressFactor = 1.0
        myPalLyr.addDirectionSymbol = False
        myPalLyr.upsidedownLabels = QgsPalLayerSettings.Upright
        myPalLyr.fontSizeInMapUnits = False
        myPalLyr.bufferSizeInMapUnits = False
        myPalLyr.labelOffsetInMapUnits = True
        myPalLyr.distInMapUnits = False
        myPalLyr.wrapChar = ""
        myPalLyr.preserveRotation = True

        myPalLyr.writeToLayer(myVectorLayer)

        # check layer labeling is PAL with customProperty access
        myPalSet = myVectorLayer.customProperty("labeling").toString()
        myMessage = '\nExpected: pal\nGot: %s' % (str(myPalSet))
        assert str(myPalSet) == 'pal', myMessage

        # check layer labeling is PAL via engine interface
        myMessage = '\nCould not get whether PAL enabled from labelingEngine'
        assert self._PalEngine.willUseLayer(myVectorLayer), myMessage

        #
        myChecker = QgsRenderChecker()
        myChecker.setControlName("expected_pal_aboveLineLabeling")
        myChecker.setMapRenderer(self._MapRenderer)

        myResult = myChecker.runTest("pal_aboveLineLabeling_python")
        myMessage = ('\nVector layer \'above line\' label engine '
                     'rendering test failed')
        assert myResult, myMessage

        # compare against a straight rendering/save as from QgsMapCanvasMap
        # unnecessary? works a bit different than QgsRenderChecker, though
        #        myImage = os.path.join(unicode(QDir.tempPath()),
        #                               'render_pal_aboveLineLabeling.png')
        #        self._Map.render()
        #        self._Canvas.saveAsImage(myImage)
        #        myChecker.setRenderedImage(myImage)
        #        myResult = myChecker.compareImages("pal_aboveLineLabeling_python")
        #        myMessage = ('\nVector layer \'above line\' label engine '
        #                     'comparison to QgsMapCanvasMap.render() test failed')
        #        assert myResult, myMessage

        self._MapRegistry.removeMapLayer(myVectorLayer.id())