-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
testqgsspatialindex.cpp
229 lines (191 loc) · 6.59 KB
/
testqgsspatialindex.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/***************************************************************************
test_template.cpp
--------------------------------------
Date : Sun Sep 16 12:22:23 AKDT 2007
Copyright : (C) 2007 by Gary E. Sherman
Email : sherman at mrcc dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgstest.h"
#include <QObject>
#include <QString>
#include <qgsapplication.h>
#include "qgsfeatureiterator.h"
#include <qgsgeometry.h>
#include <qgsspatialindex.h>
#include <qgsvectordataprovider.h>
#include <qgsvectorlayer.h>
static QgsFeature _pointFeature( QgsFeatureId id, qreal x, qreal y )
{
QgsFeature f( id );
QgsGeometry g = QgsGeometry::fromPoint( QgsPoint( x, y ) );
f.setGeometry( g );
return f;
}
static QList<QgsFeature> _pointFeatures()
{
/*
* 2 | 1
* |
* -----+-----
* |
* 3 | 4
*/
QList<QgsFeature> feats;
feats << _pointFeature( 1, 1, 1 )
<< _pointFeature( 2, -1, 1 )
<< _pointFeature( 3, -1, -1 )
<< _pointFeature( 4, 1, -1 );
return feats;
}
class TestQgsSpatialIndex : public QObject
{
Q_OBJECT
private slots:
void initTestCase()
{
QgsApplication::init();
QgsApplication::initQgis();
}
void cleanupTestCase()
{
QgsApplication::exitQgis();
}
void testQuery()
{
QgsSpatialIndex index;
Q_FOREACH ( const QgsFeature &f, _pointFeatures() )
index.insertFeature( f );
QList<QgsFeatureId> fids = index.intersects( QgsRectangle( 0, 0, 10, 10 ) );
QVERIFY( fids.count() == 1 );
QVERIFY( fids[0] == 1 );
QList<QgsFeatureId> fids2 = index.intersects( QgsRectangle( -10, -10, 0, 10 ) );
QVERIFY( fids2.count() == 2 );
QVERIFY( fids2.contains( 2 ) );
QVERIFY( fids2.contains( 3 ) );
}
void testQueryManualInsert()
{
QgsSpatialIndex index;
index.insertFeature( 1, QgsRectangle( 2, 3, 2, 3 ) );
index.insertFeature( 2, QgsRectangle( 12, 13, 12, 13 ) );
index.insertFeature( 3, QgsRectangle( 14, 13, 14, 13 ) );
QList<QgsFeatureId> fids = index.intersects( QgsRectangle( 1, 2, 3, 4 ) );
QVERIFY( fids.count() == 1 );
QVERIFY( fids.at( 0 ) == 1 );
QList<QgsFeatureId> fids2 = index.intersects( QgsRectangle( 10, 12, 15, 14 ) );
QVERIFY( fids2.count() == 2 );
QVERIFY( fids2.contains( 2 ) );
QVERIFY( fids2.contains( 3 ) );
}
void testCopy()
{
QgsSpatialIndex *index = new QgsSpatialIndex;
Q_FOREACH ( const QgsFeature &f, _pointFeatures() )
index->insertFeature( f );
// create copy of the index
QgsSpatialIndex indexCopy( *index );
QVERIFY( index->refs() == 2 );
QVERIFY( indexCopy.refs() == 2 );
// test that copied index works
QList<QgsFeatureId> fids1 = indexCopy.intersects( QgsRectangle( 0, 0, 10, 10 ) );
QVERIFY( fids1.count() == 1 );
QVERIFY( fids1[0] == 1 );
// check that the index is still shared
QVERIFY( index->refs() == 2 );
QVERIFY( indexCopy.refs() == 2 );
// do a modification
QgsFeature f2( _pointFeatures().at( 1 ) );
indexCopy.deleteFeature( f2 );
// check that the index is not shared anymore
QVERIFY( index->refs() == 1 );
QVERIFY( indexCopy.refs() == 1 );
delete index;
// test that copied index still works
QList<QgsFeatureId> fids = indexCopy.intersects( QgsRectangle( 0, 0, 10, 10 ) );
QVERIFY( fids.count() == 1 );
QVERIFY( fids[0] == 1 );
}
void benchmarkIntersect()
{
// add 50K features to the index
QgsSpatialIndex index;
for ( int i = 0; i < 100; ++i )
{
for ( int k = 0; k < 500; ++k )
{
QgsFeature f( i * 1000 + k );
QgsGeometry g = QgsGeometry::fromPoint( QgsPoint( i / 10, i % 10 ) );
f.setGeometry( g );
index.insertFeature( f );
}
}
QBENCHMARK
{
for ( int i = 0; i < 100; ++i )
index.intersects( QgsRectangle( i / 10, i % 10, i / 10 + 1, i % 10 + 1 ) );
}
}
void benchmarkBulkLoad()
{
QgsVectorLayer *vl = new QgsVectorLayer( QStringLiteral( "Point" ), QStringLiteral( "x" ), QStringLiteral( "memory" ) );
for ( int i = 0; i < 100; ++i )
{
QgsFeatureList flist;
for ( int k = 0; k < 500; ++k )
{
QgsFeature f( i * 1000 + k );
QgsGeometry g = QgsGeometry::fromPoint( QgsPoint( i / 10, i % 10 ) );
f.setGeometry( g );
flist << f;
}
vl->dataProvider()->addFeatures( flist );
}
QTime t;
QgsSpatialIndex *indexBulk = nullptr;
QgsSpatialIndex *indexInsert = nullptr;
t.start();
{
QgsFeature f;
QgsFeatureIterator fi = vl->getFeatures();
while ( fi.nextFeature( f ) )
;
}
qDebug( "iter only: %d ms", t.elapsed() );
t.start();
{
QgsFeatureIterator fi = vl->getFeatures();
indexBulk = new QgsSpatialIndex( fi );
}
qDebug( "bulk load: %d ms", t.elapsed() );
t.start();
{
QgsFeatureIterator fi = vl->getFeatures();
QgsFeature f;
indexInsert = new QgsSpatialIndex;
while ( fi.nextFeature( f ) )
indexInsert->insertFeature( f );
}
qDebug( "insert: %d ms", t.elapsed() );
// test whether a query will give us the same results
QgsRectangle rect( 4.9, 4.9, 5.1, 5.1 );
QList<QgsFeatureId> resBulk = indexBulk->intersects( rect );
QList<QgsFeatureId> resInsert = indexInsert->intersects( rect );
QCOMPARE( resBulk.count(), 500 );
QCOMPARE( resInsert.count(), 500 );
// the trees are built differently so they will give also different order of fids
std::sort( resBulk.begin(), resBulk.end() );
std::sort( resInsert.begin(), resInsert.end() );
QCOMPARE( resBulk, resInsert );
delete indexBulk;
delete indexInsert;
}
};
QGSTEST_MAIN( TestQgsSpatialIndex )
#include "testqgsspatialindex.moc"