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())
 def setLabelFormatter(self, layer): # remove topicType
     try:
         palyr = QgsPalLayerSettings()
         palyr.readFromLayer(layer)
         palyr.enabled = True
         palyr.fontBold = True
         palyr.shapeDraw = True
         palyr.shapeTransparency = 0
         palyr.shapeType = QgsPalLayerSettings.ShapeRectangle
         palyr.textColor = QColor(255,255,255) # white
         palyr.placement = QgsPalLayerSettings.OverPoint
         palyr.quadOffset = QgsPalLayerSettings.QuadrantBelow
         palyr.multilineAlign = QgsPalLayerSettings.MultiCenter
         palyr.yOffset = 2
         palyr.labelOffsetInMapUnits = False
         self.setPalLabelSettings(palyr) # obj so pass by reference
         palyr.writeToLayer(layer)
         Log.debug("Palyr Settings updated")
     except Exception as e:
         Log.debug("Error setting Label Format " + str(e))
 def setLabelFormatter(self, layer):  # remove topicType
     try:
         palyr = QgsPalLayerSettings()
         palyr.readFromLayer(layer)
         palyr.enabled = True
         palyr.fontBold = True
         palyr.shapeDraw = True
         palyr.shapeTransparency = 0
         palyr.shapeType = QgsPalLayerSettings.ShapeRectangle
         palyr.textColor = QColor(255, 255, 255)  # white
         palyr.placement = QgsPalLayerSettings.OverPoint
         palyr.quadOffset = QgsPalLayerSettings.QuadrantBelow
         palyr.multilineAlign = QgsPalLayerSettings.MultiCenter
         palyr.yOffset = 2
         palyr.labelOffsetInMapUnits = False
         self.setPalLabelSettings(palyr)  # obj so pass by reference
         palyr.writeToLayer(layer)
         Log.debug("Palyr Settings updated")
     except Exception as e:
         Log.debug("Error setting Label Format " + str(e))
    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())