Skip to content

Commit

Permalink
Add unit test for QgsProcessingUtils::createSpatialIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed May 2, 2017
1 parent 49c688b commit e709e3e
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions tests/src/core/testqgsprocessing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ class TestQgsProcessing: public QObject
void algorithm();
void features();
void uniqueValues();
void createIndex();

private:

Expand Down Expand Up @@ -658,5 +659,42 @@ void TestQgsProcessing::uniqueValues()
delete layer;
}

void TestQgsProcessing::createIndex()
{
QgsVectorLayer *layer = new QgsVectorLayer( "Point", "v1", "memory" );
for ( int i = 1; i < 6; ++i )
{
QgsFeature f( i );
f.setGeometry( QgsGeometry( new QgsPointV2( i, 2 ) ) );
layer->dataProvider()->addFeatures( QgsFeatureList() << f );
}

QgsProcessingContext context;
// disable selected features check
context.setFlags( QgsProcessingContext::Flags( 0 ) );
QgsSpatialIndex index = QgsProcessingUtils::createSpatialIndex( layer, context );
QList<QgsFeatureId> ids = index.nearestNeighbor( QgsPoint( 2.1, 2 ), 1 );
QCOMPARE( ids, QList<QgsFeatureId>() << 2 );

// selected features check, but none selected
context.setFlags( QgsProcessingContext::UseSelectionIfPresent );
index = QgsProcessingUtils::createSpatialIndex( layer, context );
ids = index.nearestNeighbor( QgsPoint( 2.1, 2 ), 1 );
QCOMPARE( ids, QList<QgsFeatureId>() << 2 );

// create selection
layer->selectByIds( QgsFeatureIds() << 4 << 5 );
index = QgsProcessingUtils::createSpatialIndex( layer, context );
ids = index.nearestNeighbor( QgsPoint( 2.1, 2 ), 1 );
QCOMPARE( ids, QList<QgsFeatureId>() << 4 );

// selection but not using selection mode
context.setFlags( QgsProcessingContext::Flags( 0 ) );
index = QgsProcessingUtils::createSpatialIndex( layer, context );
ids = index.nearestNeighbor( QgsPoint( 2.1, 2 ), 1 );
QCOMPARE( ids, QList<QgsFeatureId>() << 2 );

}

QGSTEST_MAIN( TestQgsProcessing )
#include "testqgsprocessing.moc"

0 comments on commit e709e3e

Please sign in to comment.