Ejemplo n.º 1
0
    def test_select_all_features(self):
        """Check that if there is no selection, the alg will run on all features"""

        self.vl.rollBack()
        self.vl.removeSelection()
        old_count = self.vl.featureCount()

        context = QgsProcessingContext()
        context.setProject(QgsProject.instance())
        feedback = ConsoleFeedBack()

        alg = self.registry.createAlgorithmById('native:translategeometry')

        self.assertIsNotNone(alg)

        parameters = {
            'DELTA_X': 1.1,
            'DELTA_Y': 1.1,
        }
        parameters['INPUT'] = self.vl
        parameters['OUTPUT'] = 'memory:'

        old_features = [f for f in self.vl.getFeatures()]

        ok, _ = execute_in_place_run(alg,
                                     parameters,
                                     context=context,
                                     feedback=feedback,
                                     raise_exceptions=True)
        new_features = [f for f in self.vl.getFeatures()]

        self.assertEqual(len(new_features), old_count)

        # Check all are selected
        self.assertEqual(len(self.vl.selectedFeatureIds()), old_count)
Ejemplo n.º 2
0
    def _test_difference_on_invalid_geometries(self, geom_option):
        polygon_layer = self._make_layer('Polygon')
        self.assertTrue(polygon_layer.startEditing())
        f = QgsFeature(polygon_layer.fields())
        f.setAttributes([1])
        # Flake!
        f.setGeometry(
            QgsGeometry.fromWkt('Polygon ((0 0, 2 2, 0 2, 2 0, 0 0))'))
        self.assertTrue(f.isValid())
        self.assertTrue(polygon_layer.addFeatures([f]))
        polygon_layer.commitChanges()
        polygon_layer.rollBack()
        self.assertEqual(polygon_layer.featureCount(), 1)

        overlay_layer = self._make_layer('Polygon')
        self.assertTrue(overlay_layer.startEditing())
        f = QgsFeature(overlay_layer.fields())
        f.setAttributes([1])
        f.setGeometry(
            QgsGeometry.fromWkt('Polygon ((0 0, 2 0, 2 2, 0 2, 0 0))'))
        self.assertTrue(f.isValid())
        self.assertTrue(overlay_layer.addFeatures([f]))
        overlay_layer.commitChanges()
        overlay_layer.rollBack()
        self.assertEqual(overlay_layer.featureCount(), 1)

        QgsProject.instance().addMapLayers([polygon_layer, overlay_layer])

        old_features = [f for f in polygon_layer.getFeatures()]

        # 'Ignore features with invalid geometries' = 1
        ProcessingConfig.setSettingValue(
            ProcessingConfig.FILTER_INVALID_GEOMETRIES, geom_option)

        feedback = ConsoleFeedBack()
        context = dataobjects.createContext(feedback)
        context.setProject(QgsProject.instance())

        alg = self.registry.createAlgorithmById('native:difference')
        self.assertIsNotNone(alg)

        parameters = {
            'OVERLAY': overlay_layer,
            'INPUT': polygon_layer,
            'OUTPUT': ':memory',
        }

        old_features = [f for f in polygon_layer.getFeatures()]

        self.assertTrue(polygon_layer.startEditing())
        polygon_layer.selectAll()
        ok, _ = execute_in_place_run(alg,
                                     parameters,
                                     context=context,
                                     feedback=feedback,
                                     raise_exceptions=True)

        new_features = [f for f in polygon_layer.getFeatures()]

        return old_features, new_features
Ejemplo n.º 3
0
    def _alg_tester(self, alg_name, input_layer, parameters):

        alg = self.registry.createAlgorithmById(alg_name)

        self.assertIsNotNone(alg)
        parameters['INPUT'] = input_layer
        parameters['OUTPUT'] = 'memory:'

        old_features = [f for f in input_layer.getFeatures()]
        input_layer.selectByIds([old_features[0].id()])
        # Check selected
        self.assertEqual(input_layer.selectedFeatureIds(),
                         [old_features[0].id()], alg_name)

        context = QgsProcessingContext()
        context.setProject(QgsProject.instance())
        feedback = ConsoleFeedBack()

        input_layer.rollBack()
        ok = False
        ok, _ = execute_in_place_run(alg,
                                     parameters,
                                     context=context,
                                     feedback=feedback,
                                     raise_exceptions=True)
        new_features = [f for f in input_layer.getFeatures()]

        # Check ret values
        self.assertTrue(ok, alg_name)

        # Check geometry types (drop Z or M)
        self.assertEqual(new_features[0].geometry().wkbType(),
                         old_features[0].geometry().wkbType())

        return old_features, new_features
Ejemplo n.º 4
0
    def test_select_all_features(self):
        """Check that if there is no selection, the alg will run on all features"""

        self.vl.rollBack()
        self.vl.removeSelection()
        old_count = self.vl.featureCount()

        context = QgsProcessingContext()
        context.setProject(QgsProject.instance())
        feedback = ConsoleFeedBack()

        alg = self.registry.createAlgorithmById('native:translategeometry')

        self.assertIsNotNone(alg)

        parameters = {
            'DELTA_X': 1.1,
            'DELTA_Y': 1.1,
        }
        parameters['INPUT'] = self.vl
        parameters['OUTPUT'] = 'memory:'

        old_features = [f for f in self.vl.getFeatures()]

        ok, _ = execute_in_place_run(
            alg, parameters, context=context, feedback=feedback, raise_exceptions=True)
        new_features = [f for f in self.vl.getFeatures()]

        self.assertEqual(len(new_features), old_count)

        # Check all are selected
        self.assertEqual(len(self.vl.selectedFeatureIds()), old_count)
Ejemplo n.º 5
0
    def _alg_tester(self, alg_name, input_layer, parameters):

        alg = self.registry.createAlgorithmById(alg_name)

        self.assertIsNotNone(alg)
        parameters['INPUT'] = input_layer
        parameters['OUTPUT'] = 'memory:'

        old_features = [f for f in input_layer.getFeatures()]
        input_layer.selectByIds([old_features[0].id()])
        # Check selected
        self.assertEqual(input_layer.selectedFeatureIds(), [old_features[0].id()], alg_name)

        context = QgsProcessingContext()
        context.setProject(QgsProject.instance())
        feedback = ConsoleFeedBack()

        input_layer.rollBack()
        ok = False
        ok, _ = execute_in_place_run(
            alg, parameters, context=context, feedback=feedback, raise_exceptions=True)
        new_features = [f for f in input_layer.getFeatures()]

        # Check ret values
        self.assertTrue(ok, alg_name)

        # Check geometry types (drop Z or M)
        self.assertEqual(new_features[0].geometry().wkbType(), old_features[0].geometry().wkbType())

        return old_features, new_features
Ejemplo n.º 6
0
    def _test_difference_on_invalid_geometries(self, geom_option):
        polygon_layer = self._make_layer('Polygon')
        self.assertTrue(polygon_layer.startEditing())
        f = QgsFeature(polygon_layer.fields())
        f.setAttributes([1])
        # Flake!
        f.setGeometry(QgsGeometry.fromWkt('Polygon ((0 0, 2 2, 0 2, 2 0, 0 0))'))
        self.assertTrue(f.isValid())
        self.assertTrue(polygon_layer.addFeatures([f]))
        polygon_layer.commitChanges()
        polygon_layer.rollBack()
        self.assertEqual(polygon_layer.featureCount(), 1)

        overlay_layer = self._make_layer('Polygon')
        self.assertTrue(overlay_layer.startEditing())
        f = QgsFeature(overlay_layer.fields())
        f.setAttributes([1])
        f.setGeometry(QgsGeometry.fromWkt('Polygon ((0 0, 2 0, 2 2, 0 2, 0 0))'))
        self.assertTrue(f.isValid())
        self.assertTrue(overlay_layer.addFeatures([f]))
        overlay_layer.commitChanges()
        overlay_layer.rollBack()
        self.assertEqual(overlay_layer.featureCount(), 1)

        QgsProject.instance().addMapLayers([polygon_layer, overlay_layer])

        old_features = [f for f in polygon_layer.getFeatures()]

        # 'Ignore features with invalid geometries' = 1
        ProcessingConfig.setSettingValue(ProcessingConfig.FILTER_INVALID_GEOMETRIES, geom_option)

        feedback = ConsoleFeedBack()
        context = dataobjects.createContext(feedback)
        context.setProject(QgsProject.instance())

        alg = self.registry.createAlgorithmById('native:difference')
        self.assertIsNotNone(alg)

        parameters = {
            'OVERLAY': overlay_layer,
            'INPUT': polygon_layer,
            'OUTPUT': ':memory',
        }

        old_features = [f for f in polygon_layer.getFeatures()]

        self.assertTrue(polygon_layer.startEditing())
        polygon_layer.selectAll()
        ok, _ = execute_in_place_run(
            alg, parameters, context=context, feedback=feedback, raise_exceptions=True)

        new_features = [f for f in polygon_layer.getFeatures()]

        return old_features, new_features
Ejemplo n.º 7
0
    def test_unique_constraints(self):
        """Test issue #31634"""
        temp_dir = QTemporaryDir()
        temp_path = temp_dir.path()
        gpkg_name = 'bug_31634_Multi_to_Singleparts_FID.gpkg'
        gpkg_path = os.path.join(temp_path, gpkg_name)
        shutil.copyfile(os.path.join(unitTestDataPath(), gpkg_name), gpkg_path)

        gpkg_layer = QgsVectorLayer(
            gpkg_path + '|layername=Multi_to_Singleparts_FID_bug', 'lyr',
            'ogr')
        self.assertTrue(gpkg_layer.isValid())
        QgsProject.instance().addMapLayers([gpkg_layer])

        # Test that makeFeaturesCompatible set to NULL unique constraint fields
        feature = next(gpkg_layer.getFeatures())

        feedback = ConsoleFeedBack()
        context = dataobjects.createContext(feedback)
        context.setProject(QgsProject.instance())

        alg = self.registry.createAlgorithmById(
            'native:multiparttosingleparts')
        self.assertIsNotNone(alg)

        parameters = {
            'INPUT': gpkg_layer,
            'OUTPUT': ':memory',
        }

        ok, _ = execute_in_place_run(alg,
                                     parameters,
                                     context=context,
                                     feedback=feedback,
                                     raise_exceptions=True)

        pks = set()
        for f in gpkg_layer.getFeatures():
            pks.add(f.attribute(0))

        self.assertTrue(gpkg_layer.commitChanges())