Skip to content

Commit 612c9cb

Browse files
committed
Use sets for index results
1 parent 682671d commit 612c9cb

File tree

5 files changed

+45
-23
lines changed

5 files changed

+45
-23
lines changed

python/core/auto_generated/qgsspatialindexkdbush.sip.in

+9-4
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,15 @@ Copy constructor
6565

6666
~QgsSpatialIndexKDBush();
6767

68-
QList<QgsFeatureId> intersect( const QgsRectangle &rectangle ) const;
68+
QSet<QgsFeatureId> intersect( const QgsRectangle &rectangle ) const;
6969
%Docstring
70-
Returns a list of features which fall within the specified ``rectangle``.
70+
Returns the set of features which fall within the specified ``rectangle``.
7171
%End
7272

7373

74-
QList<QgsFeatureId> within( const QgsPointXY &point, double radius ) const;
74+
QSet<QgsFeatureId> within( const QgsPointXY &point, double radius ) const;
7575
%Docstring
76-
Returns a list of features which are within the given search ``radius``
76+
Returns the set of features which are within the given search ``radius``
7777
of ``point``.
7878
%End
7979

@@ -84,6 +84,11 @@ Fetches the point from the index with matching ``id`` and stores it in ``point``
8484

8585
Returns true if the point was found, or false if no matching feature ID is present
8686
in the index.
87+
%End
88+
89+
qgssize size() const;
90+
%Docstring
91+
Returns the size of the index, i.e. the number of points contained within the index.
8792
%End
8893

8994
};

src/core/qgsspatialindexkdbush.cpp

+10-5
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ QgsSpatialIndexKDBush::~QgsSpatialIndexKDBush()
5656
delete d;
5757
}
5858

59-
QList<QgsFeatureId> QgsSpatialIndexKDBush::within( const QgsPointXY &point, double radius ) const
59+
QSet<QgsFeatureId> QgsSpatialIndexKDBush::within( const QgsPointXY &point, double radius ) const
6060
{
61-
QList<QgsFeatureId> result;
62-
d->index->within( point.x(), point.y(), radius, [&result]( const QgsFeatureId id ) { result << id; } );
61+
QSet<QgsFeatureId> result;
62+
d->index->within( point.x(), point.y(), radius, [&result]( const QgsFeatureId id ) { result.insert( id ); } );
6363
return result;
6464
}
6565

@@ -73,9 +73,14 @@ bool QgsSpatialIndexKDBush::point( QgsFeatureId id, QgsPointXY &point ) const
7373
return d->index->point( id, point );
7474
}
7575

76-
QList<QgsFeatureId> QgsSpatialIndexKDBush::intersect( const QgsRectangle &rectangle ) const
76+
qgssize QgsSpatialIndexKDBush::size() const
7777
{
78-
QList<QgsFeatureId> result;
78+
return d->index->size();
79+
}
80+
81+
QSet<QgsFeatureId> QgsSpatialIndexKDBush::intersect( const QgsRectangle &rectangle ) const
82+
{
83+
QSet<QgsFeatureId> result;
7984
d->index->range( rectangle.xMinimum(),
8085
rectangle.yMinimum(),
8186
rectangle.xMaximum(),

src/core/qgsspatialindexkdbush.h

+9-4
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ class CORE_EXPORT QgsSpatialIndexKDBush
8383
~QgsSpatialIndexKDBush();
8484

8585
/**
86-
* Returns a list of features which fall within the specified \a rectangle.
86+
* Returns the set of features which fall within the specified \a rectangle.
8787
*/
88-
QList<QgsFeatureId> intersect( const QgsRectangle &rectangle ) const;
88+
QSet<QgsFeatureId> intersect( const QgsRectangle &rectangle ) const;
8989

9090
/**
9191
* Calls a \a visitor function for all features which fall within the specified \a rectangle.
@@ -95,10 +95,10 @@ class CORE_EXPORT QgsSpatialIndexKDBush
9595
void intersect( const QgsRectangle &rectangle, const std::function<void( QgsFeatureId )> &visitor ) const SIP_SKIP;
9696

9797
/**
98-
* Returns a list of features which are within the given search \a radius
98+
* Returns the set of features which are within the given search \a radius
9999
* of \a point.
100100
*/
101-
QList<QgsFeatureId> within( const QgsPointXY &point, double radius ) const;
101+
QSet<QgsFeatureId> within( const QgsPointXY &point, double radius ) const;
102102

103103
/**
104104
* Calls a \a visitor function for all features which are within the given search \a radius
@@ -116,6 +116,11 @@ class CORE_EXPORT QgsSpatialIndexKDBush
116116
*/
117117
bool point( QgsFeatureId id, QgsPointXY &point ) const;
118118

119+
/**
120+
* Returns the size of the index, i.e. the number of points contained within the index.
121+
*/
122+
qgssize size() const;
123+
119124
private:
120125

121126
//! Implicitly shared data pointer

src/core/qgsspatialindexkdbush_p.h

+5
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ class PointXYKDBush : public kdbush::KDBush< std::pair<double, double>, QgsFeatu
9999
return true;
100100
}
101101

102+
std::size_t size() const
103+
{
104+
return points.size();
105+
}
106+
102107
private:
103108

104109
QHash< QgsFeatureId, QgsPointXY > mIdToPoint;

tests/src/core/testqgsspatialindexkdbush.cpp

+12-10
Original file line numberDiff line numberDiff line change
@@ -73,27 +73,28 @@ class TestQgsSpatialIndexKdBush : public QObject
7373
for ( QgsFeature f : _pointFeatures() )
7474
vl->dataProvider()->addFeature( f );
7575
QgsSpatialIndexKDBush index( *vl->dataProvider() );
76+
QCOMPARE( index.size(), 4 );
7677

77-
QList<QgsFeatureId> fids = index.intersect( QgsRectangle( 0, 0, 10, 10 ) );
78+
QSet<QgsFeatureId> fids = index.intersect( QgsRectangle( 0, 0, 10, 10 ) );
7879
QVERIFY( fids.count() == 1 );
79-
QCOMPARE( fids[0], 1 );
80+
QVERIFY( fids.contains( 1 ) );
8081

81-
QList<QgsFeatureId> fids2 = index.intersect( QgsRectangle( -10, -10, 0, 10 ) );
82+
QSet<QgsFeatureId> fids2 = index.intersect( QgsRectangle( -10, -10, 0, 10 ) );
8283
QCOMPARE( fids2.count(), 2 );
8384
QVERIFY( fids2.contains( 2 ) );
8485
QVERIFY( fids2.contains( 3 ) );
8586

86-
QList<QgsFeatureId> fids3 = index.within( QgsPointXY( 0, 0 ), 2 );
87+
QSet<QgsFeatureId> fids3 = index.within( QgsPointXY( 0, 0 ), 2 );
8788
QCOMPARE( fids3.count(), 4 );
8889
QVERIFY( fids3.contains( 1 ) );
8990
QVERIFY( fids3.contains( 2 ) );
9091
QVERIFY( fids3.contains( 3 ) );
9192
QVERIFY( fids3.contains( 4 ) );
9293

93-
QList<QgsFeatureId> fids4 = index.within( QgsPointXY( 0, 0 ), 1 );
94+
QSet<QgsFeatureId> fids4 = index.within( QgsPointXY( 0, 0 ), 1 );
9495
QCOMPARE( fids4.count(), 0 );
9596

96-
QList<QgsFeatureId> fids5 = index.within( QgsPointXY( -1, -1 ), 2.1 );
97+
QSet<QgsFeatureId> fids5 = index.within( QgsPointXY( -1, -1 ), 2.1 );
9798
QCOMPARE( fids5.count(), 3 );
9899
QVERIFY( fids5.contains( 2 ) );
99100
QVERIFY( fids5.contains( 3 ) );
@@ -127,9 +128,9 @@ class TestQgsSpatialIndexKdBush : public QObject
127128
QVERIFY( index->d->ref == 2 );
128129

129130
// test that copied index works
130-
QList<QgsFeatureId> fids = indexCopy->intersect( QgsRectangle( 0, 0, 10, 10 ) );
131+
QSet<QgsFeatureId> fids = indexCopy->intersect( QgsRectangle( 0, 0, 10, 10 ) );
131132
QVERIFY( fids.count() == 1 );
132-
QCOMPARE( fids[0], 1 );
133+
QVERIFY( fids.contains( 1 ) );
133134

134135
// check that the index is still shared
135136
QCOMPARE( index->d, indexCopy->d );
@@ -140,12 +141,13 @@ class TestQgsSpatialIndexKdBush : public QObject
140141
// test that copied index still works
141142
fids = indexCopy->intersect( QgsRectangle( 0, 0, 10, 10 ) );
142143
QVERIFY( fids.count() == 1 );
143-
QCOMPARE( fids[0], 1 );
144+
QVERIFY( fids.contains( 1 ) );
144145
QVERIFY( indexCopy->d->ref == 1 );
145146

146147
// assignment operator
147148
std::unique_ptr< QgsVectorLayer > vl2 = qgis::make_unique< QgsVectorLayer >( "Point", QString(), QStringLiteral( "memory" ) );
148149
QgsSpatialIndexKDBush index3( *vl2->dataProvider() );
150+
QCOMPARE( index3.size(), 0 );
149151
fids = index3.intersect( QgsRectangle( 0, 0, 10, 10 ) );
150152
QCOMPARE( fids.count(), 0 );
151153
QVERIFY( index3.d->ref == 1 );
@@ -155,7 +157,7 @@ class TestQgsSpatialIndexKdBush : public QObject
155157
QVERIFY( index3.d->ref == 2 );
156158
fids = index3.intersect( QgsRectangle( 0, 0, 10, 10 ) );
157159
QVERIFY( fids.count() == 1 );
158-
QCOMPARE( fids[0], 1 );
160+
QVERIFY( fids.contains( 1 ) );
159161

160162
indexCopy.reset();
161163
QVERIFY( index3.d->ref == 1 );

0 commit comments

Comments
 (0)