QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsalgorithmattributeindex.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsalgorithmattributeraster.cpp
3  ---------------------
4  begin : November 2019
5  copyright : (C) 2019 by Alexander Bruy
6  email : alexander dot bruy 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 
19 #include "qgsvectorlayer.h"
20 #include "qgsvectordataprovider.h"
21 
23 
24 QString QgsAttributeIndexAlgorithm::name() const
25 {
26  return QStringLiteral( "createattributeindex" );
27 }
28 
29 QString QgsAttributeIndexAlgorithm::displayName() const
30 {
31  return QObject::tr( "Create attribute index" );
32 }
33 
34 QStringList QgsAttributeIndexAlgorithm::tags() const
35 {
36  return QObject::tr( "table,attribute,index,create,vector" ).split( ',' );
37 }
38 
39 QString QgsAttributeIndexAlgorithm::group() const
40 {
41  return QObject::tr( "Vector general" );
42 }
43 
44 QString QgsAttributeIndexAlgorithm::groupId() const
45 {
46  return QStringLiteral( "vectorgeneral" );
47 }
48 
49 QgsProcessingAlgorithm::Flags QgsAttributeIndexAlgorithm::flags() const
50 {
52 }
53 
54 
55 QString QgsAttributeIndexAlgorithm::shortHelpString() const
56 {
57  return QObject::tr( "Creates an index to speed up queries made against "
58  "a field in a table. Support for index creation is "
59  "dependent on the layer's data provider and the field type." );
60 }
61 
62 QgsAttributeIndexAlgorithm *QgsAttributeIndexAlgorithm::createInstance() const
63 {
64  return new QgsAttributeIndexAlgorithm();
65 }
66 
67 void QgsAttributeIndexAlgorithm::initAlgorithm( const QVariantMap & )
68 {
69  addParameter( new QgsProcessingParameterVectorLayer( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ) ) );
70  addParameter( new QgsProcessingParameterField( QStringLiteral( "FIELD" ), QObject::tr( "Attribute to index" ), QVariant(), QStringLiteral( "INPUT" ) ) );
71 
72  addOutput( new QgsProcessingOutputVectorLayer( QStringLiteral( "OUTPUT" ), QObject::tr( "Indexed layer" ) ) );
73 }
74 
75 QVariantMap QgsAttributeIndexAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
76 {
77  QgsVectorLayer *layer = parameterAsVectorLayer( parameters, QStringLiteral( "INPUT" ), context );
78 
79  if ( !layer )
80  throw QgsProcessingException( QObject::tr( "Could not load source layer for %1." ).arg( QLatin1String( "INPUT" ) ) );
81 
82  const QString field = parameterAsString( parameters, QStringLiteral( "FIELD" ), context );
83 
84  QgsVectorDataProvider *provider = layer->dataProvider();
85 
86  const int fieldIndex = layer->fields().lookupField( field );
87  if ( fieldIndex < 0 || layer->fields().fieldOrigin( fieldIndex ) != QgsFields::OriginProvider )
88  {
89  feedback->pushInfo( QObject::tr( "Can not create attribute index on %1" ).arg( field ) );
90  }
91  else
92  {
93  const int providerIndex = layer->fields().fieldOriginIndex( fieldIndex );
95  {
96  if ( !provider->createAttributeIndex( providerIndex ) )
97  {
98  feedback->pushInfo( QObject::tr( "Could not create attribute index" ) );
99  }
100  }
101  else
102  {
103  feedback->pushInfo( QObject::tr( "Layer's data provider does not support creating attribute indexes" ) );
104  }
105  }
106 
107  QVariantMap outputs;
108  outputs.insert( QStringLiteral( "OUTPUT" ), layer->id() );
109  return outputs;
110 }
111 
QgsFields::OriginProvider
@ OriginProvider
Field comes from the underlying data provider of the vector layer (originIndex = index in provider's ...
Definition: qgsfields.h:51
QgsVectorDataProvider::createAttributeIndex
virtual bool createAttributeIndex(int field)
Create an attribute index on the datasource.
Definition: qgsvectordataprovider.cpp:202
QgsVectorLayer::dataProvider
QgsVectorDataProvider * dataProvider() FINAL
Returns the layer's data provider, it may be nullptr.
Definition: qgsvectorlayer.cpp:676
QgsProcessingFeedback
Base class for providing feedback from a processing algorithm.
Definition: qgsprocessingfeedback.h:37
QgsProcessingFeedback::pushInfo
virtual void pushInfo(const QString &info)
Pushes a general informational message from the algorithm.
Definition: qgsprocessingfeedback.cpp:77
QgsProcessingOutputVectorLayer
A vector layer output for processing algorithms.
Definition: qgsprocessingoutputs.h:179
QgsVectorDataProvider::CreateAttributeIndex
@ CreateAttributeIndex
Can create indexes on provider's fields.
Definition: qgsvectordataprovider.h:84
field
const QgsField & field
Definition: qgsfield.h:463
QgsVectorLayer::fields
QgsFields fields() const FINAL
Returns the list of fields of this layer.
Definition: qgsvectorlayer.cpp:3436
QgsVectorDataProvider::capabilities
virtual Q_INVOKABLE QgsVectorDataProvider::Capabilities capabilities() const
Returns flags containing the supported capabilities.
Definition: qgsvectordataprovider.cpp:208
QgsProcessingContext
Contains information about the context in which a processing algorithm is executed.
Definition: qgsprocessingcontext.h:46
qgsvectordataprovider.h
QgsMapLayer::id
QString id() const
Returns the layer's unique ID, which is used to access this layer from QgsProject.
Definition: qgsmaplayer.cpp:169
QgsProcessingParameterVectorLayer
A vector layer (with or without geometry) parameter for processing algorithms. Consider using the mor...
Definition: qgsprocessingparameters.h:2827
qgsvectorlayer.h
qgsalgorithmattributeindex.h
QgsProcessingAlgorithm::FlagNoThreading
@ FlagNoThreading
Algorithm is not thread safe and cannot be run in a background thread, e.g. for algorithms which mani...
Definition: qgsprocessingalgorithm.h:76
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:391
QgsFields::fieldOriginIndex
int fieldOriginIndex(int fieldIdx) const
Returns the field's origin index (its meaning is specific to each type of origin).
Definition: qgsfields.cpp:197
QgsVectorDataProvider
This is the base class for vector data providers.
Definition: qgsvectordataprovider.h:58
QgsProcessingAlgorithm::flags
virtual Flags flags() const
Returns the flags indicating how and when the algorithm operates and should be exposed to users.
Definition: qgsprocessingalgorithm.cpp:90
QgsFields::lookupField
int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
Definition: qgsfields.cpp:349
QgsProcessingException
Custom exception class for processing related exceptions.
Definition: qgsexception.h:82
QgsProcessingParameterField
A vector layer or feature source field parameter for processing algorithms.
Definition: qgsprocessingparameters.h:2940