QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
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
21#include "qgsvectorlayer.h"
22
24
25QString QgsAttributeIndexAlgorithm::name() const
26{
27 return QStringLiteral( "createattributeindex" );
28}
29
30QString QgsAttributeIndexAlgorithm::displayName() const
31{
32 return QObject::tr( "Create attribute index" );
33}
34
35QStringList QgsAttributeIndexAlgorithm::tags() const
36{
37 return QObject::tr( "table,attribute,index,create,vector" ).split( ',' );
38}
39
40QString QgsAttributeIndexAlgorithm::group() const
41{
42 return QObject::tr( "Vector general" );
43}
44
45QString QgsAttributeIndexAlgorithm::groupId() const
46{
47 return QStringLiteral( "vectorgeneral" );
48}
49
50Qgis::ProcessingAlgorithmFlags QgsAttributeIndexAlgorithm::flags() const
51{
53}
54
55
56QString QgsAttributeIndexAlgorithm::shortHelpString() const
57{
58 return QObject::tr( "This algorithm creates an index to speed up queries made against "
59 "a field in a table. Support for index creation is "
60 "dependent on the layer's data provider and the field type." );
61}
62
63QString QgsAttributeIndexAlgorithm::shortDescription() const
64{
65 return QObject::tr( "Creates an index to speed up queries made against a field in a table." );
66}
67
68QgsAttributeIndexAlgorithm *QgsAttributeIndexAlgorithm::createInstance() const
69{
70 return new QgsAttributeIndexAlgorithm();
71}
72
73void QgsAttributeIndexAlgorithm::initAlgorithm( const QVariantMap & )
74{
75 addParameter( new QgsProcessingParameterVectorLayer( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ), QList<int> { static_cast<int>( Qgis::ProcessingSourceType::Vector ) } ) );
76 addParameter( new QgsProcessingParameterField( QStringLiteral( "FIELD" ), QObject::tr( "Attribute to index" ), QVariant(), QStringLiteral( "INPUT" ) ) );
77
78 addOutput( new QgsProcessingOutputVectorLayer( QStringLiteral( "OUTPUT" ), QObject::tr( "Indexed layer" ) ) );
79}
80
81QVariantMap QgsAttributeIndexAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
82{
83 QgsVectorLayer *layer = parameterAsVectorLayer( parameters, QStringLiteral( "INPUT" ), context );
84
85 if ( !layer )
86 throw QgsProcessingException( QObject::tr( "Could not load source layer for %1." ).arg( QLatin1String( "INPUT" ) ) );
87
88 const QString field = parameterAsString( parameters, QStringLiteral( "FIELD" ), context );
89
90 QgsVectorDataProvider *provider = layer->dataProvider();
91
92 const int fieldIndex = layer->fields().lookupField( field );
93 if ( fieldIndex < 0 || layer->fields().fieldOrigin( fieldIndex ) != Qgis::FieldOrigin::Provider )
94 {
95 feedback->pushInfo( QObject::tr( "Can not create attribute index on %1" ).arg( field ) );
96 }
97 else
98 {
99 const int providerIndex = layer->fields().fieldOriginIndex( fieldIndex );
101 {
102 if ( !provider->createAttributeIndex( providerIndex ) )
103 {
104 feedback->pushInfo( QObject::tr( "Could not create attribute index" ) );
105 }
106 }
107 else
108 {
109 feedback->pushInfo( QObject::tr( "Layer's data provider does not support creating attribute indexes" ) );
110 }
111 }
112
113 QVariantMap outputs;
114 outputs.insert( QStringLiteral( "OUTPUT" ), layer->id() );
115 return outputs;
116}
117
@ Vector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
Definition qgis.h:3539
@ CreateAttributeIndex
Can create indexes on provider's fields.
Definition qgis.h:510
QFlags< ProcessingAlgorithmFlag > ProcessingAlgorithmFlags
Flags indicating how and when an algorithm operates and should be exposed to users.
Definition qgis.h:3609
@ Provider
Field originates from the underlying data provider of the vector layer.
Definition qgis.h:1706
@ NoThreading
Algorithm is not thread safe and cannot be run in a background thread, e.g. for algorithms which mani...
Definition qgis.h:3588
int fieldOriginIndex(int fieldIdx) const
Returns the field's origin index (its meaning is specific to each type of origin).
Q_INVOKABLE int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
QString id
Definition qgsmaplayer.h:83
virtual Qgis::ProcessingAlgorithmFlags flags() const
Returns the flags indicating how and when the algorithm operates and should be exposed to users.
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.
virtual void pushInfo(const QString &info)
Pushes a general informational message from the algorithm.
A vector layer output for processing algorithms.
A vector layer or feature source field parameter for processing algorithms.
A vector layer (with or without geometry) parameter for processing algorithms.
Base class for vector data providers.
virtual Q_INVOKABLE Qgis::VectorProviderCapabilities capabilities() const
Returns flags containing the supported capabilities.
virtual bool createAttributeIndex(int field)
Create an attribute index on the datasource.
Represents a vector layer which manages a vector based dataset.
QgsVectorDataProvider * dataProvider() final
Returns the layer's data provider, it may be nullptr.