Skip to content

Commit e709e3e

Browse files
committedMay 2, 2017
Add unit test for QgsProcessingUtils::createSpatialIndex
1 parent 49c688b commit e709e3e

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
 

‎tests/src/core/testqgsprocessing.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ class TestQgsProcessing: public QObject
109109
void algorithm();
110110
void features();
111111
void uniqueValues();
112+
void createIndex();
112113

113114
private:
114115

@@ -658,5 +659,42 @@ void TestQgsProcessing::uniqueValues()
658659
delete layer;
659660
}
660661

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+
661699
QGSTEST_MAIN( TestQgsProcessing )
662700
#include "testqgsprocessing.moc"

0 commit comments

Comments
 (0)
Please sign in to comment.