Skip to content

Commit 6110931

Browse files
committed
Rename QgsSpatialIndex::insertFeature to ::addFeature, for consistency
with other classes And make QgsSpatialIndex a QgsFeatureSink
1 parent 63f597f commit 6110931

21 files changed

+139
-49
lines changed

python/core/auto_generated/qgsspatialindex.sip.in

+35-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515

1616

17-
class QgsSpatialIndex
17+
class QgsSpatialIndex : QgsFeatureSink
1818
{
1919
%Docstring
2020

@@ -75,18 +75,49 @@ Copy constructor
7575

7676

7777

78-
bool insertFeature( const QgsFeature &feature );
78+
bool insertFeature( const QgsFeature &feature ) /Deprecated/;
7979
%Docstring
8080
Adds a ``feature`` to the index.
81+
82+
.. deprecated:: Use addFeature() instead
83+
%End
84+
85+
virtual bool addFeature( QgsFeature &feature, QgsFeatureSink::Flags flags = 0 );
86+
87+
%Docstring
88+
Adds a ``feature`` to the index.
89+
90+
The ``flags`` argument is ignored.
91+
92+
.. versionadded:: 3.4
93+
%End
94+
95+
virtual bool addFeatures( QgsFeatureList &features, QgsFeatureSink::Flags flags = 0 );
96+
97+
%Docstring
98+
Adds a list of ``features`` to the index.
99+
100+
The ``flags`` argument is ignored.
101+
102+
.. seealso:: :py:func:`addFeature`
81103
%End
82104

83-
bool insertFeature( QgsFeatureId id, const QgsRectangle &bounds );
105+
bool insertFeature( QgsFeatureId id, const QgsRectangle &bounds ) /Deprecated/;
84106
%Docstring
85107
Add a feature ``id`` to the index with a specified bounding box.
86108

87109
:return: true if feature was successfully added to index.
88110

89-
.. versionadded:: 3.0
111+
.. deprecated:: Use addFeature() instead
112+
%End
113+
114+
bool addFeature( QgsFeatureId id, const QgsRectangle &bounds );
115+
%Docstring
116+
Add a feature ``id`` to the index with a specified bounding box.
117+
118+
:return: true if feature was successfully added to index.
119+
120+
.. versionadded:: 3.4
90121
%End
91122

92123
bool deleteFeature( const QgsFeature &feature );

python/plugins/processing/algs/qgis/DeleteDuplicateGeometries.py

+3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from qgis.core import (QgsFeatureRequest,
2929
QgsProcessingException,
3030
QgsFeatureSink,
31+
QgsSpatialIndex,
3132
QgsProcessingParameterFeatureSource,
3233
QgsProcessingParameterFeatureSink)
3334
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
@@ -71,11 +72,13 @@ def processAlgorithm(self, parameters, context, feedback):
7172
features = source.getFeatures(QgsFeatureRequest().setSubsetOfAttributes([]))
7273
total = 100.0 / source.featureCount() if source.featureCount() else 0
7374
geoms = dict()
75+
index = QgsSpatialIndex()
7476
for current, f in enumerate(features):
7577
if feedback.isCanceled():
7678
break
7779

7880
geoms[f.id()] = f.geometry()
81+
#index.insertFeature
7982
feedback.setProgress(int(current * total))
8083

8184
cleaned = dict(geoms)

python/plugins/processing/algs/qgis/PointsDisplacement.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def searchRect(p):
116116

117117
other_features_within_radius = index.intersects(searchRect(point))
118118
if not other_features_within_radius:
119-
index.insertFeature(f)
119+
index.addFeature(f)
120120
group = [f]
121121
clustered_groups.append(group)
122122
group_index[f.id()] = len(clustered_groups) - 1

python/plugins/processing/algs/qgis/RandomPointsAlongLines.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ def processAlgorithm(self, parameters, context, feedback):
163163
f.setAttribute('id', nPoints)
164164
f.setGeometry(geom)
165165
sink.addFeature(f, QgsFeatureSink.FastInsert)
166-
index.insertFeature(f)
166+
index.addFeature(f)
167167
points[nPoints] = p
168168
nPoints += 1
169169
feedback.setProgress(int(nPoints * total))

python/plugins/processing/algs/qgis/RandomPointsExtent.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ def processAlgorithm(self, parameters, context, feedback):
142142
f.setAttribute('id', nPoints)
143143
f.setGeometry(geom)
144144
sink.addFeature(f, QgsFeatureSink.FastInsert)
145-
index.insertFeature(f)
145+
index.addFeature(f)
146146
points[nPoints] = p
147147
nPoints += 1
148148
feedback.setProgress(int(nPoints * total))

python/plugins/processing/algs/qgis/RandomPointsLayer.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ def processAlgorithm(self, parameters, context, feedback):
151151
f.setAttribute('id', nPoints)
152152
f.setGeometry(geom)
153153
sink.addFeature(f, QgsFeatureSink.FastInsert)
154-
index.insertFeature(f)
154+
index.addFeature(f)
155155
points[nPoints] = p
156156
nPoints += 1
157157
feedback.setProgress(int(nPoints * total))

python/plugins/processing/algs/qgis/RandomPointsPolygons.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ def processAlgorithm(self, parameters, context, feedback):
197197
f.setAttribute('id', nPoints)
198198
f.setGeometry(geom)
199199
sink.addFeature(f, QgsFeatureSink.FastInsert)
200-
index.insertFeature(f)
200+
index.addFeature(f)
201201
points[nPoints] = p
202202
nPoints += 1
203203
feedback.setProgress(current_progress + int(nPoints * feature_total))

python/plugins/processing/algs/qgis/TopoColors.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ def compute_graph(features, feedback, create_id_graph=False, min_distance=0):
190190
if id_graph:
191191
id_graph.add_edge(f.id(), f2.id())
192192

193-
index.insertFeature(f)
193+
index.addFeature(f)
194194
i += 1
195195
feedback.setProgress(int(i * total))
196196

src/analysis/processing/qgsalgorithmsplitwithlines.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ QVariantMap QgsSplitWithLinesAlgorithm::processAlgorithm( const QVariantMap &par
118118
}
119119

120120
splitGeoms.insert( aSplitFeature.id(), aSplitFeature.geometry() );
121-
spatialIndex.insertFeature( aSplitFeature );
121+
spatialIndex.addFeature( aSplitFeature );
122122
}
123123

124124
QgsFeature outFeat;

src/analysis/processing/qgsoverlayutils.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ void QgsOverlayUtils::resolveOverlaps( const QgsFeatureSource &source, QgsFeatur
298298
std::unique_ptr< QgsGeometryEngine > g1engine;
299299

300300
geometries.insert( fid1, g1 );
301-
index.insertFeature( f );
301+
index.addFeature( f );
302302

303303
QgsRectangle bbox( f.geometry().boundingBox() );
304304
const QList<QgsFeatureId> ids = index.intersects( bbox );
@@ -335,7 +335,7 @@ void QgsOverlayUtils::resolveOverlaps( const QgsFeatureSource &source, QgsFeatur
335335
QgsFeature fx( newFid );
336336
fx.setGeometry( geomIntersection );
337337

338-
index.insertFeature( fx );
338+
index.addFeature( fx );
339339

340340
// figure out which feature IDs belong to this intersection. Some of the IDs can be of the newly
341341
// created geometries - in such case we need to retrieve original IDs
@@ -365,7 +365,7 @@ void QgsOverlayUtils::resolveOverlaps( const QgsFeatureSource &source, QgsFeatur
365365

366366
QgsFeature f1x( fid1 );
367367
f1x.setGeometry( g12 );
368-
index.insertFeature( f1x );
368+
index.addFeature( f1x );
369369
}
370370

371371
//
@@ -386,7 +386,7 @@ void QgsOverlayUtils::resolveOverlaps( const QgsFeatureSource &source, QgsFeatur
386386

387387
QgsFeature f2x( fid2 );
388388
f2x.setGeometry( g21 );
389-
index.insertFeature( f2x );
389+
index.addFeature( f2x );
390390
}
391391

392392
// update our temporary copy of the geometry to what is left from it

src/analysis/vector/geometry_checker/qgsfeaturepool.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ bool QgsFeaturePool::getFeature( QgsFeatureId id, QgsFeature &feature )
5757
}
5858
locker.changeMode( QgsReadWriteLocker::Write );
5959
mFeatureCache.insert( id, new QgsFeature( feature ) );
60-
mIndex.insertFeature( feature );
60+
mIndex.addFeature( feature );
6161
}
6262
return true;
6363
}
@@ -90,7 +90,8 @@ void QgsFeaturePool::insertFeature( const QgsFeature &feature )
9090
{
9191
QgsReadWriteLocker locker( mCacheLock, QgsReadWriteLocker::Write );
9292
mFeatureCache.insert( feature.id(), new QgsFeature( feature ) );
93-
mIndex.insertFeature( feature );
93+
QgsFeature indexFeature( feature );
94+
mIndex.addFeature( indexFeature );
9495
}
9596

9697
void QgsFeaturePool::refreshCache( const QgsFeature &feature )

src/analysis/vector/qgsgeometrysnapper.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,7 @@ QgsGeometry QgsInternalGeometrySnapper::snapFeature( const QgsFeature &feature )
768768
}
769769
}
770770
mProcessedGeometries.insert( feat.id(), geometry );
771-
mProcessedIndex.insertFeature( feat );
771+
mProcessedIndex.addFeature( feat );
772772
mFirstFeature = false;
773773
return geometry;
774774
}

src/analysis/vector/qgsgeometrysnappersinglesource.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ static void buildSnapIndex( QgsFeatureIterator &fi, QgsSpatialIndex &index, QVec
7070
if ( ids.isEmpty() )
7171
{
7272
// add to tree and to structure
73-
index.insertFeature( pntId, pt.boundingBox() );
73+
index.addFeature( pntId, pt.boundingBox() );
7474

7575
AnchorPoint xp;
7676
xp.x = pt.x();

src/core/providers/memory/qgsmemoryprovider.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ bool QgsMemoryProvider::addFeatures( QgsFeatureList &flist, Flags )
395395

396396
// update spatial index
397397
if ( mSpatialIndex )
398-
mSpatialIndex->insertFeature( *it );
398+
mSpatialIndex->addFeature( *it );
399399
}
400400

401401
mNextFeatureId++;
@@ -541,7 +541,7 @@ bool QgsMemoryProvider::changeGeometryValues( const QgsGeometryMap &geometry_map
541541

542542
// update spatial index
543543
if ( mSpatialIndex )
544-
mSpatialIndex->insertFeature( *fit );
544+
mSpatialIndex->addFeature( *fit );
545545
}
546546

547547
updateExtents();
@@ -583,9 +583,9 @@ bool QgsMemoryProvider::createSpatialIndex()
583583
mSpatialIndex = new QgsSpatialIndex();
584584

585585
// add existing features to index
586-
for ( QgsFeatureMap::const_iterator it = mFeatures.constBegin(); it != mFeatures.constEnd(); ++it )
586+
for ( QgsFeatureMap::iterator it = mFeatures.begin(); it != mFeatures.end(); ++it )
587587
{
588-
mSpatialIndex->insertFeature( *it );
588+
mSpatialIndex->addFeature( *it );
589589
}
590590
}
591591
return true;

src/core/qgsspatialindex.cpp

+30-8
Original file line numberDiff line numberDiff line change
@@ -299,19 +299,41 @@ bool QgsSpatialIndex::featureInfo( const QgsFeature &f, QgsRectangle &rect, QgsF
299299
return true;
300300
}
301301

302-
bool QgsSpatialIndex::insertFeature( const QgsFeature &f )
302+
bool QgsSpatialIndex::addFeature( QgsFeature &feature, QgsFeatureSink::Flags )
303303
{
304304
QgsRectangle rect;
305305
QgsFeatureId id;
306-
if ( !featureInfo( f, rect, id ) )
306+
if ( !featureInfo( feature, rect, id ) )
307307
return false;
308308

309-
return insertFeature( id, rect );
309+
return addFeature( id, rect );
310+
}
311+
312+
bool QgsSpatialIndex::addFeatures( QgsFeatureList &features, QgsFeatureSink::Flags flags )
313+
{
314+
QgsFeatureList::iterator fIt = features.begin();
315+
bool result = true;
316+
for ( ; fIt != features.end(); ++fIt )
317+
{
318+
result = result && addFeature( *fIt, flags );
319+
}
320+
return result;
321+
}
322+
323+
bool QgsSpatialIndex::insertFeature( const QgsFeature &f )
324+
{
325+
QgsFeature feature( f );
326+
return addFeature( feature );
327+
}
328+
329+
bool QgsSpatialIndex::insertFeature( QgsFeatureId id, const QgsRectangle &bounds )
330+
{
331+
return addFeature( id, bounds );
310332
}
311333

312-
bool QgsSpatialIndex::insertFeature( QgsFeatureId id, const QgsRectangle &rect )
334+
bool QgsSpatialIndex::addFeature( QgsFeatureId id, const QgsRectangle &bounds )
313335
{
314-
SpatialIndex::Region r( rectToRegion( rect ) );
336+
SpatialIndex::Region r( rectToRegion( bounds ) );
315337

316338
QMutexLocker locker( &d->mMutex );
317339

@@ -324,16 +346,16 @@ bool QgsSpatialIndex::insertFeature( QgsFeatureId id, const QgsRectangle &rect )
324346
catch ( Tools::Exception &e )
325347
{
326348
Q_UNUSED( e );
327-
QgsDebugMsg( QString( "Tools::Exception caught: " ).arg( e.what().c_str() ) );
349+
QgsDebugMsg( QStringLiteral( "Tools::Exception caught: " ).arg( e.what().c_str() ) );
328350
}
329351
catch ( const std::exception &e )
330352
{
331353
Q_UNUSED( e );
332-
QgsDebugMsg( QString( "std::exception caught: " ).arg( e.what() ) );
354+
QgsDebugMsg( QStringLiteral( "std::exception caught: " ).arg( e.what() ) );
333355
}
334356
catch ( ... )
335357
{
336-
QgsDebugMsg( "unknown spatial index exception caught" );
358+
QgsDebugMsg( QStringLiteral( "unknown spatial index exception caught" ) );
337359
}
338360

339361
return false;

src/core/qgsspatialindex.h

+32-5
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class QgsPointXY;
4040

4141
#include "qgis_core.h"
4242
#include "qgis_sip.h"
43+
#include "qgsfeaturesink.h"
4344
#include <QList>
4445
#include <QSharedDataPointer>
4546

@@ -63,7 +64,7 @@ class QgsFeatureSource;
6364
*
6465
* \see QgsSpatialIndexKDBush, which is an optimised non-mutable index for point geometries only.
6566
*/
66-
class CORE_EXPORT QgsSpatialIndex
67+
class CORE_EXPORT QgsSpatialIndex : public QgsFeatureSink
6768
{
6869

6970
public:
@@ -104,7 +105,7 @@ class CORE_EXPORT QgsSpatialIndex
104105
QgsSpatialIndex( const QgsSpatialIndex &other );
105106

106107
//! Destructor finalizes work with spatial index
107-
~QgsSpatialIndex();
108+
~QgsSpatialIndex() override;
108109

109110
//! Implement assignment operator
110111
QgsSpatialIndex &operator=( const QgsSpatialIndex &other );
@@ -113,15 +114,41 @@ class CORE_EXPORT QgsSpatialIndex
113114

114115
/**
115116
* Adds a \a feature to the index.
117+
* \deprecated Use addFeature() instead
116118
*/
117-
bool insertFeature( const QgsFeature &feature );
119+
Q_DECL_DEPRECATED bool insertFeature( const QgsFeature &feature ) SIP_DEPRECATED;
120+
121+
/**
122+
* Adds a \a feature to the index.
123+
*
124+
* The \a flags argument is ignored.
125+
*
126+
* \since QGIS 3.4
127+
*/
128+
bool addFeature( QgsFeature &feature, QgsFeatureSink::Flags flags = nullptr ) override;
129+
130+
/**
131+
* Adds a list of \a features to the index.
132+
*
133+
* The \a flags argument is ignored.
134+
*
135+
* \see addFeature()
136+
*/
137+
bool addFeatures( QgsFeatureList &features, QgsFeatureSink::Flags flags = nullptr ) override;
118138

119139
/**
120140
* Add a feature \a id to the index with a specified bounding box.
121141
* \returns true if feature was successfully added to index.
122-
* \since QGIS 3.0
142+
* \deprecated Use addFeature() instead
143+
*/
144+
Q_DECL_DEPRECATED bool insertFeature( QgsFeatureId id, const QgsRectangle &bounds ) SIP_DEPRECATED;
145+
146+
/**
147+
* Add a feature \a id to the index with a specified bounding box.
148+
* \returns true if feature was successfully added to index.
149+
* \since QGIS 3.4
123150
*/
124-
bool insertFeature( QgsFeatureId id, const QgsRectangle &bounds );
151+
bool addFeature( QgsFeatureId id, const QgsRectangle &bounds );
125152

126153
/**
127154
* Removes a \a feature from the index.

src/core/symbology/qgspointdistancerenderer.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ bool QgsPointDistanceRenderer::renderFeature( const QgsFeature &feature, QgsRend
9494
QList<QgsFeatureId> intersectList = mSpatialIndex->intersects( searchRect( point, searchDistance ) );
9595
if ( intersectList.empty() )
9696
{
97-
mSpatialIndex->insertFeature( transformedFeature );
97+
mSpatialIndex->addFeature( transformedFeature );
9898
// create new group
9999
ClusteredGroup newGroup;
100100
newGroup << GroupedFeature( transformedFeature, symbol->clone(), selected, label );

0 commit comments

Comments
 (0)