|
| 1 | +/*************************************************************************** |
| 2 | + qgsalgorithmextractnodes.cpp |
| 3 | + -------------------------- |
| 4 | + begin : November 2017 |
| 5 | + copyright : (C) 2017 by Mathieu Pellerin |
| 6 | + email : nirvn dot asia at gmail dot com |
| 7 | + ***************************************************************************/ |
| 8 | + |
| 9 | +/*************************************************************************** |
| 10 | + * * |
| 11 | + * This program is free software; you can redistribute it and/or modify * |
| 12 | + * it under the terms of the GNU General Public License as published by * |
| 13 | + * the Free Software Foundation; either version 2 of the License, or * |
| 14 | + * (at your option) any later version. * |
| 15 | + * * |
| 16 | + ***************************************************************************/ |
| 17 | + |
| 18 | +#include "qgsalgorithmextractnodes.h" |
| 19 | + |
| 20 | +///@cond PRIVATE |
| 21 | + |
| 22 | +QString QgsExtractNodesAlgorithm::name() const |
| 23 | +{ |
| 24 | + return QStringLiteral( "extractnodes" ); |
| 25 | +} |
| 26 | + |
| 27 | +QString QgsExtractNodesAlgorithm::displayName() const |
| 28 | +{ |
| 29 | + return QObject::tr( "Extract nodes" ); |
| 30 | +} |
| 31 | + |
| 32 | +QStringList QgsExtractNodesAlgorithm::tags() const |
| 33 | +{ |
| 34 | + return QObject::tr( "points,vertex,vertices" ).split( ',' ); |
| 35 | +} |
| 36 | + |
| 37 | +QString QgsExtractNodesAlgorithm::group() const |
| 38 | +{ |
| 39 | + return QObject::tr( "Vector geometry" ); |
| 40 | +} |
| 41 | + |
| 42 | +QString QgsExtractNodesAlgorithm::shortHelpString() const |
| 43 | +{ |
| 44 | + 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." ) + |
| 45 | + QStringLiteral( "\n\n" ) + |
| 46 | + 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." ); |
| 47 | +} |
| 48 | + |
| 49 | +QgsExtractNodesAlgorithm *QgsExtractNodesAlgorithm::createInstance() const |
| 50 | +{ |
| 51 | + return new QgsExtractNodesAlgorithm(); |
| 52 | +} |
| 53 | + |
| 54 | +void QgsExtractNodesAlgorithm::initAlgorithm( const QVariantMap & ) |
| 55 | +{ |
| 56 | + addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ) ) ); |
| 57 | + |
| 58 | + addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Nodes" ) ) ); |
| 59 | +} |
| 60 | + |
| 61 | +QVariantMap QgsExtractNodesAlgorithm::processAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) |
| 62 | +{ |
| 63 | + std::unique_ptr< QgsFeatureSource > featureSource( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) ); |
| 64 | + if ( !featureSource ) |
| 65 | + return QVariantMap(); |
| 66 | + |
| 67 | + QgsWkbTypes::Type outputWkbType = QgsWkbTypes::Point; |
| 68 | + if ( QgsWkbTypes::hasM( featureSource->wkbType() ) ) |
| 69 | + { |
| 70 | + outputWkbType = QgsWkbTypes::addM( outputWkbType ); |
| 71 | + } |
| 72 | + if ( QgsWkbTypes::hasZ( featureSource->wkbType() ) ) |
| 73 | + { |
| 74 | + outputWkbType = QgsWkbTypes::addZ( outputWkbType ); |
| 75 | + } |
| 76 | + |
| 77 | + QgsFields outputFields = featureSource->fields(); |
| 78 | + outputFields.append( QgsField( QStringLiteral( "node_index" ), QVariant::Int, QString(), 10, 0 ) ); |
| 79 | + outputFields.append( QgsField( QStringLiteral( "distance" ), QVariant::Double, QString(), 20, 6 ) ); |
| 80 | + outputFields.append( QgsField( QStringLiteral( "angle" ), QVariant::Double, QString(), 20, 6 ) ); |
| 81 | + |
| 82 | + QString dest; |
| 83 | + std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, outputFields, outputWkbType, featureSource->sourceCrs() ) ); |
| 84 | + if ( !sink ) |
| 85 | + return QVariantMap(); |
| 86 | + |
| 87 | + double step = featureSource->featureCount() > 0 ? 100.0 / featureSource->featureCount() : 1; |
| 88 | + QgsFeatureIterator it = featureSource->getFeatures( QgsFeatureRequest() ); |
| 89 | + QgsFeature f; |
| 90 | + int i = -1; |
| 91 | + while ( it.nextFeature( f ) ) |
| 92 | + { |
| 93 | + i++; |
| 94 | + if ( feedback->isCanceled() ) |
| 95 | + { |
| 96 | + break; |
| 97 | + } |
| 98 | + |
| 99 | + QgsGeometry inputGeom = f.geometry(); |
| 100 | + if ( inputGeom.isNull() ) |
| 101 | + { |
| 102 | + sink->addFeature( f, QgsFeatureSink::FastInsert ); |
| 103 | + } |
| 104 | + else |
| 105 | + { |
| 106 | + const QgsCoordinateSequence sequence = inputGeom.constGet()->coordinateSequence(); |
| 107 | + for ( const QgsRingSequence &part : sequence ) |
| 108 | + { |
| 109 | + int vertexPos = 0; |
| 110 | + for ( const QgsPointSequence &ring : part ) |
| 111 | + { |
| 112 | + for ( int j = 0; j < ring.count(); ++ j ) |
| 113 | + { |
| 114 | + double distance = inputGeom.distanceToVertex( vertexPos ); |
| 115 | + double angle = inputGeom.angleAtVertex( vertexPos ) * 180 / M_PI_2; |
| 116 | + QgsAttributes attrs = f.attributes(); |
| 117 | + attrs << vertexPos |
| 118 | + << distance |
| 119 | + << angle; |
| 120 | + QgsFeature outputFeature = QgsFeature(); |
| 121 | + outputFeature.setAttributes( attrs ); |
| 122 | + outputFeature.setGeometry( QgsGeometry( ring.at( j ).clone() ) ); |
| 123 | + sink->addFeature( outputFeature, QgsFeatureSink::FastInsert ); |
| 124 | + vertexPos++; |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + feedback->setProgress( i * step ); |
| 130 | + } |
| 131 | + |
| 132 | + QVariantMap outputs; |
| 133 | + outputs.insert( QStringLiteral( "OUTPUT" ), dest ); |
| 134 | + return outputs; |
| 135 | +} |
| 136 | + |
| 137 | +///@endcond |
0 commit comments