Skip to content

Commit

Permalink
Merge pull request #8730 from elpaso/bugfix-20812-fixgeometries-in-place
Browse files Browse the repository at this point in the history
Processing in-place do not check validity when fixing geometries
  • Loading branch information
elpaso authored Dec 22, 2018
2 parents 9200f53 + 40fb2ce commit 157747e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
8 changes: 8 additions & 0 deletions python/plugins/processing/gui/AlgorithmExecutor.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
QgsMessageLog,
QgsProcessingException,
QgsProcessingFeatureSourceDefinition,
QgsProcessingFeatureSource,
QgsProcessingParameters,
QgsProject,
QgsFeatureRequest,
Expand Down Expand Up @@ -94,6 +95,13 @@ def execute_in_place_run(alg, parameters, context=None, feedback=None, raise_exc
if context is None:
context = dataobjects.createContext(feedback)

# Only feature based algs have sourceFlags
try:
if alg.sourceFlags() & QgsProcessingFeatureSource.FlagSkipGeometryValidityChecks:
context.setInvalidGeometryCheck(QgsFeatureRequest.GeometryNoCheck)
except AttributeError:
pass

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
Original file line number Diff line number Diff line change
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 157747e

Please sign in to comment.