QGIS API Documentation 3.99.0-Master (357b655ed83)
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
21#include "qgsfeaturerequest.h"
22
23#include <QString>
24
25using namespace Qt::StringLiterals;
26
28
29QString QgsOrderByExpressionAlgorithm::name() const
30{
31 return u"orderbyexpression"_s;
32}
33
34QString QgsOrderByExpressionAlgorithm::displayName() const
35{
36 return QObject::tr( "Order by expression" );
37}
38
39QStringList QgsOrderByExpressionAlgorithm::tags() const
40{
41 return QObject::tr( "orderby,sort,expression,field" ).split( ',' );
42}
43
44QString QgsOrderByExpressionAlgorithm::group() const
45{
46 return QObject::tr( "Vector general" );
47}
48
49QString QgsOrderByExpressionAlgorithm::groupId() const
50{
51 return u"vectorgeneral"_s;
52}
53
54void QgsOrderByExpressionAlgorithm::initAlgorithm( const QVariantMap & )
55{
56 addParameter( new QgsProcessingParameterFeatureSource( u"INPUT"_s, QObject::tr( "Input layer" ), QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::Vector ) ) );
57 addParameter( new QgsProcessingParameterExpression( u"EXPRESSION"_s, QObject::tr( "Expression" ), QVariant(), u"INPUT"_s ) );
58 addParameter( new QgsProcessingParameterBoolean( u"ASCENDING"_s, QObject::tr( "Sort ascending" ), true ) );
59 addParameter( new QgsProcessingParameterBoolean( u"NULLS_FIRST"_s, QObject::tr( "Sort nulls first" ), false ) );
60
61 addParameter( new QgsProcessingParameterFeatureSink( u"OUTPUT"_s, QObject::tr( "Ordered" ) ) );
62}
63
64QString QgsOrderByExpressionAlgorithm::shortHelpString() const
65{
66 return QObject::tr( "This algorithm sorts a vector layer according to an expression. Be careful, it might not work as expected with some providers, "
67 "the order might not be kept every time.\n\n"
68 "For help with QGIS expression functions, see the inbuilt help for specific functions "
69 "which is available in the expression builder." );
70}
71
72QString QgsOrderByExpressionAlgorithm::shortDescription() const
73{
74 return QObject::tr( "Sorts a vector layer according to an expression." );
75}
76
77Qgis::ProcessingAlgorithmDocumentationFlags QgsOrderByExpressionAlgorithm::documentationFlags() const
78{
80}
81
82QgsOrderByExpressionAlgorithm *QgsOrderByExpressionAlgorithm::createInstance() const
83{
84 return new QgsOrderByExpressionAlgorithm();
85}
86
87QVariantMap QgsOrderByExpressionAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
88{
89 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, u"INPUT"_s, context ) );
90 if ( !source )
91 throw QgsProcessingException( invalidSourceError( parameters, u"INPUT"_s ) );
92
93 const QString expressionString = parameterAsExpression( parameters, u"EXPRESSION"_s, context );
94
95 const bool ascending = parameterAsBoolean( parameters, u"ASCENDING"_s, context );
96 const bool nullsFirst = parameterAsBoolean( parameters, u"NULLS_FIRST"_s, context );
97
98 QString sinkId;
99 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, u"OUTPUT"_s, context, sinkId, source->fields(), source->wkbType(), source->sourceCrs(), QgsFeatureSink::RegeneratePrimaryKey ) );
100 if ( !sink )
101 throw QgsProcessingException( invalidSinkError( parameters, u"OUTPUT"_s ) );
102
103 const long count = source->featureCount();
104 const double step = count > 0 ? 100.0 / count : 1;
105 int current = 0;
106
107 QgsFeatureRequest request;
108 request.addOrderBy( expressionString, ascending, nullsFirst );
109
110 QgsFeature inFeature;
112 while ( features.nextFeature( inFeature ) )
113 {
114 if ( feedback->isCanceled() )
115 {
116 break;
117 }
118 if ( !sink->addFeature( inFeature ) )
119 throw QgsProcessingException( writeFeatureError( sink.get(), parameters, u"OUTPUT"_s ) );
120 feedback->setProgress( current * step );
121 current++;
122 }
123
124 sink->finalize();
125
126 QVariantMap outputs;
127 outputs.insert( u"OUTPUT"_s, sinkId );
128 return outputs;
129}
130
@ Vector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
Definition qgis.h:3610
@ RegeneratesPrimaryKey
Algorithm always drops any existing primary keys or FID values and regenerates them in outputs.
Definition qgis.h:3690
QFlags< ProcessingAlgorithmDocumentationFlag > ProcessingAlgorithmDocumentationFlags
Flags describing algorithm behavior for documentation purposes.
Definition qgis.h:3701
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Definition qgis.h:3782
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.
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.
@ RegeneratePrimaryKey
This flag indicates, that a primary key field cannot be guaranteed to be unique and the sink should i...
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:60
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition qgsfeedback.h:55
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition qgsfeedback.h:63
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.