QGIS API Documentation 3.38.0-Grenoble (exported)
Loading...
Searching...
No Matches
qgsalgorithmorderbyexpression.cpp
Go to the documentation of this file.
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
20#include "qgsfeaturerequest.h"
21
22
24
25QString QgsOrderByExpressionAlgorithm::name() const
26{
27 return QStringLiteral( "orderbyexpression" );
28}
29
30QString QgsOrderByExpressionAlgorithm::displayName() const
31{
32 return QObject::tr( "Order by expression" );
33}
34
35QStringList QgsOrderByExpressionAlgorithm::tags() const
36{
37 return QObject::tr( "orderby,sort,expression,field" ).split( ',' );
38}
39
40QString QgsOrderByExpressionAlgorithm::group() const
41{
42 return QObject::tr( "Vector general" );
43}
44
45QString QgsOrderByExpressionAlgorithm::groupId() const
46{
47 return QStringLiteral( "vectorgeneral" );
48}
49
50void QgsOrderByExpressionAlgorithm::initAlgorithm( const QVariantMap & )
51{
52 addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ), QList< int >() << static_cast< int >( Qgis::ProcessingSourceType::Vector ) ) );
53 addParameter( new QgsProcessingParameterExpression( QStringLiteral( "EXPRESSION" ), QObject::tr( "Expression" ), QVariant(), QStringLiteral( "INPUT" ) ) );
54 addParameter( new QgsProcessingParameterBoolean( QStringLiteral( "ASCENDING" ), QObject::tr( "Sort ascending" ), true ) );
55 addParameter( new QgsProcessingParameterBoolean( QStringLiteral( "NULLS_FIRST" ), QObject::tr( "Sort nulls first" ), false ) );
56
57 addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Ordered" ) ) );
58}
59
60QString QgsOrderByExpressionAlgorithm::shortHelpString() const
61{
62 return QObject::tr( "This algorithm sorts a vector layer according to an expression. Be careful, it might not work as expected with some providers, "
63 "the order might not be kept every time.\n\n"
64 "For help with QGIS expression functions, see the inbuilt help for specific functions "
65 "which is available in the expression builder." );
66}
67
68QgsOrderByExpressionAlgorithm *QgsOrderByExpressionAlgorithm::createInstance() const
69{
70 return new QgsOrderByExpressionAlgorithm();
71}
72
73QVariantMap QgsOrderByExpressionAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
74{
75 std::unique_ptr< QgsProcessingFeatureSource > source( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
76 if ( !source )
77 throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "INPUT" ) ) );
78
79 const QString expressionString = parameterAsExpression( parameters, QStringLiteral( "EXPRESSION" ), context );
80
81 const bool ascending = parameterAsBoolean( parameters, QStringLiteral( "ASCENDING" ), context );
82 const bool nullsFirst = parameterAsBoolean( parameters, QStringLiteral( "NULLS_FIRST" ), context );
83
84 QString sinkId;
85 std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, sinkId, source->fields(), source->wkbType(), source->sourceCrs() ) );
86 if ( !sink )
87 throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "OUTPUT" ) ) );
88
89 const long count = source->featureCount();
90 const double step = count > 0 ? 100.0 / count : 1;
91 int current = 0;
92
93 QgsFeatureRequest request;
94 request.addOrderBy( expressionString, ascending, nullsFirst );
95
96 QgsFeature inFeature;
98 while ( features.nextFeature( inFeature ) )
99 {
100 if ( feedback->isCanceled() )
101 {
102 break;
103 }
104 if ( !sink->addFeature( inFeature ) )
105 throw QgsProcessingException( writeFeatureError( sink.get(), parameters, QStringLiteral( "OUTPUT" ) ) );
106 feedback->setProgress( current * step );
107 current++;
108 }
109
110 QVariantMap outputs;
111 outputs.insert( QStringLiteral( "OUTPUT" ), sinkId );
112 return outputs;
113}
114
@ Vector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
Fetch next feature and stores in f, returns true on success.
This class wraps a request for features to a vector layer (or directly its vector data provider).
QgsFeatureRequest & addOrderBy(const QString &expression, bool ascending=true)
Adds a new OrderByClause, appending it as the least important one.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition qgsfeedback.h:53
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition qgsfeedback.h:61
Contains information about the context in which a processing algorithm is executed.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
A boolean parameter for processing algorithms.
An expression parameter for processing algorithms.
A feature sink output for processing algorithms.
An input feature source (such as vector layers) parameter for processing algorithms.