QGIS API Documentation 3.41.0-Master (af5edcb665c)
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 "moc_qgsprocessingprovider.cpp"
20#include "qgsapplication.h"
21#include "qgsvectorfilewriter.h"
22#include "qgsrasterfilewriter.h"
23#include "qgsmessagelog.h"
24
26 : QObject( parent )
27{}
28
29
31{
32 qDeleteAll( mAlgorithms );
33}
34
36{
37 return QgsApplication::getThemeIcon( "/processingAlgorithm.svg" );
38}
39
41{
42 return QgsApplication::iconPath( QStringLiteral( "processingAlgorithm.svg" ) );
43}
44
49
51{
52 return QString();
53}
54
56{
57 return name();
58}
59
61{
62 return QString();
63}
64
69
71{
72 return QStringList();
73}
74
79
81{
82 qDeleteAll( mAlgorithms );
83 mAlgorithms.clear();
84 if ( isActive() )
85 {
87 emit algorithmsLoaded();
88 }
89}
90
91QList<const QgsProcessingAlgorithm *> QgsProcessingProvider::algorithms() const
92{
93 return mAlgorithms.values();
94}
95
97{
98 return mAlgorithms.value( name );
99}
100
102{
103 if ( !algorithm )
104 return false;
105
106 if ( mAlgorithms.contains( algorithm->name() ) )
107 {
108 QgsMessageLog::logMessage( tr( "Duplicate algorithm name %1 for provider %2" ).arg( algorithm->name(), id() ), QObject::tr( "Processing" ) );
109 return false;
110 }
111
112 // init the algorithm - this allows direct querying of the algorithm's parameters
113 // and outputs from the provider's copy
114 algorithm->initAlgorithm( QVariantMap() );
115
116 algorithm->setProvider( this );
117 mAlgorithms.insert( algorithm->name(), algorithm );
118 return true;
119}
120
125
130
131bool QgsProcessingProvider::isSupportedOutputValue( const QVariant &outputValue, const QgsProcessingDestinationParameter *parameter, QgsProcessingContext &context, QString &error ) const
132{
133 error.clear();
134 QString outputPath = QgsProcessingParameters::parameterAsOutputLayer( parameter, outputValue, context, true ).trimmed();
135
136 if ( outputPath.isEmpty() )
137 {
139 {
140 return true;
141 }
142 else
143 {
144 error = tr( "Missing parameter value %1" ).arg( parameter->description() );
145 return false;
146 }
147 }
148
151 {
152 if ( outputPath.startsWith( QLatin1String( "memory:" ) ) )
153 {
155 {
156 error = tr( "This algorithm only supports disk-based outputs" );
157 return false;
158 }
159 return true;
160 }
161
162 QString providerKey;
163 QString uri;
164 QString layerName;
165 QMap<QString, QVariant> options;
166 bool useWriter = false;
167 QString format;
168 QString extension;
169 QgsProcessingUtils::parseDestinationString( outputPath, providerKey, uri, layerName, format, options, useWriter, extension );
170
171 if ( providerKey != QLatin1String( "ogr" ) )
172 {
174 {
175 error = tr( "This algorithm only supports disk-based outputs" );
176 return false;
177 }
178 return true;
179 }
180
181 if ( !supportedOutputVectorLayerExtensions().contains( extension, Qt::CaseInsensitive ) )
182 {
183 error = tr( "“.%1” files are not supported as outputs for this algorithm" ).arg( extension );
184 return false;
185 }
186 return true;
187 }
188 else if ( parameter->type() == QgsProcessingParameterRasterDestination::typeName() )
189 {
190 const QFileInfo fi( outputPath );
191 const QString extension = fi.suffix();
192 if ( !supportedOutputRasterLayerExtensions().contains( extension, Qt::CaseInsensitive ) )
193 {
194 error = tr( "“.%1” files are not supported as outputs for this algorithm" ).arg( extension );
195 return false;
196 }
197 return true;
198 }
200 {
201 const QFileInfo fi( outputPath );
202 const QString extension = fi.completeSuffix();
203 if ( !supportedOutputPointCloudLayerExtensions().contains( extension, Qt::CaseInsensitive ) )
204 {
205 error = tr( "“.%1” files are not supported as outputs for this algorithm" ).arg( extension );
206 return false;
207 }
208 return true;
209 }
211 {
212 const QFileInfo fi( outputPath );
213 const QString extension = fi.completeSuffix();
214 if ( !supportedOutputVectorTileLayerExtensions().contains( extension, Qt::CaseInsensitive ) )
215 {
216 error = tr( "“.%1” files are not supported as outputs for this algorithm" ).arg( extension );
217 return false;
218 }
219 return true;
220 }
221 else
222 {
223 return true;
224 }
225}
226
228{
229 const QString userDefault = QgsProcessingUtils::defaultVectorExtension();
230
231 const QStringList supportedExtensions = supportedOutputVectorLayerExtensions();
232 if ( supportedExtensions.contains( userDefault, Qt::CaseInsensitive ) )
233 {
234 // user set default is supported by provider, use that
235 return userDefault;
236 }
237 else if ( !supportedExtensions.empty() )
238 {
239 return supportedExtensions.at( 0 );
240 }
241 else
242 {
243 // who knows? provider says it has no file support at all...
244 // let's say shp. even MapInfo supports shapefiles.
245 return hasGeometry ? QStringLiteral( "shp" ) : QStringLiteral( "dbf" );
246 }
247}
248
250{
251 const QString userDefault = QgsProcessingUtils::defaultRasterExtension();
252
253 const QStringList supportedExtensions = supportedOutputRasterLayerExtensions();
254 if ( supportedExtensions.contains( userDefault, Qt::CaseInsensitive ) )
255 {
256 // user set default is supported by provider, use that
257 return userDefault;
258 }
259 else if ( !supportedExtensions.empty() )
260 {
261 return supportedExtensions.at( 0 );
262 }
263 else
264 {
265 // who knows? provider says it has no file support at all...
266 return QStringLiteral( "tif" );
267 }
268}
269
271{
272 const QString userDefault = QgsProcessingUtils::defaultPointCloudExtension();
273
274 const QStringList supportedExtensions = supportedOutputPointCloudLayerExtensions();
275 if ( supportedExtensions.contains( userDefault, Qt::CaseInsensitive ) )
276 {
277 // user set default is supported by provider, use that
278 return userDefault;
279 }
280 else if ( !supportedExtensions.empty() )
281 {
282 return supportedExtensions.at( 0 );
283 }
284 else
285 {
286 // who knows? provider says it has no file support at all...
287 return QStringLiteral( "las" );
288 }
289}
290
292{
293 const QString userDefault = QgsProcessingUtils::defaultVectorTileExtension();
294
295 const QStringList supportedExtensions = supportedOutputVectorTileLayerExtensions();
296 if ( supportedExtensions.contains( userDefault, Qt::CaseInsensitive ) )
297 {
298 // user set default is supported by provider, use that
299 return userDefault;
300 }
301 else if ( !supportedExtensions.empty() )
302 {
303 return supportedExtensions.at( 0 );
304 }
305 else
306 {
307 // who knows? provider says it has no file support at all...
308 return QStringLiteral( "mbtiles" );
309 }
310}
311
313{
314 return true;
315}
QFlags< ProcessingProviderFlag > ProcessingProviderFlags
Flags indicating how and when an processing provider operates and should be exposed to users.
Definition qgis.h:3372
@ 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