Skip to content
Permalink
Browse files

Use vertex iterator

  • Loading branch information
nirvn committed Nov 13, 2017
1 parent 7cb15c0 commit 66b27dcd500079ff335669163759da2227255d35
@@ -66,6 +66,9 @@ def name(self):
def displayName(self):
return self.tr('Extract specific nodes')

def tags(self):
return self.tr('points,vertex,vertices').split(',')

def processAlgorithm(self, parameters, context, feedback):
source = self.parameterAsSource(parameters, self.INPUT, context)
fields = source.fields()
@@ -17,6 +17,9 @@

#include "qgsalgorithmextractnodes.h"

#include "qgsabstractgeometry.h"
#include "qgsgeometryutils.h"

///@cond PRIVATE

QString QgsExtractNodesAlgorithm::name() const
@@ -76,19 +79,19 @@ QVariantMap QgsExtractNodesAlgorithm::processAlgorithm( const QVariantMap &param

QgsFields outputFields = featureSource->fields();
outputFields.append( QgsField( QStringLiteral( "node_index" ), QVariant::Int, QString(), 10, 0 ) );
outputFields.append( QgsField( QStringLiteral( "distance" ), QVariant::Double, QString(), 20, 6 ) );
outputFields.append( QgsField( QStringLiteral( "angle" ), QVariant::Double, QString(), 20, 6 ) );
outputFields.append( QgsField( QStringLiteral( "distance" ), QVariant::Double, QString(), 20, 14 ) );
outputFields.append( QgsField( QStringLiteral( "angle" ), QVariant::Double, QString(), 20, 14 ) );

QString dest;
std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, outputFields, outputWkbType, featureSource->sourceCrs() ) );
if ( !sink )
return QVariantMap();

double step = featureSource->featureCount() > 0 ? 100.0 / featureSource->featureCount() : 1;
QgsFeatureIterator it = featureSource->getFeatures( QgsFeatureRequest() );
QgsFeatureIterator fi = featureSource->getFeatures( QgsFeatureRequest() );
QgsFeature f;
int i = -1;
while ( it.nextFeature( f ) )
while ( fi.nextFeature( f ) )
{
i++;
if ( feedback->isCanceled() )
@@ -103,27 +106,24 @@ QVariantMap QgsExtractNodesAlgorithm::processAlgorithm( const QVariantMap &param
}
else
{
const QgsCoordinateSequence sequence = inputGeom.constGet()->coordinateSequence();
for ( const QgsRingSequence &part : sequence )
QgsAbstractGeometry::vertex_iterator vi = inputGeom.constGet()->vertices_begin();
QgsPoint vertex;
int vertexPos = 0;
while ( vi != inputGeom.constGet()->vertices_end() )
{
int vertexPos = 0;
for ( const QgsPointSequence &ring : part )
{
for ( int j = 0; j < ring.count(); ++ j )
{
double distance = inputGeom.distanceToVertex( vertexPos );
double angle = inputGeom.angleAtVertex( vertexPos ) * 180 / M_PI_2;
QgsAttributes attrs = f.attributes();
attrs << vertexPos
<< distance
<< angle;
QgsFeature outputFeature = QgsFeature();
outputFeature.setAttributes( attrs );
outputFeature.setGeometry( QgsGeometry( ring.at( j ).clone() ) );
sink->addFeature( outputFeature, QgsFeatureSink::FastInsert );
vertexPos++;
}
}
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
<< angle;
QgsFeature outputFeature = QgsFeature();
outputFeature.setAttributes( attrs );
outputFeature.setGeometry( QgsGeometry( ( *vi ).clone() ) );
sink->addFeature( outputFeature, QgsFeatureSink::FastInsert );
vi++;
vertexPos++;
}
}
feedback->setProgress( i * step );

0 comments on commit 66b27dc

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