@@ -314,8 +314,10 @@ class TestQgsProcessing: public QObject
314
314
void parameterString ();
315
315
void parameterExpression ();
316
316
void parameterField ();
317
+ void parameterVectorLayer ();
317
318
void parameterFeatureSource ();
318
319
void parameterFeatureSink ();
320
+ void parameterVectorOut ();
319
321
void parameterRasterOut ();
320
322
void parameterFileOut ();
321
323
void parameterFolderOut ();
@@ -2593,6 +2595,96 @@ void TestQgsProcessing::parameterField()
2593
2595
QCOMPARE ( fields, QStringList () << " abc" << " def" );
2594
2596
}
2595
2597
2598
+ void TestQgsProcessing::parameterVectorLayer ()
2599
+ {
2600
+ // setup a context
2601
+ QgsProject p;
2602
+ p.setCrs ( QgsCoordinateReferenceSystem::fromEpsgId ( 28353 ) );
2603
+ QString testDataDir = QStringLiteral ( TEST_DATA_DIR ) + ' /' ; // defined in CmakeLists.txt
2604
+ QString vector1 = testDataDir + " multipoint.shp" ;
2605
+ QString raster = testDataDir + " landsat.tif" ;
2606
+ QFileInfo fi1 ( raster );
2607
+ QFileInfo fi2 ( vector1 );
2608
+ QgsRasterLayer *r1 = new QgsRasterLayer ( fi1.filePath (), " R1" );
2609
+ QgsVectorLayer *v1 = new QgsVectorLayer ( fi2.filePath (), " V4" , " ogr" );
2610
+ p.addMapLayers ( QList<QgsMapLayer *>() << v1 << r1 );
2611
+ QgsProcessingContext context;
2612
+ context.setProject ( &p );
2613
+
2614
+ // not optional!
2615
+ std::unique_ptr< QgsProcessingParameterVectorLayer > def ( new QgsProcessingParameterVectorLayer ( " non_optional" , QString (), QString ( " somelayer" ), false ) );
2616
+ QVERIFY ( !def->checkValueIsAcceptable ( false ) );
2617
+ QVERIFY ( !def->checkValueIsAcceptable ( true ) );
2618
+ QVERIFY ( !def->checkValueIsAcceptable ( 5 ) );
2619
+ QVERIFY ( def->checkValueIsAcceptable ( " layer12312312" ) );
2620
+ QVERIFY ( !def->checkValueIsAcceptable ( " " ) );
2621
+ QVERIFY ( !def->checkValueIsAcceptable ( QVariant () ) );
2622
+ QVERIFY ( !def->checkValueIsAcceptable ( QgsProcessingFeatureSourceDefinition ( " layer1231123" ) ) );
2623
+ QVERIFY ( def->checkValueIsAcceptable ( QVariant::fromValue ( v1 ) ) );
2624
+ QVERIFY ( !def->checkValueIsAcceptable ( QVariant::fromValue ( r1 ) ) );
2625
+
2626
+ // should be OK
2627
+ QVERIFY ( def->checkValueIsAcceptable ( " c:/Users/admin/Desktop/roads_clipped_transformed_v1_reprojected_final_clipped_aAAA.shp" ) );
2628
+ // ... unless we use context, when the check that the layer actually exists is performed
2629
+ QVERIFY ( !def->checkValueIsAcceptable ( " c:/Users/admin/Desktop/roads_clipped_transformed_v1_reprojected_final_clipped_aAAA.shp" , &context ) );
2630
+
2631
+ // using existing map layer ID
2632
+ QVariantMap params;
2633
+ params.insert ( " non_optional" , v1->id () );
2634
+ QCOMPARE ( QgsProcessingParameters::parameterAsVectorLayer ( def.get (), params, context )->id (), v1->id () );
2635
+
2636
+ // using existing layer
2637
+ params.insert ( " non_optional" , QVariant::fromValue ( v1 ) );
2638
+ QCOMPARE ( QgsProcessingParameters::parameterAsVectorLayer ( def.get (), params, context )->id (), v1->id () );
2639
+
2640
+ // not vector layer
2641
+ params.insert ( " non_optional" , r1->id () );
2642
+ QVERIFY ( !QgsProcessingParameters::parameterAsVectorLayer ( def.get (), params, context ) );
2643
+
2644
+ // using existing non-vector layer
2645
+ params.insert ( " non_optional" , QVariant::fromValue ( r1 ) );
2646
+ QVERIFY ( !QgsProcessingParameters::parameterAsVectorLayer ( def.get (), params, context ) );
2647
+
2648
+ // string representing a layer source
2649
+ params.insert ( " non_optional" , vector1 );
2650
+ QCOMPARE ( QgsProcessingParameters::parameterAsVectorLayer ( def.get (), params, context )->publicSource (), vector1 );
2651
+
2652
+ // nonsense string
2653
+ params.insert ( " non_optional" , QString ( " i'm not a layer, and nothing you can do will make me one" ) );
2654
+ QVERIFY ( !QgsProcessingParameters::parameterAsVectorLayer ( def.get (), params, context ) );
2655
+
2656
+ QCOMPARE ( def->valueAsPythonString ( vector1, context ), QString ( " '" ) + testDataDir + QStringLiteral ( " multipoint.shp'" ) );
2657
+ QCOMPARE ( def->valueAsPythonString ( v1->id (), context ), QString ( " '" ) + testDataDir + QStringLiteral ( " multipoint.shp'" ) );
2658
+ QCOMPARE ( def->valueAsPythonString ( QVariant::fromValue ( v1 ), context ), QString ( " '" ) + testDataDir + QStringLiteral ( " multipoint.shp'" ) );
2659
+ QCOMPARE ( def->valueAsPythonString ( QVariant::fromValue ( QgsProperty::fromExpression ( " \" a\" =1" ) ), context ), QStringLiteral ( " QgsProperty.fromExpression('\" a\" =1')" ) );
2660
+ QVariantMap map = def->toVariantMap ();
2661
+ QgsProcessingParameterVectorLayer fromMap ( " x" );
2662
+ QVERIFY ( fromMap.fromVariantMap ( map ) );
2663
+ QCOMPARE ( fromMap.name (), def->name () );
2664
+ QCOMPARE ( fromMap.description (), def->description () );
2665
+ QCOMPARE ( fromMap.flags (), def->flags () );
2666
+ QCOMPARE ( fromMap.defaultValue (), def->defaultValue () );
2667
+ def.reset ( dynamic_cast < QgsProcessingParameterVectorLayer *>( QgsProcessingParameters::parameterFromVariantMap ( map ) ) );
2668
+ QVERIFY ( dynamic_cast < QgsProcessingParameterVectorLayer *>( def.get () ) );
2669
+
2670
+ // optional
2671
+ def.reset ( new QgsProcessingParameterVectorLayer ( " optional" , QString (), v1->id (), true ) );
2672
+ params.insert ( " optional" , QVariant () );
2673
+ QCOMPARE ( QgsProcessingParameters::parameterAsVectorLayer ( def.get (), params, context )->id (), v1->id () );
2674
+ QVERIFY ( def->checkValueIsAcceptable ( false ) );
2675
+ QVERIFY ( def->checkValueIsAcceptable ( true ) );
2676
+ QVERIFY ( def->checkValueIsAcceptable ( 5 ) );
2677
+ QVERIFY ( def->checkValueIsAcceptable ( " layer12312312" ) );
2678
+ QVERIFY ( def->checkValueIsAcceptable ( " c:/Users/admin/Desktop/roads_clipped_transformed_v1_reprojected_final_clipped_aAAA.shp" ) );
2679
+ QVERIFY ( def->checkValueIsAcceptable ( " " ) );
2680
+ QVERIFY ( def->checkValueIsAcceptable ( QVariant () ) );
2681
+ QVERIFY ( def->checkValueIsAcceptable ( QgsProcessingFeatureSourceDefinition ( " layer1231123" ) ) );
2682
+
2683
+ // optional with direct layer default
2684
+ def.reset ( new QgsProcessingParameterVectorLayer ( " optional" , QString (), QVariant::fromValue ( v1 ), true ) );
2685
+ QCOMPARE ( QgsProcessingParameters::parameterAsVectorLayer ( def.get (), params, context )->id (), v1->id () );
2686
+ }
2687
+
2596
2688
void TestQgsProcessing::parameterFeatureSource ()
2597
2689
{
2598
2690
// setup a context
@@ -2747,6 +2839,68 @@ void TestQgsProcessing::parameterFeatureSink()
2747
2839
2748
2840
}
2749
2841
2842
+ void TestQgsProcessing::parameterVectorOut ()
2843
+ {
2844
+ // setup a context
2845
+ QgsProject p;
2846
+ p.setCrs ( QgsCoordinateReferenceSystem::fromEpsgId ( 28353 ) );
2847
+ QgsProcessingContext context;
2848
+ context.setProject ( &p );
2849
+
2850
+ // not optional!
2851
+ std::unique_ptr< QgsProcessingParameterVectorOutput > def ( new QgsProcessingParameterVectorOutput ( " non_optional" , QString (), QgsProcessingParameterDefinition::TypeVectorAny, QString ( " EPSG:3113" ), false ) );
2852
+ QVERIFY ( !def->checkValueIsAcceptable ( false ) );
2853
+ QVERIFY ( !def->checkValueIsAcceptable ( true ) );
2854
+ QVERIFY ( !def->checkValueIsAcceptable ( 5 ) );
2855
+ QVERIFY ( def->checkValueIsAcceptable ( " layer12312312" ) );
2856
+ QVERIFY ( !def->checkValueIsAcceptable ( " " ) );
2857
+ QVERIFY ( !def->checkValueIsAcceptable ( QVariant () ) );
2858
+ QVERIFY ( def->checkValueIsAcceptable ( QgsProcessingOutputLayerDefinition ( " layer1231123" ) ) );
2859
+
2860
+ // should be OK with or without context - it's an output layer!
2861
+ QVERIFY ( def->checkValueIsAcceptable ( " c:/Users/admin/Desktop/roads_clipped_transformed_v1_reprojected_final_clipped_aAAA.shp" ) );
2862
+ QVERIFY ( def->checkValueIsAcceptable ( " c:/Users/admin/Desktop/roads_clipped_transformed_v1_reprojected_final_clipped_aAAA.shp" , &context ) );
2863
+
2864
+ QCOMPARE ( def->valueAsPythonString ( QStringLiteral ( " abc" ), context ), QStringLiteral ( " 'abc'" ) );
2865
+ QCOMPARE ( def->valueAsPythonString ( QVariant::fromValue ( QgsProcessingOutputLayerDefinition ( " abc" ) ), context ), QStringLiteral ( " QgsProcessingOutputLayerDefinition('abc')" ) );
2866
+ QCOMPARE ( def->valueAsPythonString ( QVariant::fromValue ( QgsProcessingOutputLayerDefinition ( QgsProperty::fromValue ( " abc" ) ) ), context ), QStringLiteral ( " QgsProcessingOutputLayerDefinition('abc')" ) );
2867
+ QCOMPARE ( def->valueAsPythonString ( QVariant::fromValue ( QgsProcessingOutputLayerDefinition ( QgsProperty::fromExpression ( " \" abc\" || \" def\" " ) ) ), context ), QStringLiteral ( " QgsProcessingOutputLayerDefinition(QgsProperty.fromExpression('\" abc\" || \" def\" '))" ) );
2868
+ QCOMPARE ( def->valueAsPythonString ( QVariant::fromValue ( QgsProperty::fromExpression ( " \" a\" =1" ) ), context ), QStringLiteral ( " QgsProperty.fromExpression('\" a\" =1')" ) );
2869
+
2870
+ QVariantMap map = def->toVariantMap ();
2871
+ QgsProcessingParameterVectorOutput fromMap ( " x" );
2872
+ QVERIFY ( fromMap.fromVariantMap ( map ) );
2873
+ QCOMPARE ( fromMap.name (), def->name () );
2874
+ QCOMPARE ( fromMap.description (), def->description () );
2875
+ QCOMPARE ( fromMap.flags (), def->flags () );
2876
+ QCOMPARE ( fromMap.defaultValue (), def->defaultValue () );
2877
+ QCOMPARE ( fromMap.dataType (), def->dataType () );
2878
+ def.reset ( dynamic_cast < QgsProcessingParameterVectorOutput *>( QgsProcessingParameters::parameterFromVariantMap ( map ) ) );
2879
+ QVERIFY ( dynamic_cast < QgsProcessingParameterVectorOutput *>( def.get () ) );
2880
+
2881
+ // optional
2882
+ def.reset ( new QgsProcessingParameterVectorOutput ( " optional" , QString (), QgsProcessingParameterDefinition::TypeVectorAny, QString (), true ) );
2883
+ QVERIFY ( !def->checkValueIsAcceptable ( false ) );
2884
+ QVERIFY ( !def->checkValueIsAcceptable ( true ) );
2885
+ QVERIFY ( !def->checkValueIsAcceptable ( 5 ) );
2886
+ QVERIFY ( def->checkValueIsAcceptable ( " layer12312312" ) );
2887
+ QVERIFY ( def->checkValueIsAcceptable ( " c:/Users/admin/Desktop/roads_clipped_transformed_v1_reprojected_final_clipped_aAAA.shp" ) );
2888
+ QVERIFY ( def->checkValueIsAcceptable ( " " ) );
2889
+ QVERIFY ( def->checkValueIsAcceptable ( QVariant () ) );
2890
+ QVERIFY ( def->checkValueIsAcceptable ( QgsProcessingOutputLayerDefinition ( " layer1231123" ) ) );
2891
+
2892
+ // test hasGeometry
2893
+ QVERIFY ( QgsProcessingParameterVectorOutput ( " test" , QString (), QgsProcessingParameterDefinition::TypeAny ).hasGeometry () );
2894
+ QVERIFY ( QgsProcessingParameterVectorOutput ( " test" , QString (), QgsProcessingParameterDefinition::TypeVectorAny ).hasGeometry () );
2895
+ QVERIFY ( QgsProcessingParameterVectorOutput ( " test" , QString (), QgsProcessingParameterDefinition::TypeVectorPoint ).hasGeometry () );
2896
+ QVERIFY ( QgsProcessingParameterVectorOutput ( " test" , QString (), QgsProcessingParameterDefinition::TypeVectorLine ).hasGeometry () );
2897
+ QVERIFY ( QgsProcessingParameterVectorOutput ( " test" , QString (), QgsProcessingParameterDefinition::TypeVectorPolygon ).hasGeometry () );
2898
+ QVERIFY ( !QgsProcessingParameterVectorOutput ( " test" , QString (), QgsProcessingParameterDefinition::TypeRaster ).hasGeometry () );
2899
+ QVERIFY ( !QgsProcessingParameterVectorOutput ( " test" , QString (), QgsProcessingParameterDefinition::TypeFile ).hasGeometry () );
2900
+ QVERIFY ( QgsProcessingParameterVectorOutput ( " test" , QString (), QgsProcessingParameterDefinition::TypeTable ).hasGeometry () );
2901
+
2902
+ }
2903
+
2750
2904
void TestQgsProcessing::parameterRasterOut ()
2751
2905
{
2752
2906
// setup a context
0 commit comments