QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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(
67 "This algorithm sorts a vector layer according to an expression. Be careful, it might not work as expected with some providers, "
68 "the order might not be kept every time.\n\n"
69 "For help with QGIS expression functions, see the inbuilt help for specific functions "
70 "which is available in the expression builder."
71 );
72}
73
74QString QgsOrderByExpressionAlgorithm::shortDescription() const
75{
76 return QObject::tr( "Sorts a vector layer according to an expression." );
77}
78
79Qgis::ProcessingAlgorithmDocumentationFlags QgsOrderByExpressionAlgorithm::documentationFlags() const
80{
82}
83
84QgsOrderByExpressionAlgorithm *QgsOrderByExpressionAlgorithm::createInstance() const
85{
86 return new QgsOrderByExpressionAlgorithm();
87}
88
89QVariantMap QgsOrderByExpressionAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
90{
91 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, u"INPUT"_s, context ) );
92 if ( !source )
93 throw QgsProcessingException( invalidSourceError( parameters, u"INPUT"_s ) );
94
95 const QString expressionString = parameterAsExpression( parameters, u"EXPRESSION"_s, context );
96
97 const bool ascending = parameterAsBoolean( parameters, u"ASCENDING"_s, context );
98 const bool nullsFirst = parameterAsBoolean( parameters, u"NULLS_FIRST"_s, context );
99
100 QString sinkId;
101 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, u"OUTPUT"_s, context, sinkId, source->fields(), source->wkbType(), source->sourceCrs(), QgsFeatureSink::RegeneratePrimaryKey ) );
102 if ( !sink )
103 throw QgsProcessingException( invalidSinkError( parameters, u"OUTPUT"_s ) );
104
105 const long count = source->featureCount();
106 const double step = count > 0 ? 100.0 / count : 1;
107 int current = 0;
108
109 QgsFeatureRequest request;
110 request.addOrderBy( expressionString, ascending, nullsFirst );
111
112 QgsFeature inFeature;
114 while ( features.nextFeature( inFeature ) )
115 {
116 if ( feedback->isCanceled() )
117 {
118 break;
119 }
120 if ( !sink->addFeature( inFeature ) )
121 throw QgsProcessingException( writeFeatureError( sink.get(), parameters, u"OUTPUT"_s ) );
122 feedback->setProgress( current * step );
123 current++;
124 }
125
126 sink->finalize();
127
128 QVariantMap outputs;
129 outputs.insert( u"OUTPUT"_s, sinkId );
130 return outputs;
131}
132
@ Vector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
Definition qgis.h:3653
@ RegeneratesPrimaryKey
Algorithm always drops any existing primary keys or FID values and regenerates them in outputs.
Definition qgis.h:3734
QFlags< ProcessingAlgorithmDocumentationFlag > ProcessingAlgorithmDocumentationFlags
Flags describing algorithm behavior for documentation purposes.
Definition qgis.h:3745
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Definition qgis.h:3828
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:56
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition qgsfeedback.h:65
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.