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