QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 
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 QString QgsOrderByExpressionAlgorithm::groupId() const
46 {
47  return QStringLiteral( "vectorgeneral" );
48 }
49 
50 void QgsOrderByExpressionAlgorithm::initAlgorithm( const QVariantMap & )
51 {
52  addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ), QList< int >() << QgsProcessing::TypeVector ) );
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 
60 QString 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 
68 QgsOrderByExpressionAlgorithm *QgsOrderByExpressionAlgorithm::createInstance() const
69 {
70  return new QgsOrderByExpressionAlgorithm();
71 }
72 
73 QVariantMap 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;
97  QgsFeatureIterator features = source->getFeatures( request, QgsProcessingFeatureSource::FlagSkipGeometryValidityChecks );
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 
QgsFeedback::setProgress
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition: qgsfeedback.h:76
qgsfeaturerequest.h
QgsFeatureRequest::addOrderBy
QgsFeatureRequest & addOrderBy(const QString &expression, bool ascending=true)
Adds a new OrderByClause, appending it as the least important one.
Definition: qgsfeaturerequest.cpp:193
QgsProcessingFeedback
Base class for providing feedback from a processing algorithm.
Definition: qgsprocessingfeedback.h:37
QgsFeedback::isCanceled
bool isCanceled() const SIP_HOLDGIL
Tells whether the operation has been canceled already.
Definition: qgsfeedback.h:67
QgsProcessingFeatureSource::FlagSkipGeometryValidityChecks
@ FlagSkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Definition: qgsprocessingutils.h:584
QgsProcessingParameterFeatureSource
An input feature source (such as vector layers) parameter for processing algorithms.
Definition: qgsprocessingparameters.h:3057
qgsalgorithmorderbyexpression.h
QgsProcessingParameterFeatureSink
A feature sink output for processing algorithms.
Definition: qgsprocessingparameters.h:3219
QgsFeatureRequest
This class wraps a request for features to a vector layer (or directly its vector data provider).
Definition: qgsfeaturerequest.h:83
QgsProcessing::TypeVector
@ TypeVector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
Definition: qgsprocessing.h:54
QgsProcessingContext
Contains information about the context in which a processing algorithm is executed.
Definition: qgsprocessingcontext.h:46
QgsProcessingParameterBoolean
A boolean parameter for processing algorithms.
Definition: qgsprocessingparameters.h:1709
QgsFeatureIterator::nextFeature
bool nextFeature(QgsFeature &f)
Definition: qgsfeatureiterator.h:399
QgsProcessingParameterExpression
An expression parameter for processing algorithms.
Definition: qgsprocessingparameters.h:2739
QgsFeature
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:55
QgsFeatureIterator
Wrapper for iterator of features from vector data provider or vector layer.
Definition: qgsfeatureiterator.h:289
QgsProcessingException
Custom exception class for processing related exceptions.
Definition: qgsexception.h:82