@@ -109,6 +109,7 @@ class TestQgsProcessing: public QObject
109
109
void algorithm ();
110
110
void features ();
111
111
void uniqueValues ();
112
+ void createIndex ();
112
113
113
114
private:
114
115
@@ -658,5 +659,42 @@ void TestQgsProcessing::uniqueValues()
658
659
delete layer;
659
660
}
660
661
662
+ void TestQgsProcessing::createIndex ()
663
+ {
664
+ QgsVectorLayer *layer = new QgsVectorLayer ( " Point" , " v1" , " memory" );
665
+ for ( int i = 1 ; i < 6 ; ++i )
666
+ {
667
+ QgsFeature f ( i );
668
+ f.setGeometry ( QgsGeometry ( new QgsPointV2 ( i, 2 ) ) );
669
+ layer->dataProvider ()->addFeatures ( QgsFeatureList () << f );
670
+ }
671
+
672
+ QgsProcessingContext context;
673
+ // disable selected features check
674
+ context.setFlags ( QgsProcessingContext::Flags ( 0 ) );
675
+ QgsSpatialIndex index = QgsProcessingUtils::createSpatialIndex ( layer, context );
676
+ QList<QgsFeatureId> ids = index.nearestNeighbor ( QgsPoint ( 2.1 , 2 ), 1 );
677
+ QCOMPARE ( ids, QList<QgsFeatureId>() << 2 );
678
+
679
+ // selected features check, but none selected
680
+ context.setFlags ( QgsProcessingContext::UseSelectionIfPresent );
681
+ index = QgsProcessingUtils::createSpatialIndex ( layer, context );
682
+ ids = index.nearestNeighbor ( QgsPoint ( 2.1 , 2 ), 1 );
683
+ QCOMPARE ( ids, QList<QgsFeatureId>() << 2 );
684
+
685
+ // create selection
686
+ layer->selectByIds ( QgsFeatureIds () << 4 << 5 );
687
+ index = QgsProcessingUtils::createSpatialIndex ( layer, context );
688
+ ids = index.nearestNeighbor ( QgsPoint ( 2.1 , 2 ), 1 );
689
+ QCOMPARE ( ids, QList<QgsFeatureId>() << 4 );
690
+
691
+ // selection but not using selection mode
692
+ context.setFlags ( QgsProcessingContext::Flags ( 0 ) );
693
+ index = QgsProcessingUtils::createSpatialIndex ( layer, context );
694
+ ids = index.nearestNeighbor ( QgsPoint ( 2.1 , 2 ), 1 );
695
+ QCOMPARE ( ids, QList<QgsFeatureId>() << 2 );
696
+
697
+ }
698
+
661
699
QGSTEST_MAIN ( TestQgsProcessing )
662
700
#include " testqgsprocessing.moc"
0 commit comments