|
| 1 | +/*************************************************************************** |
| 2 | + qgsalgorithmorderbyexpression.h |
| 3 | + --------------------- |
| 4 | + begin : November 2017 |
| 5 | + copyright : (C) 2017 by Etienne Trimaille |
| 6 | + email : etienne dot trimaille 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 | + |
| 19 | +#include "qgsalgorithmorderbyexpression.h" |
| 20 | +#include "qgsfeaturerequest.h" |
| 21 | + |
| 22 | + |
| 23 | +///@cond PRIVATE |
| 24 | + |
| 25 | +QString QgsOrderByExpressionAlgorithm::name() const |
| 26 | +{ |
| 27 | + return QStringLiteral( "orderbyexpression" ); |
| 28 | +} |
| 29 | + |
| 30 | +QString QgsOrderByExpressionAlgorithm::displayName() const |
| 31 | +{ |
| 32 | + return QObject::tr( "Order by expression" ); |
| 33 | +} |
| 34 | + |
| 35 | +QStringList QgsOrderByExpressionAlgorithm::tags() const |
| 36 | +{ |
| 37 | + return QObject::tr( "orderby,sort,expression,field" ).split( ',' ); |
| 38 | +} |
| 39 | + |
| 40 | +QString QgsOrderByExpressionAlgorithm::group() const |
| 41 | +{ |
| 42 | + return QObject::tr( "Vector general" ); |
| 43 | +} |
| 44 | + |
| 45 | +void QgsOrderByExpressionAlgorithm::initAlgorithm( const QVariantMap & ) |
| 46 | +{ |
| 47 | + addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ) ) ); |
| 48 | + addParameter( new QgsProcessingParameterExpression( QStringLiteral( "EXPRESSION" ), QObject::tr( "Expression" ), QVariant(), QStringLiteral( "INPUT" ) ) ); |
| 49 | + addParameter( new QgsProcessingParameterBoolean( QStringLiteral( "ASCENDING" ), QObject::tr( "Ascending" ), true ) ); |
| 50 | + addParameter( new QgsProcessingParameterBoolean( QStringLiteral( "NULLS_FIRST" ), QObject::tr( "Nulls first" ), false ) ); |
| 51 | + |
| 52 | + addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Output layer" ) ) ); |
| 53 | +} |
| 54 | + |
| 55 | +QString QgsOrderByExpressionAlgorithm::shortHelpString() const |
| 56 | +{ |
| 57 | + return QObject::tr( "This algorithm sorts a vector layer according to an expression. Be careful, it might not work as expected with some providers, the order might not be kept every time." ); |
| 58 | +} |
| 59 | + |
| 60 | +QgsOrderByExpressionAlgorithm *QgsOrderByExpressionAlgorithm::createInstance() const |
| 61 | +{ |
| 62 | + return new QgsOrderByExpressionAlgorithm(); |
| 63 | +} |
| 64 | + |
| 65 | +QVariantMap QgsOrderByExpressionAlgorithm::processAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) |
| 66 | +{ |
| 67 | + std::unique_ptr< QgsFeatureSource > source( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) ); |
| 68 | + if ( !source ) |
| 69 | + return QVariantMap(); |
| 70 | + |
| 71 | + QString expressionString = parameterAsExpression( parameters, QStringLiteral( "EXPRESSION" ), context ); |
| 72 | + |
| 73 | + bool ascending = parameterAsBool( parameters, QStringLiteral( "ASCENDING" ), context ); |
| 74 | + bool nullsFirst = parameterAsBool( parameters, QStringLiteral( "NULLS_FIRST" ), context ); |
| 75 | + |
| 76 | + QString sinkId; |
| 77 | + std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, sinkId, source->fields(), source->wkbType(), source->sourceCrs() ) ); |
| 78 | + if ( !sink ) |
| 79 | + return QVariantMap(); |
| 80 | + |
| 81 | + long count = source->featureCount(); |
| 82 | + double step = count > 0 ? 100.0 / count : 1; |
| 83 | + int current = 0; |
| 84 | + |
| 85 | + QgsFeatureRequest request; |
| 86 | + request.addOrderBy( expressionString, ascending, nullsFirst ); |
| 87 | + |
| 88 | + QgsFeature inFeature; |
| 89 | + QgsFeatureIterator features = source->getFeatures( request ); |
| 90 | + while ( features.nextFeature( inFeature ) ) |
| 91 | + { |
| 92 | + if ( feedback->isCanceled() ) |
| 93 | + { |
| 94 | + break; |
| 95 | + } |
| 96 | + sink->addFeature( inFeature ); |
| 97 | + feedback->setProgress( current * step ); |
| 98 | + current++; |
| 99 | + } |
| 100 | + |
| 101 | + QVariantMap outputs; |
| 102 | + outputs.insert( QStringLiteral( "OUTPUT" ), sinkId ); |
| 103 | + return outputs; |
| 104 | +} |
| 105 | + |
| 106 | +///@endcond |
0 commit comments