Skip to content

Commit ae4e4cd

Browse files
committedJul 7, 2018
Return full point container for matches, remove redundant point method
1 parent 66c1788 commit ae4e4cd

File tree

5 files changed

+45
-77
lines changed

5 files changed

+45
-77
lines changed
 

‎python/core/auto_generated/qgsspatialindexkdbush.sip.in

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,27 +65,19 @@ Copy constructor
6565

6666
~QgsSpatialIndexKDBush();
6767

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

7373

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

8080

81-
bool point( QgsFeatureId id, QgsPointXY &point ) const;
82-
%Docstring
83-
Fetches the point from the index with matching ``id`` and stores it in ``point``.
84-
85-
Returns true if the point was found, or false if no matching feature ID is present
86-
in the index.
87-
%End
88-
8981
qgssize size() const;
9082
%Docstring
9183
Returns the size of the index, i.e. the number of points contained within the index.

‎src/core/qgsspatialindexkdbush.cpp

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ QgsSpatialIndexKDBush::~QgsSpatialIndexKDBush()
5656
delete d;
5757
}
5858

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

@@ -68,23 +68,18 @@ void QgsSpatialIndexKDBush::within( const QgsPointXY &point, double radius, cons
6868
d->index->within( point.x(), point.y(), radius, visitor );
6969
}
7070

71-
bool QgsSpatialIndexKDBush::point( QgsFeatureId id, QgsPointXY &point ) const
72-
{
73-
return d->index->point( id, point );
74-
}
75-
7671
qgssize QgsSpatialIndexKDBush::size() const
7772
{
7873
return d->index->size();
7974
}
8075

81-
QSet<QgsFeatureId> QgsSpatialIndexKDBush::intersect( const QgsRectangle &rectangle ) const
76+
QList<QgsSpatialIndexKDBushData> QgsSpatialIndexKDBush::intersect( const QgsRectangle &rectangle ) const
8277
{
83-
QSet<QgsFeatureId> result;
78+
QList<QgsSpatialIndexKDBushData> result;
8479
d->index->range( rectangle.xMinimum(),
8580
rectangle.yMinimum(),
8681
rectangle.xMaximum(),
87-
rectangle.yMaximum(), [&result]( const QgsSpatialIndexKDBushData & p ) { result << p.id; } );
82+
rectangle.yMaximum(), [&result]( const QgsSpatialIndexKDBushData & p ) { result << p; } );
8883
return result;
8984
}
9085

‎src/core/qgsspatialindexkdbush.h

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@ class CORE_EXPORT QgsSpatialIndexKDBush
8484
~QgsSpatialIndexKDBush();
8585

8686
/**
87-
* Returns the set of features which fall within the specified \a rectangle.
87+
* Returns the list of features which fall within the specified \a rectangle.
8888
*/
89-
QSet<QgsFeatureId> intersect( const QgsRectangle &rectangle ) const;
89+
QList<QgsSpatialIndexKDBushData> intersect( const QgsRectangle &rectangle ) const;
9090

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

9898
/**
99-
* Returns the set of features which are within the given search \a radius
99+
* Returns the list of features which are within the given search \a radius
100100
* of \a point.
101101
*/
102-
QSet<QgsFeatureId> within( const QgsPointXY &point, double radius ) const;
102+
QList<QgsSpatialIndexKDBushData> within( const QgsPointXY &point, double radius ) const;
103103

104104
/**
105105
* Calls a \a visitor function for all features which are within the given search \a radius
@@ -109,14 +109,6 @@ class CORE_EXPORT QgsSpatialIndexKDBush
109109
*/
110110
void within( const QgsPointXY &point, double radius, const std::function<void( QgsSpatialIndexKDBushData )> &visitor ) SIP_SKIP;
111111

112-
/**
113-
* Fetches the point from the index with matching \a id and stores it in \a point.
114-
*
115-
* Returns true if the point was found, or false if no matching feature ID is present
116-
* in the index.
117-
*/
118-
bool point( QgsFeatureId id, QgsPointXY &point ) const;
119-
120112
/**
121113
* Returns the size of the index, i.e. the number of points contained within the index.
122114
*/

‎src/core/qgsspatialindexkdbush_p.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,6 @@ class PointXYKDBush : public kdbush::KDBush< std::pair<double, double>, QgsSpati
9090
sortKD( 0, size - 1, 0 );
9191
}
9292

93-
bool point( QgsFeatureId id, QgsPointXY &point ) const
94-
{
95-
auto it = std::find_if( points.begin(), points.end(),
96-
[id]( const QgsSpatialIndexKDBushData & d ) { return d.id == id; } );
97-
if ( it == points.end() )
98-
return false;
99-
100-
point = QgsPointXY( it->coords.first, it->coords.second );
101-
return true;
102-
}
103-
10493
std::size_t size() const
10594
{
10695
return points.size();

‎tests/src/core/testqgsspatialindexkdbush.cpp

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,18 @@ static QList<QgsFeature> _pointFeatures()
5151
return feats;
5252
}
5353

54+
bool testContains( const QList<QgsSpatialIndexKDBushData> &data, QgsFeatureId id, const QgsPointXY &point )
55+
{
56+
for ( const QgsSpatialIndexKDBushData &d : data )
57+
{
58+
if ( d.id == id )
59+
{
60+
return d.point() == point;
61+
}
62+
}
63+
return false;
64+
}
65+
5466
class TestQgsSpatialIndexKdBush : public QObject
5567
{
5668
Q_OBJECT
@@ -75,42 +87,30 @@ class TestQgsSpatialIndexKdBush : public QObject
7587
QgsSpatialIndexKDBush index( *vl->dataProvider() );
7688
QCOMPARE( index.size(), 4 );
7789

78-
QSet<QgsFeatureId> fids = index.intersect( QgsRectangle( 0, 0, 10, 10 ) );
90+
QList<QgsSpatialIndexKDBushData> fids = index.intersect( QgsRectangle( 0, 0, 10, 10 ) );
7991
QVERIFY( fids.count() == 1 );
80-
QVERIFY( fids.contains( 1 ) );
92+
QVERIFY( testContains( fids, 1, QgsPointXY( 1, 1 ) ) );
8193

82-
QSet<QgsFeatureId> fids2 = index.intersect( QgsRectangle( -10, -10, 0, 10 ) );
94+
QList<QgsSpatialIndexKDBushData> fids2 = index.intersect( QgsRectangle( -10, -10, 0, 10 ) );
8395
QCOMPARE( fids2.count(), 2 );
84-
QVERIFY( fids2.contains( 2 ) );
85-
QVERIFY( fids2.contains( 3 ) );
96+
QVERIFY( testContains( fids2, 2, QgsPointXY( -1, 1 ) ) );
97+
QVERIFY( testContains( fids2, 3, QgsPointXY( -1, -1 ) ) );
8698

87-
QSet<QgsFeatureId> fids3 = index.within( QgsPointXY( 0, 0 ), 2 );
99+
QList<QgsSpatialIndexKDBushData> fids3 = index.within( QgsPointXY( 0, 0 ), 2 );
88100
QCOMPARE( fids3.count(), 4 );
89-
QVERIFY( fids3.contains( 1 ) );
90-
QVERIFY( fids3.contains( 2 ) );
91-
QVERIFY( fids3.contains( 3 ) );
92-
QVERIFY( fids3.contains( 4 ) );
101+
QVERIFY( testContains( fids3, 1, QgsPointXY( 1, 1 ) ) );
102+
QVERIFY( testContains( fids3, 2, QgsPointXY( -1, 1 ) ) );
103+
QVERIFY( testContains( fids3, 3, QgsPointXY( -1, -1 ) ) );
104+
QVERIFY( testContains( fids3, 4, QgsPointXY( 1, -1 ) ) );
93105

94-
QSet<QgsFeatureId> fids4 = index.within( QgsPointXY( 0, 0 ), 1 );
106+
QList<QgsSpatialIndexKDBushData> fids4 = index.within( QgsPointXY( 0, 0 ), 1 );
95107
QCOMPARE( fids4.count(), 0 );
96108

97-
QSet<QgsFeatureId> fids5 = index.within( QgsPointXY( -1, -1 ), 2.1 );
109+
QList<QgsSpatialIndexKDBushData> fids5 = index.within( QgsPointXY( -1, -1 ), 2.1 );
98110
QCOMPARE( fids5.count(), 3 );
99-
QVERIFY( fids5.contains( 2 ) );
100-
QVERIFY( fids5.contains( 3 ) );
101-
QVERIFY( fids5.contains( 4 ) );
102-
103-
QgsPointXY p;
104-
QVERIFY( !index.point( -1, p ) );
105-
QVERIFY( !index.point( 5, p ) );
106-
QVERIFY( index.point( 1, p ) );
107-
QCOMPARE( p, QgsPointXY( 1, 1 ) );
108-
QVERIFY( index.point( 2, p ) );
109-
QCOMPARE( p, QgsPointXY( -1, 1 ) );
110-
QVERIFY( index.point( 3, p ) );
111-
QCOMPARE( p, QgsPointXY( -1, -1 ) );
112-
QVERIFY( index.point( 4, p ) );
113-
QCOMPARE( p, QgsPointXY( 1, -1 ) );
111+
QVERIFY( testContains( fids5, 2, QgsPointXY( -1, 1 ) ) );
112+
QVERIFY( testContains( fids5, 3, QgsPointXY( -1, -1 ) ) );
113+
QVERIFY( testContains( fids5, 4, QgsPointXY( 1, -1 ) ) );
114114
}
115115

116116
void testCopy()
@@ -128,9 +128,9 @@ class TestQgsSpatialIndexKdBush : public QObject
128128
QVERIFY( index->d->ref == 2 );
129129

130130
// test that copied index works
131-
QSet<QgsFeatureId> fids = indexCopy->intersect( QgsRectangle( 0, 0, 10, 10 ) );
131+
QList<QgsSpatialIndexKDBushData> fids = indexCopy->intersect( QgsRectangle( 0, 0, 10, 10 ) );
132132
QVERIFY( fids.count() == 1 );
133-
QVERIFY( fids.contains( 1 ) );
133+
QVERIFY( testContains( fids, 1, QgsPointXY( 1, 1 ) ) );
134134

135135
// check that the index is still shared
136136
QVERIFY( index->d == indexCopy->d );
@@ -141,7 +141,7 @@ class TestQgsSpatialIndexKdBush : public QObject
141141
// test that copied index still works
142142
fids = indexCopy->intersect( QgsRectangle( 0, 0, 10, 10 ) );
143143
QVERIFY( fids.count() == 1 );
144-
QVERIFY( fids.contains( 1 ) );
144+
QVERIFY( testContains( fids, 1, QgsPointXY( 1, 1 ) ) );
145145
QVERIFY( indexCopy->d->ref == 1 );
146146

147147
// assignment operator
@@ -157,7 +157,7 @@ class TestQgsSpatialIndexKdBush : public QObject
157157
QVERIFY( index3.d->ref == 2 );
158158
fids = index3.intersect( QgsRectangle( 0, 0, 10, 10 ) );
159159
QVERIFY( fids.count() == 1 );
160-
QVERIFY( fids.contains( 1 ) );
160+
QVERIFY( testContains( fids, 1, QgsPointXY( 1, 1 ) ) );
161161

162162
indexCopy.reset();
163163
QVERIFY( index3.d->ref == 1 );

0 commit comments

Comments
 (0)
Please sign in to comment.