void testQuery() { std::unique_ptr< QgsVectorLayer > vl = qgis::make_unique< QgsVectorLayer >( "Point", QString(), QStringLiteral( "memory" ) ); for ( QgsFeature f : _pointFeatures() ) vl->dataProvider()->addFeature( f ); QgsSpatialIndexKDBush index( *vl->dataProvider() ); QVERIFY( index.size() == 4 ); QList<QgsSpatialIndexKDBushData> fids = index.intersects( QgsRectangle( 0, 0, 10, 10 ) ); QVERIFY( fids.count() == 1 ); QVERIFY( testContains( fids, 1, QgsPointXY( 1, 1 ) ) ); QList<QgsSpatialIndexKDBushData> fids2 = index.intersects( QgsRectangle( -10, -10, 0, 10 ) ); QCOMPARE( fids2.count(), 2 ); QVERIFY( testContains( fids2, 2, QgsPointXY( -1, 1 ) ) ); QVERIFY( testContains( fids2, 3, QgsPointXY( -1, -1 ) ) ); QList<QgsSpatialIndexKDBushData> fids3 = index.within( QgsPointXY( 0, 0 ), 2 ); QCOMPARE( fids3.count(), 4 ); QVERIFY( testContains( fids3, 1, QgsPointXY( 1, 1 ) ) ); QVERIFY( testContains( fids3, 2, QgsPointXY( -1, 1 ) ) ); QVERIFY( testContains( fids3, 3, QgsPointXY( -1, -1 ) ) ); QVERIFY( testContains( fids3, 4, QgsPointXY( 1, -1 ) ) ); QList<QgsSpatialIndexKDBushData> fids4 = index.within( QgsPointXY( 0, 0 ), 1 ); QCOMPARE( fids4.count(), 0 ); QList<QgsSpatialIndexKDBushData> fids5 = index.within( QgsPointXY( -1, -1 ), 2.1 ); QCOMPARE( fids5.count(), 3 ); QVERIFY( testContains( fids5, 2, QgsPointXY( -1, 1 ) ) ); QVERIFY( testContains( fids5, 3, QgsPointXY( -1, -1 ) ) ); QVERIFY( testContains( fids5, 4, QgsPointXY( 1, -1 ) ) ); }
void testCopy() { std::unique_ptr< QgsVectorLayer > vl = qgis::make_unique< QgsVectorLayer >( "Point", QString(), QStringLiteral( "memory" ) ); for ( QgsFeature f : _pointFeatures() ) vl->dataProvider()->addFeature( f ); std::unique_ptr< QgsSpatialIndexKDBush > index( new QgsSpatialIndexKDBush( *vl->dataProvider() ) ); // create copy of the index std::unique_ptr< QgsSpatialIndexKDBush > indexCopy( new QgsSpatialIndexKDBush( *index ) ); QVERIFY( index->d == indexCopy->d ); QVERIFY( index->d->ref == 2 ); // test that copied index works QList<QgsSpatialIndexKDBushData> fids = indexCopy->intersects( QgsRectangle( 0, 0, 10, 10 ) ); QVERIFY( fids.count() == 1 ); QVERIFY( testContains( fids, 1, QgsPointXY( 1, 1 ) ) ); // check that the index is still shared QVERIFY( index->d == indexCopy->d ); QVERIFY( index->d->ref == 2 ); index.reset(); // test that copied index still works fids = indexCopy->intersects( QgsRectangle( 0, 0, 10, 10 ) ); QVERIFY( fids.count() == 1 ); QVERIFY( testContains( fids, 1, QgsPointXY( 1, 1 ) ) ); QVERIFY( indexCopy->d->ref == 1 ); // assignment operator std::unique_ptr< QgsVectorLayer > vl2 = qgis::make_unique< QgsVectorLayer >( "Point", QString(), QStringLiteral( "memory" ) ); QgsSpatialIndexKDBush index3( *vl2->dataProvider() ); QVERIFY( index3.size() == 0 ); fids = index3.intersects( QgsRectangle( 0, 0, 10, 10 ) ); QVERIFY( fids.count() == 0 ); QVERIFY( index3.d->ref == 1 ); index3 = *indexCopy; QVERIFY( index3.d == indexCopy->d ); QVERIFY( index3.d->ref == 2 ); fids = index3.intersects( QgsRectangle( 0, 0, 10, 10 ) ); QVERIFY( fids.count() == 1 ); QVERIFY( testContains( fids, 1, QgsPointXY( 1, 1 ) ) ); indexCopy.reset(); QVERIFY( index3.d->ref == 1 ); }
int testAll() { Debug::init(); testStringConvertion(); testTimeQueue(); testRectangleIterator(); testValueCheck(); testSplit(); testShortestPath(); testAStar(); testShortestPath2(); testShortestPathReverse(); testRange(); testRange2(); testContains(); testPredicates(); testOptional(); testMustInitialize(); testVec2(); testConcat(); testTable(); testVec2(); testRectangle(); testProjection(); testRandomExit(); testCombine(); testSectors1(); testSectors2(); testSectors3(); testReverse(); testReverse2(); testReverse3(); Debug() << "-----===== OK =====-----"; return 0; }