QGIS API Documentation  3.20.0-Odense (decaadbb31)
qgsalgorithmspatialindex.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsalgorithmspatialindex.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 QgsSpatialIndexAlgorithm::name() const
25 {
26  return QStringLiteral( "createspatialindex" );
27 }
28 
29 QString QgsSpatialIndexAlgorithm::displayName() const
30 {
31  return QObject::tr( "Create spatial index" );
32 }
33 
34 QStringList QgsSpatialIndexAlgorithm::tags() const
35 {
36  return QObject::tr( "table,spatial,geometry,index,create,vector" ).split( ',' );
37 }
38 
39 QString QgsSpatialIndexAlgorithm::group() const
40 {
41  return QObject::tr( "Vector general" );
42 }
43 
44 QString QgsSpatialIndexAlgorithm::groupId() const
45 {
46  return QStringLiteral( "vectorgeneral" );
47 }
48 
49 QgsProcessingAlgorithm::Flags QgsSpatialIndexAlgorithm::flags() const
50 {
52 }
53 
54 
55 QString QgsSpatialIndexAlgorithm::shortHelpString() const
56 {
57  return QObject::tr( "Creates an index to speed up access to the features "
58  "in a layer based on their spatial location. Support "
59  "for spatial index creation is dependent on the layer's "
60  "data provider." );
61 }
62 
63 QgsSpatialIndexAlgorithm *QgsSpatialIndexAlgorithm::createInstance() const
64 {
65  return new QgsSpatialIndexAlgorithm();
66 }
67 
68 void QgsSpatialIndexAlgorithm::initAlgorithm( const QVariantMap & )
69 {
70  addParameter( new QgsProcessingParameterVectorLayer( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ) ) );
71 
72  addOutput( new QgsProcessingOutputVectorLayer( QStringLiteral( "OUTPUT" ), QObject::tr( "Indexed layer" ) ) );
73 }
74 
75 QVariantMap QgsSpatialIndexAlgorithm::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  QgsVectorDataProvider *provider = layer->dataProvider();
83 
85  {
86  if ( !provider->createSpatialIndex() )
87  {
88  feedback->pushInfo( QObject::tr( "Could not create spatial index" ) );
89  }
90  }
91  else
92  {
93  feedback->pushInfo( QObject::tr( "Layer's data provider does not support spatial indexes" ) );
94  }
95 
96  QVariantMap outputs;
97  outputs.insert( QStringLiteral( "OUTPUT" ), layer->id() );
98  return outputs;
99 }
100 
QString id() const
Returns the layer's unique ID, which is used to access this layer from QgsProject.
virtual Flags flags() const
Returns the flags indicating how and when the algorithm operates and should be exposed to users.
@ FlagNoThreading
Algorithm is not thread safe and cannot be run in a background thread, e.g. for algorithms which mani...
Contains information about the context in which a processing algorithm is executed.
Custom exception class for processing related exceptions.
Definition: qgsexception.h:83
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 (with or without geometry) parameter for processing algorithms.
This is the base class for vector data providers.
@ CreateSpatialIndex
Allows creation of spatial index.
virtual bool createSpatialIndex()
Creates a spatial index on the datasource (if supported by the provider type).
virtual Q_INVOKABLE QgsVectorDataProvider::Capabilities capabilities() const
Returns flags containing the supported capabilities.
Represents a vector layer which manages a vector based data sets.
QgsVectorDataProvider * dataProvider() FINAL
Returns the layer's data provider, it may be nullptr.