Пример #1
0
    def testObstacles(self):
        w = QgsLabelObstacleSettingsWidget()
        settings = QgsLabelObstacleSettings()
        settings.setFactor(0.4)
        settings.setType(QgsLabelObstacleSettings.PolygonBoundary)
        spy = QSignalSpy(w.changed)
        w.setSettings(settings)
        self.assertEqual(len(spy), 0)
        settings = w.settings()
        self.assertEqual(settings.factor(), 0.4)
        self.assertEqual(settings.type(),
                         QgsLabelObstacleSettings.PolygonBoundary)
        settings.setFactor(1.2)
        settings.setType(QgsLabelObstacleSettings.PolygonInterior)
        w.setSettings(settings)
        self.assertEqual(len(spy), 0)
        settings = w.settings()
        self.assertEqual(settings.factor(), 1.2)
        self.assertEqual(settings.type(),
                         QgsLabelObstacleSettings.PolygonInterior)

        props = QgsPropertyCollection()
        props.setProperty(QgsPalLayerSettings.ObstacleFactor,
                          QgsProperty.fromValue(5))
        w.setDataDefinedProperties(props)

        props = QgsPropertyCollection()
        self.assertFalse(props.isActive(QgsPalLayerSettings.ObstacleFactor))
        w.updateDataDefinedProperties(props)
        self.assertTrue(props.isActive(QgsPalLayerSettings.ObstacleFactor))
        props = w.dataDefinedProperties()
        prop = props.property(QgsPalLayerSettings.ObstacleFactor)
        self.assertEqual(prop.asExpression(), '5')
Пример #2
0
    def grid_labeler(self, coord_base_x, coord_base_y, px, py, u, t, dx, dy,
                     desc, fSize, fontType, expression_str, trLLUTM, llcolor,
                     layer_bound, trUTMLL):
        pgrid = QgsPoint(coord_base_x + px * u, coord_base_y + py * t)
        pgrid.transform(trLLUTM)
        pgrid = QgsPoint(pgrid.x() + dx, pgrid.y() + dy)
        if layer_bound.crs().isGeographic() == True:
            pgrid.transform(trUTMLL)
        #Label Format Settings
        settings = QgsPalLayerSettings()
        settings.placement = 1
        settings.isExpression = True
        textprop = QgsTextFormat()
        textprop.setColor(llcolor)
        textprop.setSizeUnit(4)
        textprop.setSize(fSize * 2.8346)
        textprop.setFont(QFont(fontType))
        textprop.setLineHeight(1)
        settings.setFormat(textprop)
        settings.fieldName = expression_str

        #Label Position
        settings.geometryGeneratorEnabled = True
        settings.geometryGenerator = ("make_point({}, {})".format(
            pgrid.x(), pgrid.y()))
        datadefined = QgsPropertyCollection()
        datadefined.property(20).setExpressionString('True')
        datadefined.property(20).setActive(True)
        datadefined.property(15).setExpressionString('True')
        datadefined.property(15).setActive(True)
        datadefined.property(77).setExpressionString('2')
        datadefined.property(77).setActive(True)

        #Creating and Activating Labeling Rule
        settings.setDataDefinedProperties(datadefined)
        rule = QgsRuleBasedLabeling.Rule(settings)
        rule.setDescription(desc)
        rule.setActive(True)

        return rule