Ejemplo n.º 1
0
    def testGettersSetters(self):
        """
        Test provider list
        """
        d = QgsProviderSublayerDetails()
        d.setProviderKey('key')
        self.assertEqual(d.providerKey(), 'key')

        d.setType(QgsMapLayerType.MeshLayer)
        self.assertEqual(d.type(), QgsMapLayerType.MeshLayer)

        d.setUri('some uri')
        self.assertEqual(d.uri(), 'some uri')

        d.setName('name')
        self.assertEqual(d.name(), 'name')

        d.setDescription('desc')
        self.assertEqual(d.description(), 'desc')

        d.setPath(['a', 'b', 'c'])
        self.assertEqual(d.path(), ['a', 'b', 'c'])

        self.assertEqual(d.featureCount(), Qgis.FeatureCountState.UnknownCount)
        d.setFeatureCount(1000)
        self.assertEqual(d.featureCount(), 1000)

        self.assertEqual(d.wkbType(), QgsWkbTypes.Unknown)
        d.setWkbType(QgsWkbTypes.Point)
        self.assertEqual(d.wkbType(), QgsWkbTypes.Point)

        d.setGeometryColumnName('geom_col')
        self.assertEqual(d.geometryColumnName(), 'geom_col')

        d.setLayerNumber(13)
        self.assertEqual(d.layerNumber(), 13)

        d.setDriverName('drv')
        self.assertEqual(d.driverName(), 'drv')

        d.setSkippedContainerScan(True)
        self.assertTrue(d.skippedContainerScan())
        d.setSkippedContainerScan(False)
        self.assertFalse(d.skippedContainerScan())

        d.setFlags(Qgis.SublayerFlag.SystemTable)
        self.assertEqual(d.flags(),
                         Qgis.SublayerFlags(Qgis.SublayerFlag.SystemTable))
        d.setFlags(Qgis.SublayerFlags())
        self.assertEqual(d.flags(), Qgis.SublayerFlags())
    def test_equality(self):
        """
        Test equality operator
        """
        d = QgsProviderSublayerDetails()
        d2 = QgsProviderSublayerDetails()
        d.setProviderKey('key')
        self.assertNotEqual(d, d2)
        d2.setProviderKey('key')
        self.assertEqual(d, d2)

        d.setType(QgsMapLayerType.MeshLayer)
        self.assertNotEqual(d, d2)
        d2.setType(QgsMapLayerType.MeshLayer)
        self.assertEqual(d, d2)

        d.setUri('some uri')
        self.assertNotEqual(d, d2)
        d2.setUri('some uri')
        self.assertEqual(d, d2)

        d.setName('name')
        self.assertNotEqual(d, d2)
        d2.setName('name')
        self.assertEqual(d, d2)

        d.setDescription('desc')
        self.assertNotEqual(d, d2)
        d2.setDescription('desc')
        self.assertEqual(d, d2)

        d.setPath(['a', 'b', 'c'])
        self.assertNotEqual(d, d2)
        d2.setPath(['a', 'b', 'c'])
        self.assertEqual(d, d2)

        d.setFeatureCount(1000)
        self.assertNotEqual(d, d2)
        d2.setFeatureCount(1000)
        self.assertEqual(d, d2)

        d.setWkbType(QgsWkbTypes.Point)
        self.assertNotEqual(d, d2)
        d2.setWkbType(QgsWkbTypes.Point)
        self.assertEqual(d, d2)

        d.setGeometryColumnName('geom_col')
        self.assertNotEqual(d, d2)
        d2.setGeometryColumnName('geom_col')
        self.assertEqual(d, d2)

        d.setLayerNumber(13)
        self.assertNotEqual(d, d2)
        d2.setLayerNumber(13)
        self.assertEqual(d, d2)

        d.setDriverName('drv')
        self.assertNotEqual(d, d2)
        d2.setDriverName('drv')
        self.assertEqual(d, d2)

        d.setSkippedContainerScan(True)
        self.assertNotEqual(d, d2)
        d2.setSkippedContainerScan(True)
        self.assertEqual(d, d2)
Ejemplo n.º 3
0
    def test_sublayerDetailsAreIncomplete(self):
        """
        Test sublayerDetailsAreIncomplete
        """
        uri = unitTestDataPath() + '/mixed_types.TAB'

        # surface scan only
        sublayers = QgsProviderRegistry.instance().querySublayers(uri)
        self.assertEqual(len(sublayers), 1)
        self.assertEqual(sublayers[0].wkbType(), QgsWkbTypes.Unknown)

        # need to resolve geometry types for complete details about this uri!
        self.assertTrue(
            QgsProviderUtils.sublayerDetailsAreIncomplete(sublayers))
        self.assertTrue(
            QgsProviderUtils.sublayerDetailsAreIncomplete(
                sublayers,
                QgsProviderUtils.SublayerCompletenessFlags(
                    QgsProviderUtils.SublayerCompletenessFlag.
                    IgnoreUnknownFeatureCount)))
        self.assertTrue(
            QgsProviderUtils.sublayerDetailsAreIncomplete(
                sublayers,
                QgsProviderUtils.SublayerCompletenessFlags(
                    QgsProviderUtils.SublayerCompletenessFlag.
                    IgnoreUnknownGeometryType)))
        # ...unless we are ignoring both unknown feature count and unknown geometry types
        self.assertFalse(
            QgsProviderUtils.sublayerDetailsAreIncomplete(
                sublayers,
                QgsProviderUtils.SublayerCompletenessFlags(
                    QgsProviderUtils.SublayerCompletenessFlag.
                    IgnoreUnknownFeatureCount | QgsProviderUtils.
                    SublayerCompletenessFlag.IgnoreUnknownGeometryType)))

        # fake feature count, now we have complete details if we ignore unknown geometry type
        sublayers[0].setFeatureCount(5)
        self.assertFalse(
            QgsProviderUtils.sublayerDetailsAreIncomplete(
                sublayers,
                QgsProviderUtils.SublayerCompletenessFlags(
                    QgsProviderUtils.SublayerCompletenessFlag.
                    IgnoreUnknownGeometryType)))

        # retry with retrieving geometry types
        sublayers = QgsProviderRegistry.instance().querySublayers(
            uri, Qgis.SublayerQueryFlag.ResolveGeometryType)
        # now we have all the details
        self.assertEqual(len(sublayers), 3)
        self.assertFalse(
            QgsProviderUtils.sublayerDetailsAreIncomplete(sublayers))
        self.assertFalse(
            QgsProviderUtils.sublayerDetailsAreIncomplete(
                sublayers,
                QgsProviderUtils.SublayerCompletenessFlags(
                    QgsProviderUtils.SublayerCompletenessFlag.
                    IgnoreUnknownFeatureCount)))
        self.assertFalse(
            QgsProviderUtils.sublayerDetailsAreIncomplete(
                sublayers,
                QgsProviderUtils.SublayerCompletenessFlags(
                    QgsProviderUtils.SublayerCompletenessFlag.
                    IgnoreUnknownGeometryType)))

        # this geopackage file requires manually requesting feature counts
        uri = unitTestDataPath() + '/mixed_layers.gpkg'

        # surface scan only
        sublayers = QgsProviderRegistry.instance().querySublayers(uri)
        self.assertEqual(len(sublayers), 4)
        self.assertEqual(sublayers[0].name(), 'band1')
        self.assertEqual(sublayers[1].name(), 'band2')
        self.assertEqual(sublayers[2].name(), 'points')
        self.assertEqual(sublayers[2].featureCount(),
                         Qgis.FeatureCountState.Uncounted)
        self.assertEqual(sublayers[3].name(), 'lines')
        self.assertEqual(sublayers[3].featureCount(),
                         Qgis.FeatureCountState.Uncounted)

        # need to count features for complete details about this uri!
        self.assertTrue(
            QgsProviderUtils.sublayerDetailsAreIncomplete(sublayers))
        self.assertTrue(
            QgsProviderUtils.sublayerDetailsAreIncomplete(
                sublayers,
                QgsProviderUtils.SublayerCompletenessFlags(
                    QgsProviderUtils.SublayerCompletenessFlag.
                    IgnoreUnknownGeometryType)))
        # ...unless we are ignoring unknown feature counts, that is...
        self.assertFalse(
            QgsProviderUtils.sublayerDetailsAreIncomplete(
                sublayers,
                QgsProviderUtils.SublayerCompletenessFlags(
                    QgsProviderUtils.SublayerCompletenessFlag.
                    IgnoreUnknownFeatureCount)))

        # retry with retrieving feature count
        sublayers = QgsProviderRegistry.instance().querySublayers(
            uri, Qgis.SublayerQueryFlag.CountFeatures)
        # now we have all the details
        self.assertEqual(len(sublayers), 4)
        self.assertFalse(
            QgsProviderUtils.sublayerDetailsAreIncomplete(
                sublayers,
                QgsProviderUtils.SublayerCompletenessFlags(
                    QgsProviderUtils.SublayerCompletenessFlag.
                    IgnoreUnknownFeatureCount)))
        self.assertFalse(
            QgsProviderUtils.sublayerDetailsAreIncomplete(
                sublayers,
                QgsProviderUtils.SublayerCompletenessFlags(
                    QgsProviderUtils.SublayerCompletenessFlag.
                    IgnoreUnknownGeometryType)))
        self.assertFalse(
            QgsProviderUtils.sublayerDetailsAreIncomplete(sublayers))
        self.assertEqual(sublayers[0].name(), 'band1')
        self.assertEqual(sublayers[1].name(), 'band2')
        self.assertEqual(sublayers[2].name(), 'points')
        self.assertEqual(sublayers[2].featureCount(), 0)
        self.assertEqual(sublayers[3].name(), 'lines')
        self.assertEqual(sublayers[3].featureCount(), 6)

        # test with sublayer with skippedContainerScan flag
        sl1 = QgsProviderSublayerDetails()
        sl1.setProviderKey('ogr')
        sl1.setType(QgsMapLayerType.VectorLayer)
        sl1.setWkbType(QgsWkbTypes.Point)
        sl1.setFeatureCount(1)
        sl1.setSkippedContainerScan(False)
        self.assertFalse(
            QgsProviderUtils.sublayerDetailsAreIncomplete(
                [sl1],
                QgsProviderUtils.SublayerCompletenessFlags(
                    QgsProviderUtils.SublayerCompletenessFlag.
                    IgnoreUnknownFeatureCount)))
        self.assertFalse(QgsProviderUtils.sublayerDetailsAreIncomplete([sl1]))
        sl2 = QgsProviderSublayerDetails()
        sl2.setProviderKey('ogr')
        sl2.setType(QgsMapLayerType.VectorLayer)
        sl2.setWkbType(QgsWkbTypes.Point)
        sl2.setFeatureCount(1)
        sl2.setSkippedContainerScan(True)
        self.assertTrue(
            QgsProviderUtils.sublayerDetailsAreIncomplete(
                [sl2],
                QgsProviderUtils.SublayerCompletenessFlags(
                    QgsProviderUtils.SublayerCompletenessFlag.
                    IgnoreUnknownFeatureCount)))
        self.assertTrue(QgsProviderUtils.sublayerDetailsAreIncomplete([sl2]))
        self.assertTrue(
            QgsProviderUtils.sublayerDetailsAreIncomplete(
                [sl1, sl2],
                QgsProviderUtils.SublayerCompletenessFlags(
                    QgsProviderUtils.SublayerCompletenessFlag.
                    IgnoreUnknownFeatureCount)))
        self.assertTrue(
            QgsProviderUtils.sublayerDetailsAreIncomplete([sl1, sl2]))