QGIS API Documentation 4.3.0-Master (96c7fa699b4)
Loading...
Searching...
No Matches
qgsprocessingregistry.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsprocessingregistry.cpp
3 --------------------------
4 begin : December 2016
5 copyright : (C) 2016 by Nyall Dawson
6 email : nyall dot dawson 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
35#include "qgsvectorfilewriter.h"
36
37#include <QString>
38
39#include "moc_qgsprocessingregistry.cpp"
40
41using namespace Qt::StringLiterals;
42
44 : QObject( parent )
45 , mDefaultStyleRegistry( new QgsProcessingDefaultStyleRegistry( this ) )
46 , mLayerPostProcessor( std::make_unique< QgsProcessingLayerPostProcessor >() )
47 , mResultsHandler( std::make_unique< QgsProcessingResultsHandler >() )
48{
89 addParameterType( new QgsProcessingParameterTypeVectorTileWriterLayers() );
90 addParameterType( new QgsProcessingParameterTypeFieldMapping() );
91 addParameterType( new QgsProcessingParameterTypeAggregate() );
92 addParameterType( new QgsProcessingParameterTypeTinInputLayers() );
93 addParameterType( new QgsProcessingParameterTypeDxfLayers() );
94 addParameterType( new QgsProcessingParameterTypeMeshDatasetGroups() );
95 addParameterType( new QgsProcessingParameterTypeMeshDatasetTime() );
101 addParameterType( new QgsProcessingParameterTypeAlignRasterLayers() );
102 addParameterType( new QgsProcessingParameterTypeHeatmapPixelSize() );
103 addParameterType( new QgsProcessingParameterTypeReliefColors() );
104 addParameterType( new QgsProcessingParameterTypeInterpolationSource() );
105 addParameterType( new QgsProcessingParameterTypeInterpolationPixelSize() );
106 addParameterType( new QgsProcessingParameterTypeTileExtentMaxZoomList() );
107}
108
110{
111 const auto constMProviders = mProviders;
112 for ( QgsProcessingProvider *p : constMProviders )
113 {
114 removeProvider( p );
115 }
116
117 const auto parameterTypes = mParameterTypes.values();
118
120 {
121 removeParameterType( type );
122 }
123}
124
126{
127 if ( !provider )
128 return false;
129
130 if ( providerById( provider->id() ) )
131 {
132 QgsLogger::warning( u"Duplicate provider %1 registered"_s.arg( provider->id() ) );
133 delete provider;
134 return false;
135 }
136
137 if ( !provider->load() )
138 {
139 QgsLogger::warning( u"Provider %1 cannot load"_s.arg( provider->id() ) );
140 delete provider;
141 return false;
142 }
143
144 provider->setParent( this );
145 mProviders[provider->id()] = provider;
146
147 mCachedInformation.clear();
148 connect( provider, &QgsProcessingProvider::algorithmsLoaded, this, [this] { mCachedInformation.clear(); } );
149
150 emit providerAdded( provider->id() );
151 return true;
152}
153
155{
156 if ( !provider )
157 return false;
158
159 const QString id = provider->id();
160
161 if ( !mProviders.contains( id ) )
162 return false;
163
164 provider->unload();
165
166 delete mProviders.take( id );
167
168 mCachedInformation.clear();
169
170 emit providerRemoved( id );
171 return true;
172}
173
174bool QgsProcessingRegistry::removeProvider( const QString &providerId )
175{
176 QgsProcessingProvider *p = providerById( providerId );
177 return removeProvider( p );
178}
179
181{
182 auto it = mProviders.constFind( id );
183 if ( it != mProviders.constEnd() )
184 return it.value();
185
186 // transparently map old references to "grass7" provider to "grass" provider
187 if ( id.compare( "grass7"_L1, Qt::CaseInsensitive ) == 0 )
188 return providerById( u"grass"_s );
189
190 return nullptr;
191}
192
193QList< const QgsProcessingAlgorithm * > QgsProcessingRegistry::algorithms() const
194{
195 QList< const QgsProcessingAlgorithm * > algs;
196 QMap<QString, QgsProcessingProvider *>::const_iterator it = mProviders.constBegin();
197 for ( ; it != mProviders.constEnd(); ++it )
198 {
199 algs.append( it.value()->algorithms() );
200 }
201 return algs;
202}
203
205{
206 const auto it = mCachedInformation.constFind( id );
207 if ( it != mCachedInformation.constEnd() )
208 return *it;
209
212 {
213 info.displayName = algorithm->displayName();
214 info.icon = algorithm->icon();
215 }
216 mCachedInformation.insert( id, info );
217 return info;
218}
219
221{
222 if ( constId.isEmpty() )
223 return nullptr;
224
225 // allow mapping of algorithm via registered algorithm aliases
226 const QString id = mAlgorithmAliases.value( constId, constId );
227
228 // try to match just the one target provider, if we can determine it from the id easily
229 static thread_local QRegularExpression reSplitProviderId( u"^(.*?):(.*)$"_s );
230 const QRegularExpressionMatch match = reSplitProviderId.match( id );
231 if ( match.hasMatch() )
232 {
233 if ( QgsProcessingProvider *provider = providerById( match.captured( 1 ) ) )
234 {
235 if ( const QgsProcessingAlgorithm *algorithm = provider->algorithm( match.captured( 2 ) ) )
236 return algorithm;
237 }
238
239 // try mapping 'qgis' algs to 'native' algs - this allows us to freely move algorithms
240 // from the python 'qgis' provider to the c++ 'native' provider without breaking API
241 // or existing models
242 if ( match.captured( 1 ) == "qgis"_L1 )
243 {
244 const QString algorithmName = id.mid( 5 );
245 if ( QgsProcessingProvider *provider = mProviders.value( u"native"_s ) )
246 {
247 if ( const QgsProcessingAlgorithm *algorithm = provider->algorithm( algorithmName ) )
248 return algorithm;
249 }
250 }
251 }
252
253 // slow: iterate through ALL providers to find a match
254 QMap<QString, QgsProcessingProvider *>::const_iterator it = mProviders.constBegin();
255 for ( ; it != mProviders.constEnd(); ++it )
256 {
257 const QList< const QgsProcessingAlgorithm * > algorithms = it.value()->algorithms();
258 for ( const QgsProcessingAlgorithm *alg : algorithms )
259 if ( alg->id() == id )
260 return alg;
261 }
262
263 return nullptr;
264}
265
266QgsProcessingAlgorithm *QgsProcessingRegistry::createAlgorithmById( const QString &id, const QVariantMap &configuration ) const
267{
268 const QgsProcessingAlgorithm *alg = algorithmById( id );
269 if ( !alg )
270 return nullptr;
271
272 std::unique_ptr< QgsProcessingAlgorithm > creation( alg->create( configuration ) );
273 return creation.release();
274}
275
276void QgsProcessingRegistry::addAlgorithmAlias( const QString &aliasId, const QString &actualId )
277{
278 mAlgorithmAliases.insert( aliasId, actualId );
279}
280
282{
283 if ( !mParameterTypes.contains( type->id() ) )
284 {
285 mParameterTypes.insert( type->id(), type );
286 emit parameterTypeAdded( type );
287 return true;
288 }
289 else
290 {
291 QgsLogger::warning( u"Duplicate parameter type %1 (\"%2\") registered"_s.arg( type->id(), type->name() ) );
292
293 if ( mParameterTypes.value( type->id() ) != type )
294 delete type;
295
296 return false;
297 }
298}
299
301{
302 mParameterTypes.remove( type->id() );
303 emit parameterTypeRemoved( type );
304 delete type;
305}
306
308{
309 return mParameterTypes.value( id );
310}
311
312QList<QgsProcessingParameterType *> QgsProcessingRegistry::parameterTypes() const
313{
314 return mParameterTypes.values();
315}
316
318{
319 const QgsProcessingParameterType *paramTargetType = parameterType( targetDefinition->type() );
320 if ( paramTargetType )
321 return paramTargetType->acceptedParameterTypes().contains( candidateDefinition->type() );
322 return false;
323}
324
326{
327 const QgsProcessingParameterType *paramTargetType = parameterType( targetDefinition->type() );
328
329 if ( paramTargetType )
330 return paramTargetType->acceptedOutputTypes().contains( candidateOutput->type() );
331 return false;
332}
333
335{
336 return mDefaultStyleRegistry;
337}
338
339void QgsProcessingRegistry::setLayerPostProcessor( std::unique_ptr< QgsProcessingLayerPostProcessor > processor )
340{
341 mLayerPostProcessor = std::move( processor );
342}
343
345{
346 return mLayerPostProcessor.get();
347}
348
349void QgsProcessingRegistry::setResultsHandler( std::unique_ptr<QgsProcessingResultsHandler> handler )
350{
351 mResultsHandler = std::move( handler );
352}
353
355{
356 return mResultsHandler->handleAlgorithmResults( algorithm, context, parameters, feedback );
357}
static void warning(const QString &msg)
Goes to qWarning.
Contains basic properties for a Processing algorithm.
QString displayName
Algorithm display name.
Abstract base class for processing algorithms.
QgsProcessingAlgorithm * create(const QVariantMap &configuration=QVariantMap()) const
Creates a copy of the algorithm, ready for execution.
Contains information about the context in which a processing algorithm is executed.
A registry for default styles to apply to layers generated by Processing.
Base class for providing feedback from a processing algorithm.
A post-processor for result layers created by Processing algorithms.
Base class for the definition of processing outputs.
virtual QString type() const =0
Unique output type name.
Base class for the definition of processing parameters.
virtual QString type() const =0
Unique parameter type name.
An annotation layer parameter for processing algorithms.
An area parameter for processing algorithms.
An authentication configuration parameter for processing algorithms.
A raster band parameter for Processing algorithms.
A boolean parameter for processing algorithms.
A color parameter for Processing algorithms.
A coordinate operation parameter for Processing algorithms.
A crs parameter for processing algorithms.
A database schema name parameter for processing algorithms.
A database table name parameter for processing algorithms.
A datetime parameter for processing algorithms.
A distance parameter for processing algorithms.
A duration parameter for processing algorithms.
An enum based parameter for processing algorithms, allowing for selection from predefined values.
An expression parameter for processing algorithms.
A rectangular map extent parameter for processing algorithms.
A feature sink parameter 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 generic file based destination parameter, for specifying the destination path for a file (non-map l...
An input file or folder parameter for processing algorithms.
A folder destination parameter, for specifying the destination path for a folder created by the algor...
A geometry parameter for processing algorithms.
A print layout item parameter for Processing algorithms.
A print layout parameter for Processing algorithms.
A generic map layer parameter for processing algorithms.
A map theme parameter for Processing algorithms.
A table (matrix) parameter for processing algorithms.
A mesh layer parameter for processing algorithms.
A parameter for processing algorithms which accepts multiple map layers.
A numeric parameter for processing algorithms.
A point cloud layer attribute parameter for Processing algorithms.
A pointcloud layer destination parameter, for specifying the destination path for a point cloud layer...
A point cloud layer parameter for processing algorithms.
A point parameter for processing algorithms.
A provider connection name parameter for processing algorithms.
A numeric range parameter for processing algorithms.
A raster layer destination parameter, for specifying the destination path for a raster layer created ...
A raster layer parameter for processing algorithms.
A scale parameter for processing algorithms.
A string parameter for processing algorithms.
A vector layer destination parameter, for specifying the destination path for a vector layer created ...
A vector layer parameter for processing algorithms.
A vector tile layer destination parameter, for specifying the destination path for a vector tile laye...
A volume parameter for processing algorithms.
Makes metadata of processing parameters available.
virtual QString name() const =0
A human readable and translatable short name for this parameter type.
virtual QStringList acceptedOutputTypes() const =0
Returns a list of compatible Processing output types for inputs for this parameter type.
virtual QString id() const =0
A static id for this type which will be used for storing this parameter type.
virtual QStringList acceptedParameterTypes() const =0
Returns a list of compatible Processing parameter types for inputs for this parameter type.
Abstract base class for processing providers.
void algorithmsLoaded()
Emitted when the provider has loaded (or refreshed) its list of available algorithms.
virtual void unload()
Unloads the provider.
virtual QString id() const =0
Returns the unique provider id, used for identifying the provider.
virtual bool load()
Loads the provider.
void removeParameterType(QgsProcessingParameterType *type)
Unregister a custom parameter type from processing.
void parameterTypeAdded(QgsProcessingParameterType *type)
Emitted when a new parameter type has been added to the registry.
QgsProcessingAlgorithm * createAlgorithmById(const QString &id, const QVariantMap &configuration=QVariantMap()) const
Creates a new instance of an algorithm by its ID.
void setLayerPostProcessor(std::unique_ptr< QgsProcessingLayerPostProcessor > processor)
Sets the post-processor to use for all map layers generated by Processing algorithms.
bool handleAlgorithmResults(const QgsProcessingAlgorithm *algorithm, QgsProcessingContext &context, const QVariantMap &parameters, QgsProcessingFeedback *feedback=nullptr)
Handles the results from running an algorithm.
void parameterTypeRemoved(QgsProcessingParameterType *type)
Emitted when a parameter type has been removed from the registry and is about to be deleted.
QgsProcessingAlgorithmInformation algorithmInformation(const QString &id) const
Returns basic algorithm information for the algorithm with matching ID.
QList< const QgsProcessingAlgorithm * > algorithms() const
Returns a list of all available algorithms from registered providers.
QgsProcessingLayerPostProcessor * layerPostProcessor()
Returns the post-processor to use for all map layers generated by Processing algorithms.
QgsProcessingParameterType * parameterType(const QString &id) const
Returns the parameter type registered for id.
bool removeProvider(QgsProcessingProvider *provider)
Removes a provider implementation from the registry (the provider object is deleted).
QgsProcessingDefaultStyleRegistry * defaultStyleRegistry() const
Returns the default style registry, responsible for styling layers generated by Processing tools.
bool addParameterType(QgsProcessingParameterType *type)
Register a new parameter type for processing.
QgsProcessingRegistry(QObject *parent=nullptr)
Constructor for QgsProcessingRegistry.
void setResultsHandler(std::unique_ptr< QgsProcessingResultsHandler > handler)
Sets the results handler to use for results generated by Processing algorithms.
const QgsProcessingAlgorithm * algorithmById(const QString &id) const
Finds an algorithm by its ID.
QgsProcessingProvider * providerById(const QString &id) const
Returns a matching provider by provider ID.
bool isCompatibleDefinition(const QgsProcessingParameterDefinition *targetDefinition, const QgsProcessingParameterDefinition *candidateDefinition)
Returns true if the targetDefinition accepts candidateDefinition as compatible Processing parameter t...
void providerAdded(const QString &id)
Emitted when a provider has been added to the registry.
QList< QgsProcessingParameterType * > parameterTypes() const
Returns a list with all known parameter types.
bool addProvider(QgsProcessingProvider *provider)
Add a processing provider to the registry.
bool isCompatibleOutput(const QgsProcessingParameterDefinition *targetDefinition, const QgsProcessingOutputDefinition *candidateOutput)
Returns true if the targetDefinition accepts candidateOutput as compatible Processing output types fo...
void providerRemoved(const QString &id)
Emitted when a provider is removed from the registry.
void addAlgorithmAlias(const QString &aliasId, const QString &actualId)
Adds a new alias to an existing algorithm.
Handles results output by Processing algorithms.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into allowing algorithms to be written in pure substantial changes are required in order to port existing x Processing algorithms for QGIS x The most significant changes are outlined not GeoAlgorithm For algorithms which operate on features one by consider subclassing the QgsProcessingFeatureBasedAlgorithm class This class allows much of the boilerplate code for looping over features from a vector layer to be bypassed and instead requires implementation of a processFeature method Ensure that your algorithm(or algorithm 's parent class) implements the new pure virtual createInstance(self) call
#define SIP_TRANSFERTHIS
Definition qgis_sip.h:52