Skip to content

Commit

Permalink
[processing] Use optimized spatial index load in Join by Nearest
Browse files Browse the repository at this point in the history
and Split with Lines algorithms
  • Loading branch information
nyalldawson committed Jan 2, 2020
1 parent c46e89f commit bbacab4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
16 changes: 9 additions & 7 deletions src/analysis/processing/qgsalgorithmjoinbynearest.cpp
Expand Up @@ -188,23 +188,20 @@ QVariantMap QgsJoinByNearestAlgorithm::processAlgorithm( const QVariantMap &para

// make spatial index
QgsFeatureIterator f2 = input2->getFeatures( QgsFeatureRequest().setDestinationCrs( input->sourceCrs(), context.transformContext() ).setSubsetOfAttributes( fields2Fetch ) );
QgsSpatialIndex index( QgsSpatialIndex::FlagStoreFeatureGeometries );
QHash< QgsFeatureId, QgsAttributes > input2AttributeCache;
QgsFeature f;
double step = input2->featureCount() > 0 ? 50.0 / input2->featureCount() : 1;
int i = 0;
while ( f2.nextFeature( f ) )
QgsSpatialIndex index( f2, [&]( const QgsFeature & f )->bool
{
i++;
if ( feedback->isCanceled() )
break;
return false;

feedback->setProgress( i * step );

if ( !f.hasGeometry() )
continue;
return true;

index.addFeature( f );
// only keep selected attributes
QgsAttributes attributes;
for ( int j = 0; j < f.attributes().count(); ++j )
Expand All @@ -214,10 +211,15 @@ QVariantMap QgsJoinByNearestAlgorithm::processAlgorithm( const QVariantMap &para
attributes << f.attribute( j );
}
input2AttributeCache.insert( f.id(), attributes );
}

return true;
}, QgsSpatialIndex::FlagStoreFeatureGeometries );

QgsFeature f;

// create extra null attributes for non-matched records (the +2 is for the "n" and "distance", and start/end x/y fields)
QgsAttributes nullMatch;
nullMatch.reserve( fields2Indices.size() + 6 );
for ( int i = 0; i < fields2Indices.count() + 6; ++i )
nullMatch << QVariant();

Expand Down
10 changes: 5 additions & 5 deletions src/analysis/processing/qgsalgorithmsplitwithlines.cpp
Expand Up @@ -102,24 +102,24 @@ QVariantMap QgsSplitWithLinesAlgorithm::processAlgorithm( const QVariantMap &par
if ( !sink )
throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "OUTPUT" ) ) );

QgsSpatialIndex spatialIndex;
QMap< QgsFeatureId, QgsGeometry > splitGeoms;
QgsFeatureRequest request;
request.setNoAttributes();
request.setDestinationCrs( source->sourceCrs(), context.transformContext() );

QgsFeatureIterator splitLines = linesSource->getFeatures( request );
QgsFeature aSplitFeature;
while ( splitLines.nextFeature( aSplitFeature ) )

QgsSpatialIndex spatialIndex( splitLines, [&]( const QgsFeature & aSplitFeature )-> bool
{
if ( feedback->isCanceled() )
{
break;
return false;
}

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

QgsFeature outFeat;
QgsFeatureIterator features = source->getFeatures();
Expand Down

0 comments on commit bbacab4

Please sign in to comment.