コード例 #1
0
    def testClear(self):
        s = QgsAuxiliaryStorage()
        self.assertTrue(s.isValid())

        # Create a new auxiliary layer with 'pk' as key
        vl = createLayer()
        pkf = vl.fields().field(vl.fields().indexOf('pk'))
        al = s.createAuxiliaryLayer(pkf, vl)
        self.assertTrue(al.isValid())
        vl.setAuxiliaryLayer(al)

        # Add a field in auxiliary layer
        p = QgsPropertyDefinition('myprop', QgsPropertyDefinition.DataTypeNumeric, '', '', 'me')
        self.assertFalse(al.exists(p))
        self.assertTrue(al.addAuxiliaryField(p))
        self.assertTrue(al.exists(p))

        # Count auxiliary features
        self.assertEqual(al.featureCount(), 0)

        # Set value for auxiliary fields
        req = QgsFeatureRequest().setFilterExpression("name = 'Honey'")
        f = QgsFeature()
        vl.getFeatures(req).nextFeature(f)
        self.assertTrue(f.isValid())
        afName = QgsAuxiliaryLayer.nameFromProperty(p, True)
        index = vl.fields().indexOf(afName)
        vl.changeAttributeValue(f.id(), index, 333)

        # Count auxiliary features
        self.assertEqual(al.featureCount(), 1)

        # Clear and count
        al.clear()
        self.assertEqual(al.featureCount(), 0)
コード例 #2
0
    def testCreateProperty(self):
        s = QgsAuxiliaryStorage()
        self.assertTrue(s.isValid())

        # Create a new auxiliary layer with 'pk' as key
        vl = createLayer()
        pkf = vl.fields().field(vl.fields().indexOf('pk'))
        al = s.createAuxiliaryLayer(pkf, vl)
        self.assertTrue(al.isValid())
        vl.setAuxiliaryLayer(al)

        # Create a new labeling property on layer without labels
        key = QgsPalLayerSettings.PositionX
        index = QgsAuxiliaryLayer.createProperty(key, vl)
        self.assertEqual(index, -1)

        vl.setLabeling(QgsVectorLayerSimpleLabeling(QgsPalLayerSettings()))
        index = QgsAuxiliaryLayer.createProperty(key, vl)

        p = QgsPalLayerSettings.propertyDefinitions()[key]
        afName = QgsAuxiliaryLayer.nameFromProperty(p, True)
        afIndex = vl.fields().indexOf(afName)
        self.assertEqual(index, afIndex)
コード例 #3
0
    def testClear(self):
        s = QgsAuxiliaryStorage()
        self.assertTrue(s.isValid())

        # Create a new auxiliary layer with 'pk' as key
        vl = createLayer()
        pkf = vl.fields().field(vl.fields().indexOf('pk'))
        al = s.createAuxiliaryLayer(pkf, vl)
        self.assertTrue(al.isValid())
        vl.setAuxiliaryLayer(al)

        # Add a field in auxiliary layer
        p = QgsPropertyDefinition('myprop',
                                  QgsPropertyDefinition.DataTypeNumeric, '',
                                  '', 'me')
        self.assertFalse(al.exists(p))
        self.assertTrue(al.addAuxiliaryField(p))
        self.assertTrue(al.exists(p))

        # Count auxiliary features
        self.assertEqual(al.featureCount(), 0)

        # Set value for auxiliary fields
        req = QgsFeatureRequest().setFilterExpression("name = 'Honey'")
        f = QgsFeature()
        vl.getFeatures(req).nextFeature(f)
        self.assertTrue(f.isValid())
        afName = QgsAuxiliaryLayer.nameFromProperty(p, True)
        index = vl.fields().indexOf(afName)
        vl.changeAttributeValue(f.id(), index, 333)

        # Count auxiliary features
        self.assertEqual(al.featureCount(), 1)

        # Clear and count
        al.clear()
        self.assertEqual(al.featureCount(), 0)
コード例 #4
0
    def testAuxiliaryFieldWidgets(self):
        # Init storage
        s = QgsAuxiliaryStorage()
        self.assertTrue(s.isValid())

        # Create a new auxiliary layer with 'pk' as key
        vl = createLayer()
        pkf = vl.fields().field(vl.fields().indexOf('pk'))
        al = s.createAuxiliaryLayer(pkf, vl)
        self.assertTrue(al.isValid())

        # Set the auxiliary layer to the vector layer
        vl.setAuxiliaryLayer(al)

        # Add a visible property
        p = QgsPropertyDefinition('propName',
                                  QgsPropertyDefinition.DataTypeNumeric, '',
                                  '', 'user')
        self.assertTrue(al.addAuxiliaryField(p))

        index = al.indexOfPropertyDefinition(p)
        self.assertFalse(al.isHiddenProperty(index))

        afName = QgsAuxiliaryLayer.nameFromProperty(p, True)
        index = vl.fields().indexOf(afName)
        setup = vl.editorWidgetSetup(index)
        self.assertEqual(setup.type(), '')

        tested = False
        for c in vl.attributeTableConfig().columns():
            if c.name == afName:
                self.assertFalse(c.hidden)
                tested = True
                break
        self.assertTrue(tested)

        # Add a hidden property
        p = QgsPalLayerSettings.propertyDefinitions()[
            QgsPalLayerSettings.PositionX]
        self.assertTrue(al.addAuxiliaryField(p))

        index = al.indexOfPropertyDefinition(p)
        self.assertTrue(al.isHiddenProperty(index))

        afName = QgsAuxiliaryLayer.nameFromProperty(p, True)
        index = vl.fields().indexOf(afName)
        setup = vl.editorWidgetSetup(index)
        self.assertEqual(setup.type(), 'Hidden')

        tested = False
        for c in vl.attributeTableConfig().columns():
            if c.name == afName:
                self.assertTrue(c.hidden)
                tested = True
                break
        self.assertTrue(tested)

        # Add a color property
        p = QgsSymbolLayer.propertyDefinitions()[
            QgsSymbolLayer.PropertyFillColor]
        self.assertTrue(al.addAuxiliaryField(p))

        index = al.indexOfPropertyDefinition(p)
        self.assertFalse(al.isHiddenProperty(index))

        afName = QgsAuxiliaryLayer.nameFromProperty(p, True)
        index = vl.fields().indexOf(afName)
        setup = vl.editorWidgetSetup(index)
        self.assertEqual(setup.type(), 'Color')
コード例 #5
0
    def testProjectStorage(self):
        # New project without fileName
        p0 = QgsProject()
        self.assertTrue(p0.auxiliaryStorage().isValid())

        # Create new layers with key otherwise auxiliary layers are not
        # automacially created when added in project
        vl0 = createLayer()
        vl0Shp = writeShape(vl0, 'vl0.shp')

        vl1 = createLayer()
        vl1Shp = writeShape(vl1, 'vl1.shp')

        vl0 = QgsVectorLayer(vl0Shp, 'points', 'ogr')
        self.assertTrue(vl0.isValid())

        vl1 = QgsVectorLayer(vl1Shp, 'points', 'ogr')
        self.assertTrue(vl1.isValid())

        # Add layers to project and check underlying auxiliary layers
        p0.addMapLayers([vl0, vl1])

        self.assertTrue(vl0.loadAuxiliaryLayer(p0.auxiliaryStorage(), 'pk'))
        self.assertTrue(
            vl1.loadAuxiliaryLayer(p0.auxiliaryStorage(), 'num_char'))

        al0 = vl0.auxiliaryLayer()
        al1 = vl1.auxiliaryLayer()

        self.assertEqual(al0.joinInfo().targetFieldName(), 'pk')
        self.assertEqual(al1.joinInfo().targetFieldName(), 'num_char')

        # Add a field in auxiliary layers
        pdef0 = QgsPropertyDefinition('propname',
                                      QgsPropertyDefinition.DataTypeNumeric,
                                      '', '', 'ut')
        self.assertTrue(al0.addAuxiliaryField(pdef0))

        pdef1 = QgsPropertyDefinition('propname1',
                                      QgsPropertyDefinition.DataTypeString, '',
                                      '', 'ut')
        self.assertTrue(al1.addAuxiliaryField(pdef1))

        # Check auxiliary fields names
        af0Name = QgsAuxiliaryLayer.nameFromProperty(pdef0, False)
        self.assertEqual(af0Name, 'ut_propname')
        af1Name = QgsAuxiliaryLayer.nameFromProperty(pdef1, False)
        self.assertEqual(af1Name, 'ut_propname1')

        # Set value for auxiliary fields
        req = QgsFeatureRequest().setFilterExpression("name = 'Honey'")
        f = QgsFeature()
        vl0.getFeatures(req).nextFeature(f)
        self.assertTrue(f.isValid())
        af0Name = QgsAuxiliaryLayer.nameFromProperty(pdef0, True)
        index0 = vl0.fields().indexOf(af0Name)
        vl0.changeAttributeValue(f.id(), index0, 333)

        req = QgsFeatureRequest().setFilterExpression("name = 'Apple'")
        f = QgsFeature()
        vl1.getFeatures(req).nextFeature(f)
        self.assertTrue(f.isValid())
        af1Name = QgsAuxiliaryLayer.nameFromProperty(pdef1, True)
        index1 = vl1.fields().indexOf(af1Name)
        vl1.changeAttributeValue(f.id(), index0, 'myvalue')

        req = QgsFeatureRequest().setFilterExpression("name = 'Orange'")
        f = QgsFeature()
        vl1.getFeatures(req).nextFeature(f)
        self.assertTrue(f.isValid())
        vl1.changeAttributeValue(f.id(), index0, 'myvalue1')

        # Save the project in a zip file
        f = tmpPath() + '.qgz'
        p0.write(f)

        # Open the zip file with embedded auxiliary storage
        p1 = QgsProject()
        p1.read(f)

        # Check that auxiliary fields are well loaded in layers
        self.assertEqual(len(p1.mapLayers().values()), 2)

        for vl in p1.mapLayers().values():
            al = vl.auxiliaryLayer()
            self.assertEqual(len(al.auxiliaryFields()), 1)

            af = al.auxiliaryFields()[0]
            afPropDef = QgsAuxiliaryLayer.propertyDefinitionFromField(af)
            self.assertEqual(afPropDef.origin(), 'ut')

            if vl.auxiliaryLayer().joinInfo().targetFieldName() == 'pk':
                self.assertEqual(afPropDef.name(), 'propname')
                self.assertEqual(al.featureCount(), 1)

                req = QgsFeatureRequest().setFilterExpression("name = 'Honey'")
                f = QgsFeature()
                vl.getFeatures(req).nextFeature(f)
                self.assertTrue(f.isValid())
                self.assertEqual(f.attributes()[index0], 333.0)
            else:  # num_char
                self.assertEqual(al.featureCount(), 2)
                self.assertEqual(afPropDef.name(), 'propname1')

                req = QgsFeatureRequest().setFilterExpression("name = 'Apple'")
                f = QgsFeature()
                vl.getFeatures(req).nextFeature(f)
                self.assertTrue(f.isValid())
                self.assertEqual(f.attributes()[index1], 'myvalue')

                req = QgsFeatureRequest().setFilterExpression(
                    "name = 'Orange'")
                f = QgsFeature()
                vl.getFeatures(req).nextFeature(f)
                self.assertTrue(f.isValid())
                self.assertEqual(f.attributes()[index1], 'myvalue1')
コード例 #6
0
    def testCreatePropertyDiagram(self):
        s = QgsAuxiliaryStorage()
        self.assertTrue(s.isValid())

        # Create a new auxiliary layer with 'pk' as key
        vl = createLayer()
        pkf = vl.fields().field(vl.fields().indexOf('pk'))
        al = s.createAuxiliaryLayer(pkf, vl)
        self.assertTrue(al.isValid())
        vl.setAuxiliaryLayer(al)

        # Create a new labeling property on layer without labels
        key = QgsDiagramLayerSettings.PositionX
        index = QgsAuxiliaryLayer.createProperty(key, vl)
        self.assertEqual(index, -1)

        renderer = QgsSingleCategoryDiagramRenderer()
        diagram_settings = QgsDiagramLayerSettings()
        vl.setDiagramRenderer(renderer)
        vl.setDiagramLayerSettings(diagram_settings)
        index = QgsAuxiliaryLayer.createProperty(key, vl)

        p = QgsDiagramLayerSettings.propertyDefinitions()[key]
        afName = QgsAuxiliaryLayer.nameFromProperty(p, True)
        afIndex = vl.fields().indexOf(afName)
        self.assertEqual(index, afIndex)

        settings = vl.diagramLayerSettings()
        self.assertTrue(settings.dataDefinedProperties().property(key).isActive())
        self.assertEqual(settings.dataDefinedProperties().property(key).field(), "auxiliary_storage_diagram_positionx")

        # with existing property
        key = QgsDiagramLayerSettings.Distance
        settings = QgsDiagramLayerSettings()
        settings.dataDefinedProperties().setProperty(key, QgsProperty.fromExpression('$y + 20'))
        vl.setDiagramLayerSettings(settings)

        # without overwriting existing, property should be upgraded to coalesce("aux field", 'existing expression') type
        index = QgsAuxiliaryLayer.createProperty(key, vl, False)
        p = QgsDiagramLayerSettings.propertyDefinitions()[key]
        afName = QgsAuxiliaryLayer.nameFromProperty(p, True)
        afIndex = vl.fields().indexOf(afName)
        self.assertEqual(index, afIndex)

        settings = vl.diagramLayerSettings()
        self.assertTrue(settings.dataDefinedProperties().property(key).isActive())
        self.assertEqual(settings.dataDefinedProperties().property(key).asExpression(), 'coalesce("auxiliary_storage_diagram_distance",$y + 20)')

        # with overwrite existing
        key = QgsDiagramLayerSettings.PositionY
        settings = QgsDiagramLayerSettings()
        settings.dataDefinedProperties().setProperty(key, QgsProperty.fromExpression('$y > 20'))
        vl.setDiagramLayerSettings(settings)

        # existing property should be discarded
        index = QgsAuxiliaryLayer.createProperty(key, vl, True)
        p = QgsDiagramLayerSettings.propertyDefinitions()[key]
        afName = QgsAuxiliaryLayer.nameFromProperty(p, True)
        afIndex = vl.fields().indexOf(afName)
        self.assertEqual(index, afIndex)

        settings = vl.diagramLayerSettings()
        self.assertTrue(settings.dataDefinedProperties().property(key).isActive())
        self.assertEqual(settings.dataDefinedProperties().property(key).field(), 'auxiliary_storage_diagram_positiony')
コード例 #7
0
    def testCreateCalloutProperty(self):
        s = QgsAuxiliaryStorage()
        self.assertTrue(s.isValid())

        # Create a new auxiliary layer with 'pk' as key
        vl = createLayer()
        pkf = vl.fields().field(vl.fields().indexOf('pk'))
        al = s.createAuxiliaryLayer(pkf, vl)
        self.assertTrue(al.isValid())
        vl.setAuxiliaryLayer(al)

        # Create a new callout property on layer without labels
        key = QgsCallout.DestinationX
        index = QgsAuxiliaryLayer.createProperty(key, vl)
        self.assertEqual(index, -1)

        # Labeling, but no callouts
        settings = QgsPalLayerSettings()
        settings.setCallout(None)
        vl.setLabeling(QgsVectorLayerSimpleLabeling(settings))
        index = QgsAuxiliaryLayer.createProperty(key, vl)
        self.assertEqual(index, -1)

        # callouts
        settings = QgsPalLayerSettings()
        callout = QgsSimpleLineCallout()
        callout.setEnabled(True)
        settings.setCallout(callout)
        vl.setLabeling(QgsVectorLayerSimpleLabeling(settings))

        index = QgsAuxiliaryLayer.createProperty(key, vl)

        p = QgsCallout.propertyDefinitions()[key]
        afName = QgsAuxiliaryLayer.nameFromProperty(p, True)
        afIndex = vl.fields().indexOf(afName)
        self.assertEqual(index, afIndex)

        settings = vl.labeling().settings()
        self.assertEqual(settings.callout().dataDefinedProperties().property(key), QgsProperty.fromField('auxiliary_storage_callouts_destinationx'))

        key2 = QgsCallout.DestinationY
        index = QgsAuxiliaryLayer.createProperty(key2, vl)

        p = QgsCallout.propertyDefinitions()[key2]
        afName = QgsAuxiliaryLayer.nameFromProperty(p, True)
        afIndex = vl.fields().indexOf(afName)
        self.assertEqual(index, afIndex)

        settings = vl.labeling().settings()
        self.assertEqual(settings.callout().dataDefinedProperties().property(key),
                         QgsProperty.fromField('auxiliary_storage_callouts_destinationx'))
        self.assertEqual(settings.callout().dataDefinedProperties().property(key2),
                         QgsProperty.fromField('auxiliary_storage_callouts_destinationy'))

        # with existing property
        key = QgsCallout.OriginX
        settings = QgsPalLayerSettings()
        callout = QgsSimpleLineCallout()
        callout.dataDefinedProperties().setProperty(key, QgsProperty.fromExpression('$x + 20'))
        callout.setEnabled(True)
        settings.setCallout(callout)
        vl.setLabeling(QgsVectorLayerSimpleLabeling(settings))

        # without overwriting existing, property should be upgraded to coalesce("aux field", 'existing expression') type
        index = QgsAuxiliaryLayer.createProperty(key, vl, False)
        p = QgsCallout.propertyDefinitions()[key]
        afName = QgsAuxiliaryLayer.nameFromProperty(p, True)
        afIndex = vl.fields().indexOf(afName)
        self.assertEqual(index, afIndex)

        settings = vl.labeling().settings()
        self.assertTrue(settings.callout().dataDefinedProperties().property(key).isActive())
        self.assertEqual(settings.callout().dataDefinedProperties().property(key).asExpression(), 'coalesce("auxiliary_storage_callouts_originx",$x + 20)')

        # with overwrite existing
        key = QgsCallout.OriginY
        callout = QgsSimpleLineCallout()
        callout.dataDefinedProperties().setProperty(key, QgsProperty.fromExpression('$y + 20'))
        settings.setCallout(callout)
        vl.setLabeling(QgsVectorLayerSimpleLabeling(settings))

        # existing property should be discarded
        index = QgsAuxiliaryLayer.createProperty(key, vl, True)
        p = QgsCallout.propertyDefinitions()[key]
        afName = QgsAuxiliaryLayer.nameFromProperty(p, True)
        afIndex = vl.fields().indexOf(afName)
        self.assertEqual(index, afIndex)

        settings = vl.labeling().settings()
        self.assertTrue(settings.callout().dataDefinedProperties().property(key).isActive())
        self.assertEqual(settings.callout().dataDefinedProperties().property(key).field(), 'auxiliary_storage_callouts_originy')
コード例 #8
0
    def testCreateProperty(self):
        s = QgsAuxiliaryStorage()
        self.assertTrue(s.isValid())

        # Create a new auxiliary layer with 'pk' as key
        vl = createLayer()
        pkf = vl.fields().field(vl.fields().indexOf('pk'))
        al = s.createAuxiliaryLayer(pkf, vl)
        self.assertTrue(al.isValid())
        vl.setAuxiliaryLayer(al)

        # Create a new labeling property on layer without labels
        key = QgsPalLayerSettings.PositionX
        index = QgsAuxiliaryLayer.createProperty(key, vl)
        self.assertEqual(index, -1)

        vl.setLabeling(QgsVectorLayerSimpleLabeling(QgsPalLayerSettings()))
        index = QgsAuxiliaryLayer.createProperty(key, vl)

        p = QgsPalLayerSettings.propertyDefinitions()[key]
        afName = QgsAuxiliaryLayer.nameFromProperty(p, True)
        afIndex = vl.fields().indexOf(afName)
        self.assertEqual(index, afIndex)

        # with existing property
        key = QgsPalLayerSettings.PositionY
        settings = QgsPalLayerSettings()
        settings.dataDefinedProperties().setProperty(key, QgsProperty.fromExpression('$y + 20'))
        vl.setLabeling(QgsVectorLayerSimpleLabeling(settings))

        # without overwriting existing, property should be upgraded to coalesce("aux field", 'existing expression') type
        index = QgsAuxiliaryLayer.createProperty(key, vl, False)
        p = QgsPalLayerSettings.propertyDefinitions()[key]
        afName = QgsAuxiliaryLayer.nameFromProperty(p, True)
        afIndex = vl.fields().indexOf(afName)
        self.assertEqual(index, afIndex)

        settings = vl.labeling().settings()
        self.assertTrue(settings.dataDefinedProperties().property(key).isActive())
        self.assertEqual(settings.dataDefinedProperties().property(key).asExpression(), 'coalesce("auxiliary_storage_labeling_positiony",$y + 20)')

        # with existing but invalid field name
        key = QgsPalLayerSettings.PositionY
        settings = QgsPalLayerSettings()
        settings.dataDefinedProperties().setProperty(key, QgsProperty.fromField(''))
        vl.setLabeling(QgsVectorLayerSimpleLabeling(settings))

        # even when asked to not overwrite existing, this is an invalid property and should be overwritten
        index = QgsAuxiliaryLayer.createProperty(key, vl, False)
        p = QgsPalLayerSettings.propertyDefinitions()[key]
        afName = QgsAuxiliaryLayer.nameFromProperty(p, True)
        afIndex = vl.fields().indexOf(afName)
        self.assertEqual(index, afIndex)

        settings = vl.labeling().settings()
        self.assertTrue(settings.dataDefinedProperties().property(key).isActive())
        self.assertEqual(settings.dataDefinedProperties().property(key).asExpression(), '"auxiliary_storage_labeling_positiony"')

        # with existing valid field name
        key = QgsPalLayerSettings.PositionY
        settings = QgsPalLayerSettings()
        settings.dataDefinedProperties().setProperty(key, QgsProperty.fromField('asd'))
        vl.setLabeling(QgsVectorLayerSimpleLabeling(settings))

        index = QgsAuxiliaryLayer.createProperty(key, vl, False)
        p = QgsPalLayerSettings.propertyDefinitions()[key]
        afName = QgsAuxiliaryLayer.nameFromProperty(p, True)
        afIndex = vl.fields().indexOf(afName)
        self.assertEqual(index, afIndex)

        settings = vl.labeling().settings()
        self.assertTrue(settings.dataDefinedProperties().property(key).isActive())
        self.assertEqual(settings.dataDefinedProperties().property(key).asExpression(), 'coalesce("auxiliary_storage_labeling_positiony","asd")')

        # with overwrite existing
        key = QgsPalLayerSettings.Show
        settings = QgsPalLayerSettings()
        settings.dataDefinedProperties().setProperty(key, QgsProperty.fromExpression('$y > 20'))
        vl.setLabeling(QgsVectorLayerSimpleLabeling(settings))

        # existing property should be discarded
        index = QgsAuxiliaryLayer.createProperty(key, vl, True)
        p = QgsPalLayerSettings.propertyDefinitions()[key]
        afName = QgsAuxiliaryLayer.nameFromProperty(p, True)
        afIndex = vl.fields().indexOf(afName)
        self.assertEqual(index, afIndex)

        settings = vl.labeling().settings()
        self.assertTrue(settings.dataDefinedProperties().property(key).isActive())
        self.assertEqual(settings.dataDefinedProperties().property(key).field(), 'auxiliary_storage_labeling_show')
コード例 #9
0
    def testAuxiliaryFieldWidgets(self):
        # Init storage
        s = QgsAuxiliaryStorage()
        self.assertTrue(s.isValid())

        # Create a new auxiliary layer with 'pk' as key
        vl = createLayer()
        pkf = vl.fields().field(vl.fields().indexOf('pk'))
        al = s.createAuxiliaryLayer(pkf, vl)
        self.assertTrue(al.isValid())

        # Set the auxiliary layer to the vector layer
        vl.setAuxiliaryLayer(al)

        # Add a visible property
        p = QgsPropertyDefinition('propName', QgsPropertyDefinition.DataTypeNumeric, '', '', 'user')
        self.assertTrue(al.addAuxiliaryField(p))

        index = al.indexOfPropertyDefinition(p)
        self.assertFalse(al.isHiddenProperty(index))

        afName = QgsAuxiliaryLayer.nameFromProperty(p, True)
        index = vl.fields().indexOf(afName)
        setup = vl.editorWidgetSetup(index)
        self.assertEqual(setup.type(), '')

        tested = False
        for c in vl.attributeTableConfig().columns():
            if c.name == afName:
                self.assertFalse(c.hidden)
                tested = True
                break
        self.assertTrue(tested)

        # Add a hidden property
        p = QgsPalLayerSettings.propertyDefinitions()[QgsPalLayerSettings.PositionX]
        self.assertTrue(al.addAuxiliaryField(p))

        index = al.indexOfPropertyDefinition(p)
        self.assertTrue(al.isHiddenProperty(index))

        afName = QgsAuxiliaryLayer.nameFromProperty(p, True)
        index = vl.fields().indexOf(afName)
        setup = vl.editorWidgetSetup(index)
        self.assertEqual(setup.type(), 'Hidden')

        tested = False
        for c in vl.attributeTableConfig().columns():
            if c.name == afName:
                self.assertTrue(c.hidden)
                tested = True
                break
        self.assertTrue(tested)

        # Add a color property
        p = QgsSymbolLayer.propertyDefinitions()[QgsSymbolLayer.PropertyFillColor]
        self.assertTrue(al.addAuxiliaryField(p))

        index = al.indexOfPropertyDefinition(p)
        self.assertFalse(al.isHiddenProperty(index))

        afName = QgsAuxiliaryLayer.nameFromProperty(p, True)
        index = vl.fields().indexOf(afName)
        setup = vl.editorWidgetSetup(index)
        self.assertEqual(setup.type(), 'Color')
コード例 #10
0
    def testProjectStorage(self):
        # New project without fileName
        p0 = QgsProject()
        self.assertTrue(p0.auxiliaryStorage().isValid())

        # Create new layers with key otherwise auxiliary layers are not
        # automacially created when added in project
        vl0 = createLayer()
        vl0Shp = writeShape(vl0, 'vl0.shp')

        vl1 = createLayer()
        vl1Shp = writeShape(vl1, 'vl1.shp')

        vl0 = QgsVectorLayer(vl0Shp, 'points', 'ogr')
        self.assertTrue(vl0.isValid())

        vl1 = QgsVectorLayer(vl1Shp, 'points', 'ogr')
        self.assertTrue(vl1.isValid())

        # Add layers to project and check underlying auxiliary layers
        p0.addMapLayers([vl0, vl1])

        self.assertTrue(vl0.loadAuxiliaryLayer(p0.auxiliaryStorage(), 'pk'))
        self.assertTrue(vl1.loadAuxiliaryLayer(p0.auxiliaryStorage(), 'num_char'))

        al0 = vl0.auxiliaryLayer()
        al1 = vl1.auxiliaryLayer()

        self.assertEqual(al0.joinInfo().targetFieldName(), 'pk')
        self.assertEqual(al1.joinInfo().targetFieldName(), 'num_char')

        # Add a field in auxiliary layers
        pdef0 = QgsPropertyDefinition('propname', QgsPropertyDefinition.DataTypeNumeric, '', '', 'ut')
        self.assertTrue(al0.addAuxiliaryField(pdef0))

        pdef1 = QgsPropertyDefinition('propname1', QgsPropertyDefinition.DataTypeString, '', '', 'ut')
        self.assertTrue(al1.addAuxiliaryField(pdef1))

        # Check auxiliary fields names
        af0Name = QgsAuxiliaryLayer.nameFromProperty(pdef0, False)
        self.assertEqual(af0Name, 'ut_propname')
        af1Name = QgsAuxiliaryLayer.nameFromProperty(pdef1, False)
        self.assertEqual(af1Name, 'ut_propname1')

        # Set value for auxiliary fields
        req = QgsFeatureRequest().setFilterExpression("name = 'Honey'")
        f = QgsFeature()
        vl0.getFeatures(req).nextFeature(f)
        self.assertTrue(f.isValid())
        af0Name = QgsAuxiliaryLayer.nameFromProperty(pdef0, True)
        index0 = vl0.fields().indexOf(af0Name)
        vl0.changeAttributeValue(f.id(), index0, 333)

        req = QgsFeatureRequest().setFilterExpression("name = 'Apple'")
        f = QgsFeature()
        vl1.getFeatures(req).nextFeature(f)
        self.assertTrue(f.isValid())
        af1Name = QgsAuxiliaryLayer.nameFromProperty(pdef1, True)
        index1 = vl1.fields().indexOf(af1Name)
        vl1.changeAttributeValue(f.id(), index0, 'myvalue')

        req = QgsFeatureRequest().setFilterExpression("name = 'Orange'")
        f = QgsFeature()
        vl1.getFeatures(req).nextFeature(f)
        self.assertTrue(f.isValid())
        vl1.changeAttributeValue(f.id(), index0, 'myvalue1')

        # Save the project in a zip file
        f = tmpPath() + '.qgz'
        p0.write(f)

        # Open the zip file with embedded auxiliary storage
        p1 = QgsProject()
        p1.read(f)

        # Check that auxiliary fields are well loaded in layers
        self.assertEqual(len(p1.mapLayers().values()), 2)

        for vl in p1.mapLayers().values():
            al = vl.auxiliaryLayer()
            self.assertEqual(len(al.auxiliaryFields()), 1)

            af = al.auxiliaryFields()[0]
            afPropDef = QgsAuxiliaryLayer.propertyDefinitionFromField(af)
            self.assertEqual(afPropDef.origin(), 'ut')

            if vl.auxiliaryLayer().joinInfo().targetFieldName() == 'pk':
                self.assertEqual(afPropDef.name(), 'propname')
                self.assertEqual(al.featureCount(), 1)

                req = QgsFeatureRequest().setFilterExpression("name = 'Honey'")
                f = QgsFeature()
                vl.getFeatures(req).nextFeature(f)
                self.assertTrue(f.isValid())
                self.assertEqual(f.attributes()[index0], 333.0)
            else: # num_char
                self.assertEqual(al.featureCount(), 2)
                self.assertEqual(afPropDef.name(), 'propname1')

                req = QgsFeatureRequest().setFilterExpression("name = 'Apple'")
                f = QgsFeature()
                vl.getFeatures(req).nextFeature(f)
                self.assertTrue(f.isValid())
                self.assertEqual(f.attributes()[index1], 'myvalue')

                req = QgsFeatureRequest().setFilterExpression("name = 'Orange'")
                f = QgsFeature()
                vl.getFeatures(req).nextFeature(f)
                self.assertTrue(f.isValid())
                self.assertEqual(f.attributes()[index1], 'myvalue1')