Skip to content
Permalink
Browse files

Some tests for multi/single part

  • Loading branch information
elpaso authored and nyalldawson committed Sep 14, 2018
1 parent ac55ce0 commit d8d32ac81035a1d222fea50a16fc8e45331e3955
Showing with 89 additions and 25 deletions.
  1. +89 −25 tests/src/python/test_qgsprocessinginplace.py
@@ -13,16 +13,19 @@
__revision__ = '$Format:%H$'

from qgis.PyQt.QtCore import QCoreApplication, QVariant
from qgis.core import QgsFeature, QgsGeometry, QgsSettings, QgsApplication, QgsMemoryProviderUtils, QgsWkbTypes, QgsField, QgsFields, QgsProcessingFeatureSourceDefinition, QgsProcessingContext, QgsProcessingFeedback
from qgis.core import (
QgsFeature, QgsGeometry, QgsSettings, QgsApplication, QgsMemoryProviderUtils, QgsWkbTypes, QgsField, QgsFields, QgsProcessingFeatureSourceDefinition, QgsProcessingContext, QgsProcessingFeedback, QgsCoordinateReferenceSystem, QgsProject, QgsProcessingException
)
from processing.core.Processing import Processing
from processing.gui.AlgorithmExecutor import execute_in_place_run
from qgis.testing import start_app, unittest
from qgis.PyQt.QtTest import QSignalSpy
from qgis.analysis import QgsNativeAlgorithms

start_app()


class TestQgsProcessingRecentAlgorithmLog(unittest.TestCase):
class TestQgsProcessingInPlace(unittest.TestCase):

@classmethod
def setUpClass(cls):
@@ -38,7 +41,7 @@ def setUpClass(cls):
fields = QgsFields()
fields.append(QgsField('int_f', QVariant.Int))
cls.vl = QgsMemoryProviderUtils.createMemoryLayer(
'mylayer', fields, QgsWkbTypes.Point)
'mylayer', fields, QgsWkbTypes.Point, QgsCoordinateReferenceSystem(4326))

f1 = QgsFeature(cls.vl.fields())
f1['int_f'] = 1
@@ -51,38 +54,99 @@ def setUpClass(cls):
assert cls.vl.isValid()
assert cls.vl.featureCount() == 2

def test_algs(self):
# Clone?
alg = self.registry.algorithmById('native:translategeometry')
# Multipolygon layer

cls.multipoly_vl = QgsMemoryProviderUtils.createMemoryLayer(
'mymultiplayer', fields, QgsWkbTypes.MultiPolygon, QgsCoordinateReferenceSystem(4326))

f3 = QgsFeature(cls.multipoly_vl.fields())
f3.setGeometry(QgsGeometry.fromWkt('MultiPolygon (((2.81856297539240419 41.98170998812887689, 2.81874467773035464 41.98167537995160359, 2.81879535908157752 41.98154066615795443, 2.81866433873670452 41.98144056064155905, 2.81848263699778379 41.98147516865246587, 2.81843195500470811 41.98160988234612034, 2.81856297539240419 41.98170998812887689)),((2.81898589063455907 41.9815711567298635, 2.81892080450418803 41.9816030048432367, 2.81884192631866437 41.98143737613141724, 2.8190679469505846 41.98142270931093378, 2.81898589063455907 41.9815711567298635)))'))
f4 = QgsFeature(cls.multipoly_vl.fields())
f4.setGeometry(QgsGeometry.fromWkt('MultiPolygon (((2.81823679385631332 41.98133290154246566, 2.81830770255185703 41.98123540208609228, 2.81825871989355159 41.98112524362621656, 2.81813882853970243 41.98111258462271422, 2.81806791984415872 41.98121008407908761, 2.81811690250246416 41.98132024253896333, 2.81823679385631332 41.98133290154246566)),((2.81835835162010895 41.98123286963267731, 2.8183127674586852 41.98108725356146209, 2.8184520523963692 41.98115436357689134, 2.81835835162010895 41.98123286963267731)))'))
cls.multipoly_vl.dataProvider().addFeatures([f3, f4])

assert cls.multipoly_vl.isValid()
assert cls.multipoly_vl.featureCount() == 2

QgsProject.instance().addMapLayers([cls.vl, cls.multipoly_vl])

def _alg_tester(self, alg_name, input_layer, parameters):

alg = self.registry.createAlgorithmById(alg_name)

self.assertIsNotNone(alg)
parameters = {}
parameters['INPUT'] = QgsProcessingFeatureSourceDefinition(self.vl.id(), True)
parameters['INPUT'] = QgsProcessingFeatureSourceDefinition(
input_layer.id(), True)
parameters['OUTPUT'] = 'memory:'
parameters['DELTA_X'] = 1.1
parameters['DELTA_Y'] = 1.1

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 = QgsProcessingFeedback()
self.assertTrue(alg.prepare(parameters, context, feedback))
# Failing:
#self.assertEqual(alg.parameterAsVectorLayer(parameters, 'INPUT', context ), input_layer)

def _f():
execute_in_place_run(
alg, parameters, context=context, feedback=feedback)

self.assertRaises(QgsProcessingException, _f)

input_layer.startEditing()
ok, _ = execute_in_place_run(
alg, parameters, context=context, feedback=feedback)

self.assertTrue(ok, alg_name)

return old_features, [f for f in input_layer.getFeatures()]

def test_execute_in_place_run(self):
"""Test the execution in place"""

old_features, new_features = self._alg_tester(
'native:translategeometry',
self.vl,
{
'DELTA_X': 1.1,
'DELTA_Y': 1.1,
}
)

# First feature was selected and modified
self.assertEqual(new_features[0].id(), old_features[0].id())
self.assertAlmostEqual(new_features[0].geometry().asPoint().x(), old_features[0].geometry().asPoint().x() + 1.1, delta=0.01)
self.assertAlmostEqual(new_features[0].geometry().asPoint().y(), old_features[0].geometry().asPoint().y() + 1.1, delta=0.01)

# Second feature was not selected and not modified
self.assertEqual(new_features[1].id(), old_features[1].id())
self.assertEqual(new_features[1].geometry().asPoint().x(), old_features[1].geometry().asPoint().x())
self.assertEqual(new_features[1].geometry().asPoint().y(), old_features[1].geometry().asPoint().y())

# Check selected
self.assertEqual(self.vl.selectedFeatureIds(), [old_features[0].id()])

self.assertTrue(self.vl.startEditing())
def test_multi_to_single(self):
"""Check that the geometry type is still multi after the alg is run"""

field_idxs = range(len(self.vl.fields()))
old_features, new_features = self._alg_tester(
'native:multiparttosingleparts',
self.multipoly_vl,
{
}
)

new_features = {}
for f in self.vl.getFeatures():
new_f = alg.processFeature(f, context, feedback)[0]
new_features[f.id()] = new_f
self.assertEqual(new_f.id(), f.id())
# This alg does change the geometry!
self.vl.changeGeometry(f.id(), new_f.geometry())
# This alg does not change attrs but some other might do
self.vl.changeAttributeValues(f.id(), dict(zip(field_idxs, new_f.attributes())), dict(zip(field_idxs, f.attributes())))
self.assertEqual(len(new_features), 3)

self.assertTrue(self.vl.commitChanges())
# Check selected
self.assertEqual(sorted(self.multipoly_vl.selectedFeatureIds()), [-3, -2])

for f in self.vl.getFeatures():
self.assertEqual(new_features[f.id()].geometry().asWkt(), f.geometry().asWkt())
def test_make_compatible(self):
"""Test fixer function"""
pass


if __name__ == '__main__':

0 comments on commit d8d32ac

Please sign in to comment.
You can’t perform that action at this time.