Skip to content
Permalink
Browse files

Merge pull request #5612 from nirvn/native_nodes

Optimize extract nodes algorithm
  • Loading branch information
nyalldawson committed Nov 13, 2017
2 parents 6dbdbea + b7d0582 commit 3c8d50d5250fdd97c68978399ce059ee0783583f

This file was deleted.

@@ -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()
@@ -64,7 +64,6 @@
from .ExportGeometryInfo import ExportGeometryInfo
from .ExtendLines import ExtendLines
from .ExtentFromLayer import ExtentFromLayer
from .ExtractNodes import ExtractNodes
from .ExtractSpecificNodes import ExtractSpecificNodes
from .FieldPyculator import FieldsPyculator
from .FieldsCalculator import FieldsCalculator
@@ -190,7 +189,6 @@ def getAlgs(self):
ExportGeometryInfo(),
ExtendLines(),
ExtentFromLayer(),
ExtractNodes(),
ExtractSpecificNodes(),
FieldsCalculator(),
FieldsMapper(),
@@ -1164,6 +1164,12 @@ tests:
OUTPUT:
name: expected/extract_nodes_multipolys.gml
type: vector
compare:
fields:
distance:
precision: 7
angle:
precision: 7

- algorithm: qgis:extractnodes
name: Extract nodes from polygons
@@ -1175,6 +1181,12 @@ tests:
OUTPUT:
name: expected/extract_nodes_polys.gml
type: vector
compare:
fields:
distance:
precision: 7
angle:
precision: 7

- algorithm: qgis:extractnodes
name: Extract nodes from multilines
@@ -1186,6 +1198,12 @@ tests:
OUTPUT:
name: expected/extract_nodes_multilines.gml
type: vector
compare:
fields:
distance:
precision: 7
angle:
precision: 7

- algorithm: qgis:extractnodes
name: Extract nodes from lines
@@ -1197,6 +1215,12 @@ tests:
OUTPUT:
name: expected/extract_nodes_lines.gml
type: vector
compare:
fields:
distance:
precision: 7
angle:
precision: 7

- algorithm: native:simplifygeometries
name: Simplify (lines)
@@ -35,6 +35,7 @@ SET(QGIS_ANALYSIS_SRCS
processing/qgsalgorithmextractbyexpression.cpp
processing/qgsalgorithmextractbyextent.cpp
processing/qgsalgorithmextractbylocation.cpp
processing/qgsalgorithmextractnodes.cpp
processing/qgsalgorithmfiledownloader.cpp
processing/qgsalgorithmfixgeometries.cpp
processing/qgsalgorithmjoinbyattribute.cpp
@@ -0,0 +1,137 @@
/***************************************************************************
qgsalgorithmextractnodes.cpp
--------------------------
begin : November 2017
copyright : (C) 2017 by Mathieu Pellerin
email : nirvn dot asia at gmail dot com
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgsalgorithmextractnodes.h"

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

///@cond PRIVATE

QString QgsExtractNodesAlgorithm::name() const
{
return QStringLiteral( "extractnodes" );
}

QString QgsExtractNodesAlgorithm::displayName() const
{
return QObject::tr( "Extract nodes" );
}

QStringList QgsExtractNodesAlgorithm::tags() const
{
return QObject::tr( "points,vertex,vertices" ).split( ',' );
}

QString QgsExtractNodesAlgorithm::group() const
{
return QObject::tr( "Vector geometry" );
}

QString QgsExtractNodesAlgorithm::shortHelpString() const
{
return QObject::tr( "This algorithm takes a line or polygon layer and generates a point layer with points representing the nodes in the input lines or polygons. The attributes associated to each point are the same ones associated to the line or polygon that the point belongs to." ) +
QStringLiteral( "\n\n" ) +
QObject::tr( "Additional fields are added to the nodes indicating the node index (beginning at 0), distance along original geometry and bisector angle of node for original geometry." );
}

QgsExtractNodesAlgorithm *QgsExtractNodesAlgorithm::createInstance() const
{
return new QgsExtractNodesAlgorithm();
}

void QgsExtractNodesAlgorithm::initAlgorithm( const QVariantMap & )
{
addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ) ) );

addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Nodes" ) ) );
}

QVariantMap QgsExtractNodesAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
{
std::unique_ptr< QgsFeatureSource > featureSource( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
if ( !featureSource )
return QVariantMap();

QgsWkbTypes::Type outputWkbType = QgsWkbTypes::Point;
if ( QgsWkbTypes::hasM( featureSource->wkbType() ) )
{
outputWkbType = QgsWkbTypes::addM( outputWkbType );
}
if ( QgsWkbTypes::hasZ( featureSource->wkbType() ) )
{
outputWkbType = QgsWkbTypes::addZ( outputWkbType );
}

QgsFields outputFields = featureSource->fields();
outputFields.append( QgsField( QStringLiteral( "node_index" ), QVariant::Int, QString(), 10, 0 ) );
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 fi = featureSource->getFeatures( QgsFeatureRequest() );
QgsFeature f;
int i = -1;
while ( fi.nextFeature( f ) )
{
i++;
if ( feedback->isCanceled() )
{
break;
}

QgsGeometry inputGeom = f.geometry();
if ( inputGeom.isNull() )
{
sink->addFeature( f, QgsFeatureSink::FastInsert );
}
else
{
QgsAbstractGeometry::vertex_iterator vi = inputGeom.constGet()->vertices_begin();
QgsPoint vertex;
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
<< angle;
QgsFeature outputFeature = QgsFeature();
outputFeature.setAttributes( attrs );
outputFeature.setGeometry( QgsGeometry( ( *vi ).clone() ) );
sink->addFeature( outputFeature, QgsFeatureSink::FastInsert );
vi++;
vertexPos++;
}
}
feedback->setProgress( i * step );
}

QVariantMap outputs;
outputs.insert( QStringLiteral( "OUTPUT" ), dest );
return outputs;
}

///@endcond

0 comments on commit 3c8d50d

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