22QString QgsExtractByAttributeAlgorithm::name()
 const 
   24  return QStringLiteral( 
"extractbyattribute" );
 
   27QString QgsExtractByAttributeAlgorithm::displayName()
 const 
   29  return QObject::tr( 
"Extract by attribute" );
 
   32QStringList QgsExtractByAttributeAlgorithm::tags()
 const 
   34  return QObject::tr( 
"extract,filter,attribute,value,contains,null,field" ).split( 
',' );
 
   37QString QgsExtractByAttributeAlgorithm::group()
 const 
   39  return QObject::tr( 
"Vector selection" );
 
   42QString QgsExtractByAttributeAlgorithm::groupId()
 const 
   44  return QStringLiteral( 
"vectorselection" );
 
   47void QgsExtractByAttributeAlgorithm::initAlgorithm( 
const QVariantMap & )
 
   50  addParameter( 
new QgsProcessingParameterField( QStringLiteral( 
"FIELD" ), QObject::tr( 
"Selection attribute" ), QVariant(), QStringLiteral( 
"INPUT" ) ) );
 
   51  addParameter( 
new QgsProcessingParameterEnum( QStringLiteral( 
"OPERATOR" ), QObject::tr( 
"Operator" ), QStringList() << QObject::tr( 
"=" ) << QObject::tr( 
"≠" ) << QObject::tr( 
">" ) << QObject::tr( 
"≥" ) << QObject::tr( 
"<" ) << QObject::tr( 
"≤" ) << QObject::tr( 
"begins with" ) << QObject::tr( 
"contains" ) << QObject::tr( 
"is null" ) << QObject::tr( 
"is not null" ) << QObject::tr( 
"does not contain" ), 
false, 0 ) );
 
   57  addParameter( failOutput );
 
   60QString QgsExtractByAttributeAlgorithm::shortHelpString()
 const 
   62  return QObject::tr( 
"This algorithm creates a new vector layer that only contains matching features from an input layer. " 
   63                      "The criteria for adding features to the resulting layer is defined based on the values " 
   64                      "of an attribute from the input layer." );
 
   67QString QgsExtractByAttributeAlgorithm::shortDescription()
 const 
   69  return QObject::tr( 
"Creates a vector layer that only contains features matching an attribute value from an input layer." );
 
   72QgsExtractByAttributeAlgorithm *QgsExtractByAttributeAlgorithm::createInstance()
 const 
   74  return new QgsExtractByAttributeAlgorithm();
 
   79  std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, QStringLiteral( 
"INPUT" ), context ) );
 
   83  const QString fieldName = parameterAsString( parameters, QStringLiteral( 
"FIELD" ), context );
 
   84  const Operation op = 
static_cast<Operation
>( parameterAsEnum( parameters, QStringLiteral( 
"OPERATOR" ), context ) );
 
   85  const QString value = parameterAsString( parameters, QStringLiteral( 
"VALUE" ), context );
 
   87  QString matchingSinkId;
 
   88  std::unique_ptr<QgsFeatureSink> matchingSink( parameterAsSink( parameters, QStringLiteral( 
"OUTPUT" ), context, matchingSinkId, source->fields(), source->wkbType(), source->sourceCrs() ) );
 
   92  QString nonMatchingSinkId;
 
   93  std::unique_ptr<QgsFeatureSink> nonMatchingSink( parameterAsSink( parameters, QStringLiteral( 
"FAIL_OUTPUT" ), context, nonMatchingSinkId, source->fields(), source->wkbType(), source->sourceCrs() ) );
 
   95  const int idx = source->fields().lookupField( fieldName );
 
   97    throw QgsProcessingException( QObject::tr( 
"Field '%1' was not found in INPUT source" ).arg( fieldName ) );
 
   99  const QMetaType::Type fieldType = source->fields().at( idx ).type();
 
  101  if ( fieldType != QMetaType::Type::QString && ( op == BeginsWith || op == Contains || op == DoesNotContain ) )
 
  107        method = QObject::tr( 
"begins with" );
 
  110        method = QObject::tr( 
"contains" );
 
  113        method = QObject::tr( 
"does not contain" );
 
  120    throw QgsProcessingException( QObject::tr( 
"Operator '%1' can be used only with string fields." ).arg( method ) );
 
  129      expr = QStringLiteral( 
"%1 = %3" ).arg( fieldRef, quotedVal );
 
  132      expr = QStringLiteral( 
"%1 != %3" ).arg( fieldRef, quotedVal );
 
  135      expr = QStringLiteral( 
"%1 > %3" ).arg( fieldRef, quotedVal );
 
  137    case GreaterThanEqualTo:
 
  138      expr = QStringLiteral( 
"%1 >= %3" ).arg( fieldRef, quotedVal );
 
  141      expr = QStringLiteral( 
"%1 < %3" ).arg( fieldRef, quotedVal );
 
  143    case LessThanEqualTo:
 
  144      expr = QStringLiteral( 
"%1 <= %3" ).arg( fieldRef, quotedVal );
 
  147      expr = QStringLiteral( 
"%1 LIKE '%2%'" ).arg( fieldRef, value );
 
  150      expr = QStringLiteral( 
"%1 LIKE '%%2%'" ).arg( fieldRef, value );
 
  153      expr = QStringLiteral( 
"%1 IS NULL" ).arg( fieldRef );
 
  156      expr = QStringLiteral( 
"%1 IS NOT NULL" ).arg( fieldRef );
 
  159      expr = QStringLiteral( 
"%1 NOT LIKE '%%2%'" ).arg( fieldRef, value );
 
  164  if ( expression.hasParserError() )
 
  169  QgsExpressionContext expressionContext = createExpressionContext( parameters, context, source.get() );
 
  171  const long count = source->featureCount();
 
  173  const double step = count > 0 ? 100.0 / count : 1;
 
  176  if ( !nonMatchingSink )
 
  193        throw QgsProcessingException( writeFeatureError( matchingSink.get(), parameters, QStringLiteral( 
"OUTPUT" ) ) );
 
  202    expressionContext.
setFields( source->fields() );
 
  203    expression.prepare( &expressionContext );
 
  215      if ( expression.evaluate( &expressionContext ).toBool() )
 
  218          throw QgsProcessingException( writeFeatureError( matchingSink.get(), parameters, QStringLiteral( 
"OUTPUT" ) ) );
 
  223          throw QgsProcessingException( writeFeatureError( nonMatchingSink.get(), parameters, QStringLiteral( 
"FAIL_OUTPUT" ) ) );
 
  232    matchingSink->finalize();
 
  233  if ( nonMatchingSink )
 
  234    nonMatchingSink->finalize();
 
  237  outputs.insert( QStringLiteral( 
"OUTPUT" ), matchingSinkId );
 
  238  if ( nonMatchingSink )
 
  239    outputs.insert( QStringLiteral( 
"FAIL_OUTPUT" ), nonMatchingSinkId );
 
@ Vector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
 
@ VectorAnyGeometry
Any vector layer with geometry.
 
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
 
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
 
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
 
void setFields(const QgsFields &fields)
Convenience function for setting a fields for the context.
 
Handles parsing and evaluation of expressions (formerly called "search strings").
 
static QString quotedValue(const QVariant &value)
Returns a string representation of a literal value, including appropriate quotations where required.
 
static QString quotedColumnRef(QString name)
Returns a quoted column reference (in double quotes)
 
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 & setFilterExpression(const QString &expression)
Set the filter expression.
 
QgsFeatureRequest & setExpressionContext(const QgsExpressionContext &context)
Sets the expression context used to evaluate filter expressions.
 
@ FastInsert
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
 
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
 
bool isCanceled() const
Tells whether the operation has been canceled already.
 
void setProgress(double progress)
Sets the current progress for the feedback object.
 
Contains information about the context in which a processing algorithm is executed.
 
void setCreateByDefault(bool createByDefault)
Sets whether the destination should be created by default.
 
Custom exception class for processing related exceptions.
 
Base class for providing feedback from a processing algorithm.
 
An enum based parameter for processing algorithms, allowing for selection from predefined values.
 
A feature sink output for processing algorithms.
 
An input feature source (such as vector layers) parameter for processing algorithms.
 
A vector layer or feature source field parameter for processing algorithms.
 
A string parameter for processing algorithms.