QGIS API Documentation 3.39.0-Master (699c004a5df)
Loading...
Searching...
No Matches
qgsprocessingprovider.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsprocessingprovider.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#include "qgsapplication.h"
20#include "qgsvectorfilewriter.h"
21#include "qgsrasterfilewriter.h"
22#include "qgsmessagelog.h"
23
25 : QObject( parent )
26{}
27
28
30{
31 qDeleteAll( mAlgorithms );
32}
33
35{
36 return QgsApplication::getThemeIcon( "/processingAlgorithm.svg" );
37}
38
40{
41 return QgsApplication::iconPath( QStringLiteral( "processingAlgorithm.svg" ) );
42}
43
48
50{
51 return QString();
52}
53
55{
56 return name();
57}
58
60{
61 return QString();
62}
63
68
70{
71 return QStringList();
72}
73
78
80{
81 qDeleteAll( mAlgorithms );
82 mAlgorithms.clear();
83 if ( isActive() )
84 {
86 emit algorithmsLoaded();
87 }
88}
89
90QList<const QgsProcessingAlgorithm *> QgsProcessingProvider::algorithms() const
91{
92 return mAlgorithms.values();
93}
94
96{
97 return mAlgorithms.value( name );
98}
99
101{
102 if ( !algorithm )
103 return false;
104
105 if ( mAlgorithms.contains( algorithm->name() ) )
106 {
107 QgsMessageLog::logMessage( tr( "Duplicate algorithm name %1 for provider %2" ).arg( algorithm->name(), id() ), QObject::tr( "Processing" ) );
108 return false;
109 }
110
111 // init the algorithm - this allows direct querying of the algorithm's parameters
112 // and outputs from the provider's copy
113 algorithm->initAlgorithm( QVariantMap() );
114
115 algorithm->setProvider( this );
116 mAlgorithms.insert( algorithm->name(), algorithm );
117 return true;
118}
119
124
129
130bool QgsProcessingProvider::isSupportedOutputValue( const QVariant &outputValue, const QgsProcessingDestinationParameter *parameter, QgsProcessingContext &context, QString &error ) const
131{
132 error.clear();
133 QString outputPath = QgsProcessingParameters::parameterAsOutputLayer( parameter, outputValue, context, true ).trimmed();
134
135 if ( outputPath.isEmpty() )
136 {
138 {
139 return true;
140 }
141 else
142 {
143 error = tr( "Missing parameter value %1" ).arg( parameter->description() );
144 return false;
145 }
146 }
147
150 {
151 if ( outputPath.startsWith( QLatin1String( "memory:" ) ) )
152 {
154 {
155 error = tr( "This algorithm only supports disk-based outputs" );
156 return false;
157 }
158 return true;
159 }
160
161 QString providerKey;
162 QString uri;
163 QString layerName;
164 QMap<QString, QVariant> options;
165 bool useWriter = false;
166 QString format;
167 QString extension;
168 QgsProcessingUtils::parseDestinationString( outputPath, providerKey, uri, layerName, format, options, useWriter, extension );
169
170 if ( providerKey != QLatin1String( "ogr" ) )
171 {
173 {
174 error = tr( "This algorithm only supports disk-based outputs" );
175 return false;
176 }
177 return true;
178 }
179
180 if ( !supportedOutputVectorLayerExtensions().contains( extension, Qt::CaseInsensitive ) )
181 {
182 error = tr( "“.%1” files are not supported as outputs for this algorithm" ).arg( extension );
183 return false;
184 }
185 return true;
186 }
187 else if ( parameter->type() == QgsProcessingParameterRasterDestination::typeName() )
188 {
189 const QFileInfo fi( outputPath );
190 const QString extension = fi.suffix();
191 if ( !supportedOutputRasterLayerExtensions().contains( extension, Qt::CaseInsensitive ) )
192 {
193 error = tr( "“.%1” files are not supported as outputs for this algorithm" ).arg( extension );
194 return false;
195 }
196 return true;
197 }
199 {
200 const QFileInfo fi( outputPath );
201 const QString extension = fi.completeSuffix();
202 if ( !supportedOutputPointCloudLayerExtensions().contains( extension, Qt::CaseInsensitive ) )
203 {
204 error = tr( "“.%1” files are not supported as outputs for this algorithm" ).arg( extension );
205 return false;
206 }
207 return true;
208 }
210 {
211 const QFileInfo fi( outputPath );
212 const QString extension = fi.completeSuffix();
213 if ( !supportedOutputVectorTileLayerExtensions().contains( extension, Qt::CaseInsensitive ) )
214 {
215 error = tr( "“.%1” files are not supported as outputs for this algorithm" ).arg( extension );
216 return false;
217 }
218 return true;
219 }
220 else
221 {
222 return true;
223 }
224}
225
227{
228 const QString userDefault = QgsProcessingUtils::defaultVectorExtension();
229
230 const QStringList supportedExtensions = supportedOutputVectorLayerExtensions();
231 if ( supportedExtensions.contains( userDefault, Qt::CaseInsensitive ) )
232 {
233 // user set default is supported by provider, use that
234 return userDefault;
235 }
236 else if ( !supportedExtensions.empty() )
237 {
238 return supportedExtensions.at( 0 );
239 }
240 else
241 {
242 // who knows? provider says it has no file support at all...
243 // let's say shp. even MapInfo supports shapefiles.
244 return hasGeometry ? QStringLiteral( "shp" ) : QStringLiteral( "dbf" );
245 }
246}
247
249{
250 const QString userDefault = QgsProcessingUtils::defaultRasterExtension();
251
252 const QStringList supportedExtensions = supportedOutputRasterLayerExtensions();
253 if ( supportedExtensions.contains( userDefault, Qt::CaseInsensitive ) )
254 {
255 // user set default is supported by provider, use that
256 return userDefault;
257 }
258 else if ( !supportedExtensions.empty() )
259 {
260 return supportedExtensions.at( 0 );
261 }
262 else
263 {
264 // who knows? provider says it has no file support at all...
265 return QStringLiteral( "tif" );
266 }
267}
268
270{
271 const QString userDefault = QgsProcessingUtils::defaultPointCloudExtension();
272
273 const QStringList supportedExtensions = supportedOutputPointCloudLayerExtensions();
274 if ( supportedExtensions.contains( userDefault, Qt::CaseInsensitive ) )
275 {
276 // user set default is supported by provider, use that
277 return userDefault;
278 }
279 else if ( !supportedExtensions.empty() )
280 {
281 return supportedExtensions.at( 0 );
282 }
283 else
284 {
285 // who knows? provider says it has no file support at all...
286 return QStringLiteral( "las" );
287 }
288}
289
291{
292 const QString userDefault = QgsProcessingUtils::defaultVectorTileExtension();
293
294 const QStringList supportedExtensions = supportedOutputVectorTileLayerExtensions();
295 if ( supportedExtensions.contains( userDefault, Qt::CaseInsensitive ) )
296 {
297 // user set default is supported by provider, use that
298 return userDefault;
299 }
300 else if ( !supportedExtensions.empty() )
301 {
302 return supportedExtensions.at( 0 );
303 }
304 else
305 {
306 // who knows? provider says it has no file support at all...
307 return QStringLiteral( "mbtiles" );
308 }
309}
310
312{
313 return true;
314}
QFlags< ProcessingProviderFlag > ProcessingProviderFlags
Flags indicating how and when an processing provider operates and should be exposed to users.
Definition qgis.h:3281
@ Optional
Parameter is optional.
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
static QString iconPath(const QString &iconFile)
Returns path to the desired icon file.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
Abstract base class for processing algorithms.
void setProvider(QgsProcessingProvider *provider)
Associates this algorithm with its provider.
virtual void initAlgorithm(const QVariantMap &configuration=QVariantMap())=0
Initializes the algorithm using the specified configuration.
virtual QString name() const =0
Returns the algorithm name, used for identifying the algorithm.
Contains information about the context in which a processing algorithm is executed.
Base class for all parameter definitions which represent file or layer destinations,...
QString description() const
Returns the description for the parameter.
virtual QString type() const =0
Unique parameter type name.
Qgis::ProcessingParameterFlags flags() const
Returns any flags associated with the parameter.
static QString typeName()
Returns the type name for the parameter class.
static QString typeName()
Returns the type name for the parameter class.
static QString typeName()
Returns the type name for the parameter class.
static QString typeName()
Returns the type name for the parameter class.
static QString typeName()
Returns the type name for the parameter class.
static QString parameterAsOutputLayer(const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context)
Evaluates the parameter with matching definition to a output layer destination.
virtual QIcon icon() const
Returns an icon for the provider.
virtual QString defaultVectorFileExtension(bool hasGeometry=true) const
Returns the default file extension to use for vector outputs created by the provider.
virtual QString helpId() const
Returns the provider help id string, used for creating QgsHelp urls for algorithms belong to this pro...
virtual bool isSupportedOutputValue(const QVariant &outputValue, const QgsProcessingDestinationParameter *parameter, QgsProcessingContext &context, QString &error) const
Returns true if the specified outputValue is of a supported file format for the given destination par...
virtual QStringList supportedOutputTableExtensions() const
Returns a list of the table (geometry-less vector layers) file extensions supported by this provider.
const QgsProcessingAlgorithm * algorithm(const QString &name) const
Returns the matching algorithm by name, or nullptr if no matching algorithm is contained by this prov...
virtual Qgis::ProcessingProviderFlags flags() const
Returns the flags indicating how and when the provider operates and should be exposed to users.
virtual QString versionInfo() const
Returns a version information string for the provider, or an empty string if this is not applicable (...
virtual QStringList supportedOutputVectorLayerExtensions() const
Returns a list of the vector format file extensions supported by this provider.
virtual QString name() const =0
Returns the provider name, which is used to describe the provider within the GUI.
virtual QString defaultVectorTileFileExtension() const
Returns the default file extension to use for vector tile outputs created by the provider.
virtual QStringList supportedOutputRasterLayerExtensions() const
Returns a list of the raster format file extensions supported by this provider.
virtual QStringList supportedOutputVectorTileLayerExtensions() const
Returns a list of the vector tile format file extensions supported by this provider.
QgsProcessingProvider(QObject *parent=nullptr)
Constructor for QgsProcessingProvider.
virtual bool isActive() const
Returns true if the provider is active and able to run algorithms.
virtual QStringList supportedOutputPointCloudLayerExtensions() const
Returns a list of the point cloud format file extensions supported by this provider.
virtual QString defaultRasterFileExtension() const
Returns the default file extension to use for raster outputs created by the provider.
void algorithmsLoaded()
Emitted when the provider has loaded (or refreshed) its list of available algorithms.
virtual QString svgIconPath() const
Returns a path to an SVG version of the provider's icon.
virtual void loadAlgorithms()=0
Loads all algorithms belonging to this provider.
void refreshAlgorithms()
Refreshes the algorithms available from the provider, causing it to re-populate with all associated a...
virtual QString longName() const
Returns the longer version of the provider name, which can include extra details such as version numb...
virtual bool supportsNonFileBasedOutput() const
Returns true if the provider supports non-file based outputs (such as memory layers or direct databas...
virtual QString defaultPointCloudFileExtension() const
Returns the default file extension to use for point cloud outputs created by the provider.
bool addAlgorithm(QgsProcessingAlgorithm *algorithm)
Adds an algorithm to the provider.
QList< const QgsProcessingAlgorithm * > algorithms() const
Returns a list of algorithms supplied by this provider.
static QString defaultVectorExtension()
Returns the default vector extension to use, in the absence of all other constraints (e....
static QString defaultRasterExtension()
Returns the default raster extension to use, in the absence of all other constraints (e....
static QString defaultVectorTileExtension()
Returns the default vector tile extension to use, in the absence of all other constraints (e....
static QString defaultPointCloudExtension()
Returns the default point cloud extension to use, in the absence of all other constraints (e....
static QStringList supportedFormatExtensions(RasterFormatOptions options=SortRecommended)
Returns a list of file extensions for supported formats.
static QStringList supportedFormatExtensions(VectorFormatOptions options=SortRecommended)
Returns a list of file extensions for supported formats, e.g "shp", "gpkg".
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:53