Skip to content

Commit

Permalink
Rename QgsSpatialIndex::insertFeature to ::addFeature, for consistency
Browse files Browse the repository at this point in the history
with other classes

And make QgsSpatialIndex a QgsFeatureSink
  • Loading branch information
nyalldawson committed Sep 28, 2018
1 parent 63f597f commit 6110931
Show file tree
Hide file tree
Showing 21 changed files with 139 additions and 49 deletions.
39 changes: 35 additions & 4 deletions python/core/auto_generated/qgsspatialindex.sip.in
Expand Up @@ -14,7 +14,7 @@



class QgsSpatialIndex
class QgsSpatialIndex : QgsFeatureSink
{
%Docstring

Expand Down Expand Up @@ -75,18 +75,49 @@ Copy constructor



bool insertFeature( const QgsFeature &feature );
bool insertFeature( const QgsFeature &feature ) /Deprecated/;
%Docstring
Adds a ``feature`` to the index.

.. deprecated:: Use addFeature() instead
%End

virtual bool addFeature( QgsFeature &feature, QgsFeatureSink::Flags flags = 0 );

%Docstring
Adds a ``feature`` to the index.

The ``flags`` argument is ignored.

.. versionadded:: 3.4
%End

virtual bool addFeatures( QgsFeatureList &features, QgsFeatureSink::Flags flags = 0 );

%Docstring
Adds a list of ``features`` to the index.

The ``flags`` argument is ignored.

.. seealso:: :py:func:`addFeature`
%End

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

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

.. versionadded:: 3.0
.. deprecated:: Use addFeature() instead
%End

bool addFeature( QgsFeatureId id, const QgsRectangle &bounds );
%Docstring
Add a feature ``id`` to the index with a specified bounding box.

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

.. versionadded:: 3.4
%End

bool deleteFeature( const QgsFeature &feature );
Expand Down
Expand Up @@ -28,6 +28,7 @@
from qgis.core import (QgsFeatureRequest,
QgsProcessingException,
QgsFeatureSink,
QgsSpatialIndex,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterFeatureSink)
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
Expand Down Expand Up @@ -71,11 +72,13 @@ def processAlgorithm(self, parameters, context, feedback):
features = source.getFeatures(QgsFeatureRequest().setSubsetOfAttributes([]))
total = 100.0 / source.featureCount() if source.featureCount() else 0
geoms = dict()
index = QgsSpatialIndex()
for current, f in enumerate(features):
if feedback.isCanceled():
break

geoms[f.id()] = f.geometry()
#index.insertFeature
feedback.setProgress(int(current * total))

cleaned = dict(geoms)
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/PointsDisplacement.py
Expand Up @@ -116,7 +116,7 @@ def searchRect(p):

other_features_within_radius = index.intersects(searchRect(point))
if not other_features_within_radius:
index.insertFeature(f)
index.addFeature(f)
group = [f]
clustered_groups.append(group)
group_index[f.id()] = len(clustered_groups) - 1
Expand Down
Expand Up @@ -163,7 +163,7 @@ def processAlgorithm(self, parameters, context, feedback):
f.setAttribute('id', nPoints)
f.setGeometry(geom)
sink.addFeature(f, QgsFeatureSink.FastInsert)
index.insertFeature(f)
index.addFeature(f)
points[nPoints] = p
nPoints += 1
feedback.setProgress(int(nPoints * total))
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/RandomPointsExtent.py
Expand Up @@ -142,7 +142,7 @@ def processAlgorithm(self, parameters, context, feedback):
f.setAttribute('id', nPoints)
f.setGeometry(geom)
sink.addFeature(f, QgsFeatureSink.FastInsert)
index.insertFeature(f)
index.addFeature(f)
points[nPoints] = p
nPoints += 1
feedback.setProgress(int(nPoints * total))
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/RandomPointsLayer.py
Expand Up @@ -151,7 +151,7 @@ def processAlgorithm(self, parameters, context, feedback):
f.setAttribute('id', nPoints)
f.setGeometry(geom)
sink.addFeature(f, QgsFeatureSink.FastInsert)
index.insertFeature(f)
index.addFeature(f)
points[nPoints] = p
nPoints += 1
feedback.setProgress(int(nPoints * total))
Expand Down
Expand Up @@ -197,7 +197,7 @@ def processAlgorithm(self, parameters, context, feedback):
f.setAttribute('id', nPoints)
f.setGeometry(geom)
sink.addFeature(f, QgsFeatureSink.FastInsert)
index.insertFeature(f)
index.addFeature(f)
points[nPoints] = p
nPoints += 1
feedback.setProgress(current_progress + int(nPoints * feature_total))
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/TopoColors.py
Expand Up @@ -190,7 +190,7 @@ def compute_graph(features, feedback, create_id_graph=False, min_distance=0):
if id_graph:
id_graph.add_edge(f.id(), f2.id())

index.insertFeature(f)
index.addFeature(f)
i += 1
feedback.setProgress(int(i * total))

Expand Down
2 changes: 1 addition & 1 deletion src/analysis/processing/qgsalgorithmsplitwithlines.cpp
Expand Up @@ -118,7 +118,7 @@ QVariantMap QgsSplitWithLinesAlgorithm::processAlgorithm( const QVariantMap &par
}

splitGeoms.insert( aSplitFeature.id(), aSplitFeature.geometry() );
spatialIndex.insertFeature( aSplitFeature );
spatialIndex.addFeature( aSplitFeature );
}

QgsFeature outFeat;
Expand Down
8 changes: 4 additions & 4 deletions src/analysis/processing/qgsoverlayutils.cpp
Expand Up @@ -298,7 +298,7 @@ void QgsOverlayUtils::resolveOverlaps( const QgsFeatureSource &source, QgsFeatur
std::unique_ptr< QgsGeometryEngine > g1engine;

geometries.insert( fid1, g1 );
index.insertFeature( f );
index.addFeature( f );

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

index.insertFeature( fx );
index.addFeature( fx );

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

QgsFeature f1x( fid1 );
f1x.setGeometry( g12 );
index.insertFeature( f1x );
index.addFeature( f1x );
}

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

QgsFeature f2x( fid2 );
f2x.setGeometry( g21 );
index.insertFeature( f2x );
index.addFeature( f2x );
}

// update our temporary copy of the geometry to what is left from it
Expand Down
5 changes: 3 additions & 2 deletions src/analysis/vector/geometry_checker/qgsfeaturepool.cpp
Expand Up @@ -57,7 +57,7 @@ bool QgsFeaturePool::getFeature( QgsFeatureId id, QgsFeature &feature )
}
locker.changeMode( QgsReadWriteLocker::Write );
mFeatureCache.insert( id, new QgsFeature( feature ) );
mIndex.insertFeature( feature );
mIndex.addFeature( feature );
}
return true;
}
Expand Down Expand Up @@ -90,7 +90,8 @@ void QgsFeaturePool::insertFeature( const QgsFeature &feature )
{
QgsReadWriteLocker locker( mCacheLock, QgsReadWriteLocker::Write );
mFeatureCache.insert( feature.id(), new QgsFeature( feature ) );
mIndex.insertFeature( feature );
QgsFeature indexFeature( feature );
mIndex.addFeature( indexFeature );
}

void QgsFeaturePool::refreshCache( const QgsFeature &feature )
Expand Down
2 changes: 1 addition & 1 deletion src/analysis/vector/qgsgeometrysnapper.cpp
Expand Up @@ -768,7 +768,7 @@ QgsGeometry QgsInternalGeometrySnapper::snapFeature( const QgsFeature &feature )
}
}
mProcessedGeometries.insert( feat.id(), geometry );
mProcessedIndex.insertFeature( feat );
mProcessedIndex.addFeature( feat );
mFirstFeature = false;
return geometry;
}
2 changes: 1 addition & 1 deletion src/analysis/vector/qgsgeometrysnappersinglesource.cpp
Expand Up @@ -70,7 +70,7 @@ static void buildSnapIndex( QgsFeatureIterator &fi, QgsSpatialIndex &index, QVec
if ( ids.isEmpty() )
{
// add to tree and to structure
index.insertFeature( pntId, pt.boundingBox() );
index.addFeature( pntId, pt.boundingBox() );

AnchorPoint xp;
xp.x = pt.x();
Expand Down
8 changes: 4 additions & 4 deletions src/core/providers/memory/qgsmemoryprovider.cpp
Expand Up @@ -395,7 +395,7 @@ bool QgsMemoryProvider::addFeatures( QgsFeatureList &flist, Flags )

// update spatial index
if ( mSpatialIndex )
mSpatialIndex->insertFeature( *it );
mSpatialIndex->addFeature( *it );
}

mNextFeatureId++;
Expand Down Expand Up @@ -541,7 +541,7 @@ bool QgsMemoryProvider::changeGeometryValues( const QgsGeometryMap &geometry_map

// update spatial index
if ( mSpatialIndex )
mSpatialIndex->insertFeature( *fit );
mSpatialIndex->addFeature( *fit );
}

updateExtents();
Expand Down Expand Up @@ -583,9 +583,9 @@ bool QgsMemoryProvider::createSpatialIndex()
mSpatialIndex = new QgsSpatialIndex();

// add existing features to index
for ( QgsFeatureMap::const_iterator it = mFeatures.constBegin(); it != mFeatures.constEnd(); ++it )
for ( QgsFeatureMap::iterator it = mFeatures.begin(); it != mFeatures.end(); ++it )
{
mSpatialIndex->insertFeature( *it );
mSpatialIndex->addFeature( *it );
}
}
return true;
Expand Down
38 changes: 30 additions & 8 deletions src/core/qgsspatialindex.cpp
Expand Up @@ -299,19 +299,41 @@ bool QgsSpatialIndex::featureInfo( const QgsFeature &f, QgsRectangle &rect, QgsF
return true;
}

bool QgsSpatialIndex::insertFeature( const QgsFeature &f )
bool QgsSpatialIndex::addFeature( QgsFeature &feature, QgsFeatureSink::Flags )
{
QgsRectangle rect;
QgsFeatureId id;
if ( !featureInfo( f, rect, id ) )
if ( !featureInfo( feature, rect, id ) )
return false;

return insertFeature( id, rect );
return addFeature( id, rect );
}

bool QgsSpatialIndex::addFeatures( QgsFeatureList &features, QgsFeatureSink::Flags flags )
{
QgsFeatureList::iterator fIt = features.begin();
bool result = true;
for ( ; fIt != features.end(); ++fIt )
{
result = result && addFeature( *fIt, flags );
}
return result;
}

bool QgsSpatialIndex::insertFeature( const QgsFeature &f )
{
QgsFeature feature( f );
return addFeature( feature );
}

bool QgsSpatialIndex::insertFeature( QgsFeatureId id, const QgsRectangle &bounds )
{
return addFeature( id, bounds );
}

bool QgsSpatialIndex::insertFeature( QgsFeatureId id, const QgsRectangle &rect )
bool QgsSpatialIndex::addFeature( QgsFeatureId id, const QgsRectangle &bounds )
{
SpatialIndex::Region r( rectToRegion( rect ) );
SpatialIndex::Region r( rectToRegion( bounds ) );

QMutexLocker locker( &d->mMutex );

Expand All @@ -324,16 +346,16 @@ bool QgsSpatialIndex::insertFeature( QgsFeatureId id, const QgsRectangle &rect )
catch ( Tools::Exception &e )
{
Q_UNUSED( e );
QgsDebugMsg( QString( "Tools::Exception caught: " ).arg( e.what().c_str() ) );
QgsDebugMsg( QStringLiteral( "Tools::Exception caught: " ).arg( e.what().c_str() ) );
}
catch ( const std::exception &e )
{
Q_UNUSED( e );
QgsDebugMsg( QString( "std::exception caught: " ).arg( e.what() ) );
QgsDebugMsg( QStringLiteral( "std::exception caught: " ).arg( e.what() ) );
}
catch ( ... )
{
QgsDebugMsg( "unknown spatial index exception caught" );
QgsDebugMsg( QStringLiteral( "unknown spatial index exception caught" ) );
}

return false;
Expand Down
37 changes: 32 additions & 5 deletions src/core/qgsspatialindex.h
Expand Up @@ -40,6 +40,7 @@ class QgsPointXY;

#include "qgis_core.h"
#include "qgis_sip.h"
#include "qgsfeaturesink.h"
#include <QList>
#include <QSharedDataPointer>

Expand All @@ -63,7 +64,7 @@ class QgsFeatureSource;
*
* \see QgsSpatialIndexKDBush, which is an optimised non-mutable index for point geometries only.
*/
class CORE_EXPORT QgsSpatialIndex
class CORE_EXPORT QgsSpatialIndex : public QgsFeatureSink
{

public:
Expand Down Expand Up @@ -104,7 +105,7 @@ class CORE_EXPORT QgsSpatialIndex
QgsSpatialIndex( const QgsSpatialIndex &other );

//! Destructor finalizes work with spatial index
~QgsSpatialIndex();
~QgsSpatialIndex() override;

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

/**
* Adds a \a feature to the index.
* \deprecated Use addFeature() instead
*/
bool insertFeature( const QgsFeature &feature );
Q_DECL_DEPRECATED bool insertFeature( const QgsFeature &feature ) SIP_DEPRECATED;

/**
* Adds a \a feature to the index.
*
* The \a flags argument is ignored.
*
* \since QGIS 3.4
*/
bool addFeature( QgsFeature &feature, QgsFeatureSink::Flags flags = nullptr ) override;

/**
* Adds a list of \a features to the index.
*
* The \a flags argument is ignored.
*
* \see addFeature()
*/
bool addFeatures( QgsFeatureList &features, QgsFeatureSink::Flags flags = nullptr ) override;

/**
* Add a feature \a id to the index with a specified bounding box.
* \returns true if feature was successfully added to index.
* \since QGIS 3.0
* \deprecated Use addFeature() instead
*/
Q_DECL_DEPRECATED bool insertFeature( QgsFeatureId id, const QgsRectangle &bounds ) SIP_DEPRECATED;

/**
* Add a feature \a id to the index with a specified bounding box.
* \returns true if feature was successfully added to index.
* \since QGIS 3.4
*/
bool insertFeature( QgsFeatureId id, const QgsRectangle &bounds );
bool addFeature( QgsFeatureId id, const QgsRectangle &bounds );

/**
* Removes a \a feature from the index.
Expand Down
2 changes: 1 addition & 1 deletion src/core/symbology/qgspointdistancerenderer.cpp
Expand Up @@ -94,7 +94,7 @@ bool QgsPointDistanceRenderer::renderFeature( const QgsFeature &feature, QgsRend
QList<QgsFeatureId> intersectList = mSpatialIndex->intersects( searchRect( point, searchDistance ) );
if ( intersectList.empty() )
{
mSpatialIndex->insertFeature( transformedFeature );
mSpatialIndex->addFeature( transformedFeature );
// create new group
ClusteredGroup newGroup;
newGroup << GroupedFeature( transformedFeature, symbol->clone(), selected, label );
Expand Down

0 comments on commit 6110931

Please sign in to comment.