Skip to content

Commit

Permalink
Processing in-place do not check validity when fixing geometries
Browse files Browse the repository at this point in the history
Fixes #20812
  • Loading branch information
elpaso committed Dec 21, 2018
1 parent 657f5a4 commit f4d7506
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
4 changes: 4 additions & 0 deletions python/plugins/processing/gui/AlgorithmExecutor.py
Expand Up @@ -94,6 +94,10 @@ def execute_in_place_run(alg, parameters, context=None, feedback=None, raise_exc
if context is None:
context = dataobjects.createContext(feedback)

# Ugly hack: the only invalid policy option that makes sense for fixgeometries is to not check
if alg.name() in ('fixgeometries', ):
context.setInvalidGeometryCheck(QgsFeatureRequest.GeometryNoCheck)

active_layer = parameters['INPUT']

# Run some checks and prepare the layer for in-place execution by:
Expand Down
22 changes: 13 additions & 9 deletions tests/src/python/test_qgsprocessinginplace.py
Expand Up @@ -12,6 +12,7 @@
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

import re
from qgis.PyQt.QtCore import QCoreApplication, QVariant
from qgis.core import (
QgsFeature, QgsGeometry, QgsSettings, QgsApplication, QgsMemoryProviderUtils, QgsWkbTypes, QgsField, QgsFields, QgsProcessingFeatureSourceDefinition, QgsProcessingContext, QgsProcessingFeedback, QgsCoordinateReferenceSystem, QgsProject, QgsProcessingException
Expand All @@ -23,7 +24,7 @@
from qgis.testing import start_app, unittest
from qgis.PyQt.QtTest import QSignalSpy
from qgis.analysis import QgsNativeAlgorithms
from qgis.core import QgsVectorLayerUtils
from qgis.core import QgsVectorLayerUtils, QgsFeatureRequest

start_app()

Expand Down Expand Up @@ -349,7 +350,7 @@ def test_make_features_compatible_geometry(self):
self.assertEqual(len(new_features), 1)
self.assertEqual(new_features[0].geometry().asWkt(), '')

def _alg_tester(self, alg_name, input_layer, parameters):
def _alg_tester(self, alg_name, input_layer, parameters, invalid_geometry_policy=QgsFeatureRequest.GeometryNoCheck):

alg = self.registry.createAlgorithmById(alg_name)

Expand All @@ -363,6 +364,7 @@ def _alg_tester(self, alg_name, input_layer, parameters):
self.assertEqual(input_layer.selectedFeatureIds(), [old_features[0].id()], alg_name)

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

Expand Down Expand Up @@ -611,16 +613,16 @@ def test_fix_geometries(self):

polygon_layer = self._make_layer('Polygon')
self.assertTrue(polygon_layer.startEditing())
f = QgsFeature(polygon_layer.fields())
f.setAttributes([1])
f1 = QgsFeature(polygon_layer.fields())
f1.setAttributes([1])
# Flake!
f.setGeometry(QgsGeometry.fromWkt('POLYGON ((0 0, 2 2, 0 2, 2 0, 0 0))'))
self.assertTrue(f.isValid())
f1.setGeometry(QgsGeometry.fromWkt('POLYGON ((0 0, 2 2, 0 2, 2 0, 0 0))'))
self.assertTrue(f1.isValid())
f2 = QgsFeature(polygon_layer.fields())
f2.setAttributes([1])
f2.setGeometry(QgsGeometry.fromWkt('POLYGON((1.1 1.1, 1.1 2.1, 2.1 2.1, 2.1 1.1, 1.1 1.1))'))
self.assertTrue(f2.isValid())
self.assertTrue(polygon_layer.addFeatures([f, f2]))
self.assertTrue(polygon_layer.addFeatures([f1, f2]))
polygon_layer.commitChanges()
polygon_layer.rollBack()
self.assertEqual(polygon_layer.featureCount(), 2)
Expand All @@ -631,12 +633,14 @@ def test_fix_geometries(self):
'native:fixgeometries',
polygon_layer,
{
}
},
QgsFeatureRequest.GeometrySkipInvalid
)
self.assertEqual(polygon_layer.featureCount(), 3)
wkt1, wkt2, _ = [f.geometry().asWkt() for f in new_features]
wkt1, wkt2, wkt3 = [f.geometry().asWkt() for f in new_features]
self.assertEqual(wkt1, 'Polygon ((0 0, 1 1, 2 0, 0 0))')
self.assertEqual(wkt2, 'Polygon ((1 1, 0 2, 2 2, 1 1))')
self.assertEqual(re.sub(r'0000\d+', '', wkt3), 'Polygon ((1.1 1.1, 1.1 2.1, 2.1 2.1, 2.1 1.1, 1.1 1.1))')

# Test with Z (interpolated)
polygonz_layer = self._make_layer('PolygonZ')
Expand Down

0 comments on commit f4d7506

Please sign in to comment.