|
| 1 | +/*************************************************************************** |
| 2 | + TestQgsSpatialIndexKdBushkdbush.cpp |
| 3 | + -------------------------------------- |
| 4 | + Date : July 2018 |
| 5 | + Copyright : (C) 2018 by Nyall Dawson |
| 6 | + Email : nyall dot dawson at gmail dot com |
| 7 | + *************************************************************************** |
| 8 | + * * |
| 9 | + * This program is free software; you can redistribute it and/or modify * |
| 10 | + * it under the terms of the GNU General Public License as published by * |
| 11 | + * the Free Software Foundation; either version 2 of the License, or * |
| 12 | + * (at your option) any later version. * |
| 13 | + * * |
| 14 | + ***************************************************************************/ |
| 15 | + |
| 16 | +#include "qgstest.h" |
| 17 | +#include <QObject> |
| 18 | +#include <QString> |
| 19 | + |
| 20 | +#include <qgsapplication.h> |
| 21 | +#include "qgsfeatureiterator.h" |
| 22 | +#include "qgsgeometry.h" |
| 23 | +#include "qgsspatialindexkdbush.h" |
| 24 | +#include "qgsvectordataprovider.h" |
| 25 | +#include "qgsvectorlayer.h" |
| 26 | +#include "qgsspatialindexkdbush_p.h" |
| 27 | + |
| 28 | +static QgsFeature _pointFeature( QgsFeatureId id, qreal x, qreal y ) |
| 29 | +{ |
| 30 | + QgsFeature f( id ); |
| 31 | + QgsGeometry g = QgsGeometry::fromPointXY( QgsPointXY( x, y ) ); |
| 32 | + f.setGeometry( g ); |
| 33 | + return f; |
| 34 | +} |
| 35 | + |
| 36 | +static QList<QgsFeature> _pointFeatures() |
| 37 | +{ |
| 38 | + /* |
| 39 | + * 2 | 1 |
| 40 | + * | |
| 41 | + * -----+----- |
| 42 | + * | |
| 43 | + * 3 | 4 |
| 44 | + */ |
| 45 | + |
| 46 | + QList<QgsFeature> feats; |
| 47 | + feats << _pointFeature( 1, 1, 1 ) |
| 48 | + << _pointFeature( 2, -1, 1 ) |
| 49 | + << _pointFeature( 3, -1, -1 ) |
| 50 | + << _pointFeature( 4, 1, -1 ); |
| 51 | + return feats; |
| 52 | +} |
| 53 | + |
| 54 | +class TestQgsSpatialIndexKdBush : public QObject |
| 55 | +{ |
| 56 | + Q_OBJECT |
| 57 | + |
| 58 | + private slots: |
| 59 | + |
| 60 | + void initTestCase() |
| 61 | + { |
| 62 | + QgsApplication::init(); |
| 63 | + QgsApplication::initQgis(); |
| 64 | + } |
| 65 | + void cleanupTestCase() |
| 66 | + { |
| 67 | + QgsApplication::exitQgis(); |
| 68 | + } |
| 69 | + |
| 70 | + void testQuery() |
| 71 | + { |
| 72 | + std::unique_ptr< QgsVectorLayer > vl = qgis::make_unique< QgsVectorLayer >( "Point", QString(), QStringLiteral( "memory" ) ); |
| 73 | + for ( QgsFeature f : _pointFeatures() ) |
| 74 | + vl->dataProvider()->addFeature( f ); |
| 75 | + QgsSpatialIndexKDBush index( *vl->dataProvider() ); |
| 76 | + |
| 77 | + QList<QgsFeatureId> fids = index.intersect( QgsRectangle( 0, 0, 10, 10 ) ); |
| 78 | + QVERIFY( fids.count() == 1 ); |
| 79 | + QCOMPARE( fids[0], 1 ); |
| 80 | + |
| 81 | + QList<QgsFeatureId> fids2 = index.intersect( QgsRectangle( -10, -10, 0, 10 ) ); |
| 82 | + QCOMPARE( fids2.count(), 2 ); |
| 83 | + QVERIFY( fids2.contains( 2 ) ); |
| 84 | + QVERIFY( fids2.contains( 3 ) ); |
| 85 | + |
| 86 | + QList<QgsFeatureId> fids3 = index.within( QgsPointXY( 0, 0 ), 2 ); |
| 87 | + QCOMPARE( fids3.count(), 4 ); |
| 88 | + QVERIFY( fids3.contains( 1 ) ); |
| 89 | + QVERIFY( fids3.contains( 2 ) ); |
| 90 | + QVERIFY( fids3.contains( 3 ) ); |
| 91 | + QVERIFY( fids3.contains( 4 ) ); |
| 92 | + |
| 93 | + QList<QgsFeatureId> fids4 = index.within( QgsPointXY( 0, 0 ), 1 ); |
| 94 | + QCOMPARE( fids4.count(), 0 ); |
| 95 | + |
| 96 | + QList<QgsFeatureId> fids5 = index.within( QgsPointXY( -1, -1 ), 2.1 ); |
| 97 | + QCOMPARE( fids5.count(), 3 ); |
| 98 | + QVERIFY( fids5.contains( 2 ) ); |
| 99 | + QVERIFY( fids5.contains( 3 ) ); |
| 100 | + QVERIFY( fids5.contains( 4 ) ); |
| 101 | + |
| 102 | + QgsPointXY p; |
| 103 | + QVERIFY( !index.point( -1, p ) ); |
| 104 | + QVERIFY( !index.point( 5, p ) ); |
| 105 | + QVERIFY( index.point( 1, p ) ); |
| 106 | + QCOMPARE( p, QgsPointXY( 1, 1 ) ); |
| 107 | + QVERIFY( index.point( 2, p ) ); |
| 108 | + QCOMPARE( p, QgsPointXY( -1, 1 ) ); |
| 109 | + QVERIFY( index.point( 3, p ) ); |
| 110 | + QCOMPARE( p, QgsPointXY( -1, -1 ) ); |
| 111 | + QVERIFY( index.point( 4, p ) ); |
| 112 | + QCOMPARE( p, QgsPointXY( 1, -1 ) ); |
| 113 | + } |
| 114 | + |
| 115 | + void testCopy() |
| 116 | + { |
| 117 | + std::unique_ptr< QgsVectorLayer > vl = qgis::make_unique< QgsVectorLayer >( "Point", QString(), QStringLiteral( "memory" ) ); |
| 118 | + for ( QgsFeature f : _pointFeatures() ) |
| 119 | + vl->dataProvider()->addFeature( f ); |
| 120 | + |
| 121 | + std::unique_ptr< QgsSpatialIndexKDBush > index( new QgsSpatialIndexKDBush( *vl->dataProvider() ) ); |
| 122 | + |
| 123 | + // create copy of the index |
| 124 | + std::unique_ptr< QgsSpatialIndexKDBush > indexCopy( new QgsSpatialIndexKDBush( *index ) ); |
| 125 | + |
| 126 | + QCOMPARE( index->d, indexCopy->d ); |
| 127 | + QVERIFY( index->d->ref == 2 ); |
| 128 | + |
| 129 | + // test that copied index works |
| 130 | + QList<QgsFeatureId> fids = indexCopy->intersect( QgsRectangle( 0, 0, 10, 10 ) ); |
| 131 | + QVERIFY( fids.count() == 1 ); |
| 132 | + QCOMPARE( fids[0], 1 ); |
| 133 | + |
| 134 | + // check that the index is still shared |
| 135 | + QCOMPARE( index->d, indexCopy->d ); |
| 136 | + QVERIFY( index->d->ref == 2 ); |
| 137 | + |
| 138 | + index.reset(); |
| 139 | + |
| 140 | + // test that copied index still works |
| 141 | + fids = indexCopy->intersect( QgsRectangle( 0, 0, 10, 10 ) ); |
| 142 | + QVERIFY( fids.count() == 1 ); |
| 143 | + QCOMPARE( fids[0], 1 ); |
| 144 | + QVERIFY( indexCopy->d->ref == 1 ); |
| 145 | + |
| 146 | + // assignment operator |
| 147 | + std::unique_ptr< QgsVectorLayer > vl2 = qgis::make_unique< QgsVectorLayer >( "Point", QString(), QStringLiteral( "memory" ) ); |
| 148 | + QgsSpatialIndexKDBush index3( *vl2->dataProvider() ); |
| 149 | + fids = index3.intersect( QgsRectangle( 0, 0, 10, 10 ) ); |
| 150 | + QCOMPARE( fids.count(), 0 ); |
| 151 | + QVERIFY( index3.d->ref == 1 ); |
| 152 | + |
| 153 | + index3 = *indexCopy; |
| 154 | + QCOMPARE( index3.d, indexCopy->d ); |
| 155 | + QVERIFY( index3.d->ref == 2 ); |
| 156 | + fids = index3.intersect( QgsRectangle( 0, 0, 10, 10 ) ); |
| 157 | + QVERIFY( fids.count() == 1 ); |
| 158 | + QCOMPARE( fids[0], 1 ); |
| 159 | + |
| 160 | + indexCopy.reset(); |
| 161 | + QCOMPARE( index3.d->ref, 1 ); |
| 162 | + } |
| 163 | + |
| 164 | +}; |
| 165 | + |
| 166 | +QGSTEST_MAIN( TestQgsSpatialIndexKdBush ) |
| 167 | + |
| 168 | +#include "testqgsspatialindexkdbush.moc" |
| 169 | + |
| 170 | + |
0 commit comments