Skip to content
Permalink
Browse files

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 cea47427d0e645170d0f1688b88548ba57761bae
@@ -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:
@@ -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 )
{
@@ -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;
}
@@ -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 )
@@ -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 ) )
@@ -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() );
@@ -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 )
@@ -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()

0 comments on commit cea4742

Please sign in to comment.
You can’t perform that action at this time.