29
29
#include " qgsvectorlayer.h"
30
30
#include " qgscategorizedsymbolrenderer.h"
31
31
#include " qgssinglesymbolrenderer.h"
32
+ #include " qgsmultipolygon.h"
32
33
33
34
class TestQgsProcessingAlgs : public QObject
34
35
{
35
36
Q_OBJECT
36
37
38
+ private:
39
+
40
+ /* *
41
+ * Helper function to get a feature based algorithm.
42
+ */
43
+ std::unique_ptr<QgsProcessingFeatureBasedAlgorithm> featureBasedAlg ( const QString &id );
44
+
45
+ QgsFeature runForFeature ( const std::unique_ptr<QgsProcessingFeatureBasedAlgorithm> &alg, QgsFeature feature, const QString &layerType );
46
+
37
47
private slots:
38
48
void initTestCase ();// will be called before the first testfunction is executed.
39
49
void cleanupTestCase (); // will be called after the last testfunction was executed.
@@ -48,6 +58,8 @@ class TestQgsProcessingAlgs: public QObject
48
58
void kmeansCluster ();
49
59
void categorizeByStyle ();
50
60
void extractBinary ();
61
+ void polygonsToLines_data ();
62
+ void polygonsToLines ();
51
63
52
64
private:
53
65
@@ -57,6 +69,39 @@ class TestQgsProcessingAlgs: public QObject
57
69
58
70
};
59
71
72
+ std::unique_ptr<QgsProcessingFeatureBasedAlgorithm> TestQgsProcessingAlgs::featureBasedAlg ( const QString &id )
73
+ {
74
+ return std::unique_ptr<QgsProcessingFeatureBasedAlgorithm>( static_cast <QgsProcessingFeatureBasedAlgorithm *>( QgsApplication::processingRegistry ()->createAlgorithmById ( id ) ) );
75
+ }
76
+
77
+ QgsFeature TestQgsProcessingAlgs::runForFeature ( const std::unique_ptr< QgsProcessingFeatureBasedAlgorithm > &alg, QgsFeature feature, const QString &layerType )
78
+ {
79
+ std::unique_ptr< QgsProcessingContext > context = qgis::make_unique< QgsProcessingContext >();
80
+ QgsProject p;
81
+ context->setProject ( &p );
82
+
83
+ QgsProcessingFeedback feedback;
84
+ context->setFeedback ( &feedback );
85
+
86
+ QVariantMap parameters;
87
+
88
+ std::unique_ptr<QgsVectorLayer> inputLayer ( qgis::make_unique<QgsVectorLayer>( layerType, QStringLiteral ( " layer" ), QStringLiteral ( " memory" ) ) );
89
+ inputLayer->dataProvider ()->addFeature ( feature );
90
+
91
+ parameters.insert ( QStringLiteral ( " INPUT" ), QVariant::fromValue<QgsMapLayer *>( inputLayer.get () ) );
92
+
93
+ parameters.insert ( QStringLiteral ( " OUTPUT" ), QStringLiteral ( " memory:" ) );
94
+
95
+ bool ok = false ;
96
+ auto res = alg->run ( parameters, *context, &feedback, &ok );
97
+ QgsFeature result;
98
+
99
+ std::unique_ptr<QgsVectorLayer> outputLayer ( qobject_cast< QgsVectorLayer * >( context->getMapLayer ( res.value ( QStringLiteral ( " OUTPUT" ) ).toString () ) ) );
100
+ outputLayer->getFeatures ().nextFeature ( result );
101
+
102
+ return result;
103
+ }
104
+
60
105
void TestQgsProcessingAlgs::initTestCase ()
61
106
{
62
107
QgsApplication::init ();
@@ -668,5 +713,45 @@ void TestQgsProcessingAlgs::extractBinary()
668
713
}
669
714
670
715
716
+ void TestQgsProcessingAlgs::polygonsToLines_data ()
717
+ {
718
+ QTest::addColumn<QgsGeometry>( " sourceGeometry" );
719
+ QTest::addColumn<QgsGeometry>( " expectedGeometry" );
720
+
721
+ QTest::newRow ( " Simple Polygon" )
722
+ << QgsGeometry::fromWkt ( " Polygon((1 1, 2 2, 1 3, 1 1))" )
723
+ << QgsGeometry::fromWkt ( " MultiLineString ((1 1, 2 2, 1 3, 1 1))" );
724
+
725
+ QgsGeometry geomNoRing ( qgis::make_unique<QgsMultiPolygon>() );
726
+
727
+ QTest::newRow ( " Polygon without exterior ring" )
728
+ << geomNoRing
729
+ << QgsGeometry::fromWkt ( " MultiLineString ()" );
730
+
731
+ QTest::newRow ( " MultiPolygon" )
732
+ << QgsGeometry::fromWkt ( " MultiPolygon(((1 1, 2 2, 1 3, 1 1)), ((0 0, 0 10, 10 10, 10 0, 0 0), (3 3, 3 6, 6 6, 6 3, 3 3)))" )
733
+ << QgsGeometry::fromWkt ( " MultiLineString ((1 1, 2 2, 1 3, 1 1),(0 0, 0 10, 10 10, 10 0, 0 0),(3 3, 3 6, 6 6, 6 3, 3 3))" );
734
+
735
+ QTest::newRow ( " Polygon with inner ring" )
736
+ << QgsGeometry::fromWkt ( " Polygon((0 0, 0 10, 10 10, 10 0, 0 0), (3 3, 3 6, 6 6, 6 3, 3 3))" )
737
+ << QgsGeometry::fromWkt ( " MultiLineString ((0 0, 0 10, 10 10, 10 0, 0 0),(3 3, 3 6, 6 6, 6 3, 3 3))" );
738
+ }
739
+
740
+
741
+ void TestQgsProcessingAlgs::polygonsToLines ()
742
+ {
743
+ QFETCH ( QgsGeometry, sourceGeometry );
744
+ QFETCH ( QgsGeometry, expectedGeometry );
745
+
746
+ std::unique_ptr< QgsProcessingFeatureBasedAlgorithm > alg ( featureBasedAlg ( " native:polygonstolines" ) );
747
+
748
+ QgsFeature feature;
749
+ feature.setGeometry ( sourceGeometry );
750
+
751
+ QgsFeature result = runForFeature ( alg, feature, QStringLiteral ( " Polygon" ) );
752
+
753
+ QVERIFY2 ( result.geometry ().equals ( expectedGeometry ), QStringLiteral ( " Result: %1, Expected: %2" ).arg ( result.geometry ().asWkt (), expectedGeometry.asWkt () ).toUtf8 ().constData () );
754
+ }
755
+
671
756
QGSTEST_MAIN ( TestQgsProcessingAlgs )
672
757
#include " testqgsprocessingalgs.moc"
0 commit comments