QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
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
21#include "qgsvectorlayer.h"
22
24
25QString QgsSpatialIndexAlgorithm::name() const
26{
27 return QStringLiteral( "createspatialindex" );
28}
29
30QString QgsSpatialIndexAlgorithm::displayName() const
31{
32 return QObject::tr( "Create spatial index" );
33}
34
35QStringList QgsSpatialIndexAlgorithm::tags() const
36{
37 return QObject::tr( "table,spatial,geometry,index,create,vector" ).split( ',' );
38}
39
40QString QgsSpatialIndexAlgorithm::group() const
41{
42 return QObject::tr( "Vector general" );
43}
44
45QString QgsSpatialIndexAlgorithm::groupId() const
46{
47 return QStringLiteral( "vectorgeneral" );
48}
49
50Qgis::ProcessingAlgorithmFlags QgsSpatialIndexAlgorithm::flags() const
51{
53}
54
55
56QString QgsSpatialIndexAlgorithm::shortHelpString() const
57{
58 return QObject::tr( "Creates an index to speed up access to the features "
59 "in a layer based on their spatial location. Support "
60 "for spatial index creation is dependent on the layer's "
61 "data provider." );
62}
63
64QgsSpatialIndexAlgorithm *QgsSpatialIndexAlgorithm::createInstance() const
65{
66 return new QgsSpatialIndexAlgorithm();
67}
68
69void QgsSpatialIndexAlgorithm::initAlgorithm( const QVariantMap & )
70{
71 addParameter( new QgsProcessingParameterVectorLayer( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ) ) );
72
73 addOutput( new QgsProcessingOutputVectorLayer( QStringLiteral( "OUTPUT" ), QObject::tr( "Indexed layer" ) ) );
74}
75
76QVariantMap QgsSpatialIndexAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
77{
78 QgsVectorLayer *layer = parameterAsVectorLayer( parameters, QStringLiteral( "INPUT" ), context );
79
80 if ( !layer )
81 throw QgsProcessingException( QObject::tr( "Could not load source layer for %1." ).arg( QLatin1String( "INPUT" ) ) );
82
83 QgsVectorDataProvider *provider = layer->dataProvider();
84
86 {
87 if ( !provider->createSpatialIndex() )
88 {
89 feedback->pushInfo( QObject::tr( "Could not create spatial index" ) );
90 }
91 }
92 else
93 {
94 feedback->pushInfo( QObject::tr( "Layer's data provider does not support spatial indexes" ) );
95 }
96
97 QVariantMap outputs;
98 outputs.insert( QStringLiteral( "OUTPUT" ), layer->id() );
99 return outputs;
100}
101
@ CreateSpatialIndex
Allows creation of spatial index.
Definition qgis.h:506
QFlags< ProcessingAlgorithmFlag > ProcessingAlgorithmFlags
Flags indicating how and when an algorithm operates and should be exposed to users.
Definition qgis.h:3609
@ NoThreading
Algorithm is not thread safe and cannot be run in a background thread, e.g. for algorithms which mani...
Definition qgis.h:3588
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 (with or without geometry) parameter for processing algorithms.
Base class for vector data providers.
virtual bool createSpatialIndex()
Creates a spatial index on the datasource (if supported by the provider type).
virtual Q_INVOKABLE Qgis::VectorProviderCapabilities capabilities() const
Returns flags containing the supported capabilities.
Represents a vector layer which manages a vector based dataset.
QgsVectorDataProvider * dataProvider() final
Returns the layer's data provider, it may be nullptr.