Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Optimise creation of spatial indexes
Use constructor which takes an iterator instead of manually
adding features, and don't request attributes for spatial
index features
  • Loading branch information
nyalldawson committed Oct 18, 2016
1 parent 9d6acbd commit cea4742
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 43 deletions.
Expand Up @@ -73,7 +73,7 @@ def processAlgorithm(self, progress):
geom = ft.geometry()
attrSum = ft[fieldName]

idx = QgsSpatialIndex(layer.getFeatures())
idx = QgsSpatialIndex(layer.getFeatures(QgsFeatureRequest.setSubsetOfAttributes([])))
req = QgsFeatureRequest()
completed = False
while not completed:
Expand Down
47 changes: 15 additions & 32 deletions src/analysis/vector/qgsoverlayanalyzer.cpp
Expand Up @@ -54,31 +54,25 @@ bool QgsOverlayAnalyzer::intersection( QgsVectorLayer* layerA, QgsVectorLayer* l

QgsVectorFileWriter vWriter( shapefileName, dpA->encoding(), fieldsA, outputType, crs );
QgsFeature currentFeature;
QgsSpatialIndex index;

//take only selection
if ( onlySelectedFeatures )
{
const QgsFeatureIds selectionB = layerB->selectedFeaturesIds();
QgsFeatureIds::const_iterator it = selectionB.constBegin();
for ( ; it != selectionB.constEnd(); ++it )
{
if ( !layerB->getFeatures( QgsFeatureRequest().setFilterFid( *it ) ).nextFeature( currentFeature ) )
{
continue;
}
index.insertFeature( currentFeature );
}
QgsFeatureIds selectionB = layerB->selectedFeaturesIds();
QgsFeatureRequest req = QgsFeatureRequest().setFilterFids( selectionB ).setSubsetOfAttributes( QgsAttributeList() );
QgsSpatialIndex index = QgsSpatialIndex( layerB->getFeatures( req ) );

//use QgsVectorLayer::featureAtId
const QgsFeatureIds selectionA = layerA->selectedFeaturesIds();
if ( p )
{
p->setMaximum( selectionA.size() );
}
req = QgsFeatureRequest().setFilterFids( selectionA );
QgsFeatureIterator selectionAIt = layerA->getFeatures( req );
QgsFeature currentFeature;
int processedFeatures = 0;
it = selectionA.constBegin();
for ( ; it != selectionA.constEnd(); ++it )
while ( selectionAIt.nextFeature( currentFeature ) )
{
if ( p )
{
Expand All @@ -89,10 +83,7 @@ bool QgsOverlayAnalyzer::intersection( QgsVectorLayer* layerA, QgsVectorLayer* l
{
break;
}
if ( !layerA->getFeatures( QgsFeatureRequest().setFilterFid( *it ) ).nextFeature( currentFeature ) )
{
continue;
}

intersectFeature( currentFeature, &vWriter, layerB, &index );
++processedFeatures;
}
Expand All @@ -105,11 +96,8 @@ bool QgsOverlayAnalyzer::intersection( QgsVectorLayer* layerA, QgsVectorLayer* l
//take all features
else
{
QgsFeatureIterator fit = layerB->getFeatures();
while ( fit.nextFeature( currentFeature ) )
{
index.insertFeature( currentFeature );
}
QgsFeatureRequest req = QgsFeatureRequest().setSubsetOfAttributes( QgsAttributeList() );
QgsSpatialIndex index = QgsSpatialIndex( layerB->getFeatures( req ) );

int featureCount = layerA->featureCount();
if ( p )
Expand All @@ -118,7 +106,7 @@ bool QgsOverlayAnalyzer::intersection( QgsVectorLayer* layerA, QgsVectorLayer* l
}
int processedFeatures = 0;

fit = layerA->getFeatures();
QgsFeatureIterator fit = layerA->getFeatures();

QgsFeature currentFeature;
while ( fit.nextFeature( currentFeature ) )
Expand Down Expand Up @@ -154,17 +142,12 @@ void QgsOverlayAnalyzer::intersectFeature( QgsFeature& f, QgsVectorFileWriter* v
QgsGeometry intersectGeometry;
QgsFeature overlayFeature;

QList<QgsFeatureId> intersects;
intersects = index->intersects( featureGeometry.boundingBox() );
QList<QgsFeatureId>::const_iterator it = intersects.constBegin();
QList<QgsFeatureId> intersects = index->intersects( featureGeometry.boundingBox() );
QgsFeatureRequest req = QgsFeatureRequest().setFilterFids( intersects.toSet() );
QgsFeatureIterator intersectIt = vl->getFeatures( req );
QgsFeature outFeature;
for ( ; it != intersects.constEnd(); ++it )
while ( intersectIt.nextFeature( overlayFeature ) )
{
if ( !vl->getFeatures( QgsFeatureRequest().setFilterFid( *it ) ).nextFeature( overlayFeature ) )
{
continue;
}

if ( featureGeometry.intersects( overlayFeature.geometry() ) )
{
intersectGeometry = featureGeometry.intersection( overlayFeature.geometry() );
Expand Down
6 changes: 1 addition & 5 deletions src/plugins/geometry_checker/utils/qgsfeaturepool.cpp
Expand Up @@ -44,11 +44,7 @@ QgsFeaturePool::QgsFeaturePool( QgsVectorLayer *layer, bool selectedOnly )
QgsFeature feature;
QgsFeatureRequest req;
req.setSubsetOfAttributes( QgsAttributeList() );
QgsFeatureIterator it = layer->getFeatures( req );
while ( it.nextFeature( feature ) )
{
mIndex.insertFeature( feature );
}
mIndex = QgsSpatialIndex( layer->getFeatures( req ) );
}

bool QgsFeaturePool::get( QgsFeatureId id , QgsFeature& feature )
Expand Down
6 changes: 1 addition & 5 deletions src/plugins/geometry_snapper/qgsgeometrysnapper.cpp
Expand Up @@ -45,11 +45,7 @@ QgsGeometrySnapper::QgsGeometrySnapper( QgsVectorLayer *adjustLayer, QgsVectorLa
QgsFeature feature;
QgsFeatureRequest req;
req.setSubsetOfAttributes( QgsAttributeList() );
QgsFeatureIterator it = mReferenceLayer->getFeatures( req );
while ( it.nextFeature( feature ) )
{
mIndex.insertFeature( feature );
}
mIndex = QgsSpatialIndex( mReferenceLayer->getFeatures( req ) );
}

QFuture<void> QgsGeometrySnapper::processFeatures()
Expand Down

0 comments on commit cea4742

Please sign in to comment.