QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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
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
68QString QgsOrderByExpressionAlgorithm::shortDescription() const
69{
70 return QObject::tr( "Sorts a vector layer according to an expression." );
71}
72
73Qgis::ProcessingAlgorithmDocumentationFlags QgsOrderByExpressionAlgorithm::documentationFlags() const
74{
76}
77
78QgsOrderByExpressionAlgorithm *QgsOrderByExpressionAlgorithm::createInstance() const
79{
80 return new QgsOrderByExpressionAlgorithm();
81}
82
83QVariantMap QgsOrderByExpressionAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
84{
85 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
86 if ( !source )
87 throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "INPUT" ) ) );
88
89 const QString expressionString = parameterAsExpression( parameters, QStringLiteral( "EXPRESSION" ), context );
90
91 const bool ascending = parameterAsBoolean( parameters, QStringLiteral( "ASCENDING" ), context );
92 const bool nullsFirst = parameterAsBoolean( parameters, QStringLiteral( "NULLS_FIRST" ), context );
93
94 QString sinkId;
95 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, sinkId, source->fields(), source->wkbType(), source->sourceCrs(), QgsFeatureSink::RegeneratePrimaryKey ) );
96 if ( !sink )
97 throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "OUTPUT" ) ) );
98
99 const long count = source->featureCount();
100 const double step = count > 0 ? 100.0 / count : 1;
101 int current = 0;
102
103 QgsFeatureRequest request;
104 request.addOrderBy( expressionString, ascending, nullsFirst );
105
106 QgsFeature inFeature;
108 while ( features.nextFeature( inFeature ) )
109 {
110 if ( feedback->isCanceled() )
111 {
112 break;
113 }
114 if ( !sink->addFeature( inFeature ) )
115 throw QgsProcessingException( writeFeatureError( sink.get(), parameters, QStringLiteral( "OUTPUT" ) ) );
116 feedback->setProgress( current * step );
117 current++;
118 }
119
120 sink->finalize();
121
122 QVariantMap outputs;
123 outputs.insert( QStringLiteral( "OUTPUT" ), sinkId );
124 return outputs;
125}
126
@ Vector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
Definition qgis.h:3539
@ RegeneratesPrimaryKey
Algorithm always drops any existing primary keys or FID values and regenerates them in outputs.
Definition qgis.h:3619
QFlags< ProcessingAlgorithmDocumentationFlag > ProcessingAlgorithmDocumentationFlags
Flags describing algorithm behavior for documentation purposes.
Definition qgis.h:3630
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Definition qgis.h:3711
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: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.