Skip to content
Permalink
Browse files

More efficient distance to vertex calculation in extract nodes alg

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 13, 2017
1 parent fc6c69b commit d14b8592f822dced637512508bb9e93f34823cbd
Showing with 6 additions and 3 deletions.
  1. +6 −3 src/analysis/processing/qgsalgorithmextractnodes.cpp
@@ -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 );

0 comments on commit d14b859

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