Skip to content

Commit

Permalink
More efficient distance to vertex calculation in extract nodes alg
Browse files Browse the repository at this point in the history
Instead of calculating the whole distance from the start of the
geometry to the vertex for EVERY vertex, we just keep a running
sum of the cumulative distance so far and add the length of
each segment as we go.

Stats:

Before:
- dataset 1: > 20 minutes, still going at 14%....
- dataset 2: 92 seconds

After:
- dataset 1: 3 seconds
- dataset 2: 1.7 seconds

BAM!
  • Loading branch information
nyalldawson committed Nov 14, 2017
1 parent fc6c69b commit d14b859
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/analysis/processing/qgsalgorithmextractnodes.cpp
Expand Up @@ -107,23 +107,26 @@ QVariantMap QgsExtractNodesAlgorithm::processAlgorithm( const QVariantMap &param
else
{
QgsAbstractGeometry::vertex_iterator vi = inputGeom.constGet()->vertices_begin();
QgsPoint vertex;
double cumulativeDistance = 0.0;
int vertexPos = 0;
while ( vi != inputGeom.constGet()->vertices_end() )
{
QgsVertexId vertexId = vi.vertexId();
double distance = QgsGeometryUtils::distanceToVertex( *( inputGeom.constGet() ), vertexId );
double angle = inputGeom.constGet()->vertexAngle( vertexId ) * 180 / M_PI;
QgsAttributes attrs = f.attributes();
attrs << vertexPos
<< distance
<< cumulativeDistance
<< angle;
QgsFeature outputFeature = QgsFeature();
outputFeature.setAttributes( attrs );
outputFeature.setGeometry( QgsGeometry( ( *vi ).clone() ) );
sink->addFeature( outputFeature, QgsFeatureSink::FastInsert );
vi++;
vertexPos++;

// calculate distance to next vertex
double distanceToNext = inputGeom.constGet()->segmentLength( vertexId );
cumulativeDistance += distanceToNext;
}
}
feedback->setProgress( i * step );
Expand Down

0 comments on commit d14b859

Please sign in to comment.