QGIS API Documentation 3.99.0-Master (752b475928d)
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 <QRegularExpressionMatch>
26
27#include "moc_qgsprocessingprovider.cpp"
28
30 : QObject( parent )
31{}
32
33
35{
36 qDeleteAll( mAlgorithms );
37}
38
40{
41 return QgsApplication::getThemeIcon( "/processingAlgorithm.svg" );
42}
43
45{
46 return QgsApplication::iconPath( QStringLiteral( "processingAlgorithm.svg" ) );
47}
48
53
55{
56 return QString();
57}
58
60{
61 return name();
62}
63
65{
66 return QString();
67}
68
70{
71 const QList<QPair<QString, QString>> formatAndExtensions = supportedOutputRasterLayerFormatAndExtensions();
72 QSet< QString > extensions;
73 QStringList res;
74 for ( const QPair<QString, QString> &formatAndExt : std::as_const( formatAndExtensions ) )
75 {
76 if ( !extensions.contains( formatAndExt.second ) )
77 {
78 extensions.insert( formatAndExt.second );
79 res << formatAndExt.second;
80 }
81 }
82 return res;
83}
84
89
91{
93 QList<QPair<QString, QString>> res;
94
95 const thread_local QRegularExpression rx( QStringLiteral( "\\*\\.([a-zA-Z0-9]*)" ) );
96
97 for ( const QgsRasterFileWriter::FilterFormatDetails &format : formats )
98 {
99 const QString ext = format.filterString;
100 const QRegularExpressionMatch match = rx.match( ext );
101 if ( !match.hasMatch() )
102 continue;
103
104 const QString matched = match.captured( 1 );
105 res << QPair<QString, QString>( format.driverName, matched );
106 }
107
108 std::sort( res.begin(), res.end(), []( const QPair<QString, QString> &a, const QPair<QString, QString> &b ) -> bool
109 {
110 for ( const QString &tifExt : { QStringLiteral( "tif" ), QStringLiteral( "tiff" ) } )
111 {
112 if ( a.second == tifExt )
113 {
114 if ( b.second == a.second )
115 {
116 if ( a.first == QLatin1String( "GTiff" ) )
117 return true;
118 else if ( b.first == QLatin1String( "GTiff" ) )
119 return false;
120 return a.first.toLower().localeAwareCompare( b.first.toLower() ) < 0;
121 }
122 return true;
123 }
124 else if ( b.second == tifExt )
125 return false;
126 }
127
128 if ( a.second == QLatin1String( "gpkg" ) )
129 {
130 if ( b.second == a.second )
131 return a.first.toLower().localeAwareCompare( b.first.toLower() ) < 0;
132 return true;
133 }
134 else if ( b.second == QLatin1String( "gpkg" ) )
135 return false;
136
137 return a.second.toLower().localeAwareCompare( b.second.toLower() ) < 0;
138 } );
139
140 return res;
141}
142
144{
145 return QStringList();
146}
147
152
154{
155 qDeleteAll( mAlgorithms );
156 mAlgorithms.clear();
157 if ( isActive() )
158 {
160 emit algorithmsLoaded();
161 }
162}
163
164QList<const QgsProcessingAlgorithm *> QgsProcessingProvider::algorithms() const
165{
166 return mAlgorithms.values();
167}
168
170{
171 return mAlgorithms.value( name );
172}
173
175{
176 if ( !algorithm )
177 return false;
178
179 if ( mAlgorithms.contains( algorithm->name() ) )
180 {
181 QgsMessageLog::logMessage( tr( "Duplicate algorithm name %1 for provider %2" ).arg( algorithm->name(), id() ), QObject::tr( "Processing" ) );
182 return false;
183 }
184
185 // init the algorithm - this allows direct querying of the algorithm's parameters
186 // and outputs from the provider's copy
187 algorithm->initAlgorithm( QVariantMap() );
188
189 algorithm->setProvider( this );
190 mAlgorithms.insert( algorithm->name(), algorithm );
191 return true;
192}
193
198
203
204bool QgsProcessingProvider::isSupportedOutputValue( const QVariant &outputValue, const QgsProcessingDestinationParameter *parameter, QgsProcessingContext &context, QString &error ) const
205{
206 error.clear();
207 QString outputPath = QgsProcessingParameters::parameterAsOutputLayer( parameter, outputValue, context, true ).trimmed();
208
209 if ( outputPath.isEmpty() )
210 {
212 {
213 return true;
214 }
215 else
216 {
217 error = tr( "Missing parameter value %1" ).arg( parameter->description() );
218 return false;
219 }
220 }
221
224 {
225 if ( outputPath.startsWith( QLatin1String( "memory:" ) ) )
226 {
228 {
229 error = tr( "This algorithm only supports disk-based outputs" );
230 return false;
231 }
232 return true;
233 }
234
235 QString providerKey;
236 QString uri;
237 QString layerName;
238 QMap<QString, QVariant> options;
239 bool useWriter = false;
240 QString format;
241 QString extension;
242 QgsProcessingUtils::parseDestinationString( outputPath, providerKey, uri, layerName, format, options, useWriter, extension );
243
244 if ( providerKey != QLatin1String( "ogr" ) )
245 {
247 {
248 error = tr( "This algorithm only supports disk-based outputs" );
249 return false;
250 }
251 return true;
252 }
253
254 if ( !supportedOutputVectorLayerExtensions().contains( extension, Qt::CaseInsensitive ) )
255 {
256 error = tr( "“.%1” files are not supported as outputs for this algorithm" ).arg( extension );
257 return false;
258 }
259 return true;
260 }
261 else if ( parameter->type() == QgsProcessingParameterRasterDestination::typeName() )
262 {
263 const QFileInfo fi( outputPath );
264 const QString extension = fi.suffix();
265 if ( !supportedOutputRasterLayerExtensions().contains( extension, Qt::CaseInsensitive ) )
266 {
267 error = tr( "“.%1” files are not supported as outputs for this algorithm" ).arg( extension );
268 return false;
269 }
270 return true;
271 }
273 {
274 const QFileInfo fi( outputPath );
275 const QString extension = fi.completeSuffix();
276 if ( !supportedOutputPointCloudLayerExtensions().contains( extension, Qt::CaseInsensitive ) )
277 {
278 error = tr( "“.%1” files are not supported as outputs for this algorithm" ).arg( extension );
279 return false;
280 }
281 return true;
282 }
284 {
285 const QFileInfo fi( outputPath );
286 const QString extension = fi.completeSuffix();
287 if ( !supportedOutputVectorTileLayerExtensions().contains( extension, Qt::CaseInsensitive ) )
288 {
289 error = tr( "“.%1” files are not supported as outputs for this algorithm" ).arg( extension );
290 return false;
291 }
292 return true;
293 }
294 else
295 {
296 return true;
297 }
298}
299
301{
302 const QString userDefault = QgsProcessingUtils::defaultVectorExtension();
303
304 const QStringList supportedExtensions = supportedOutputVectorLayerExtensions();
305 if ( supportedExtensions.contains( userDefault, Qt::CaseInsensitive ) )
306 {
307 // user set default is supported by provider, use that
308 return userDefault;
309 }
310 else if ( !supportedExtensions.empty() )
311 {
312 return supportedExtensions.at( 0 );
313 }
314 else
315 {
316 // who knows? provider says it has no file support at all...
317 // let's say shp. even MapInfo supports shapefiles.
318 return hasGeometry ? QStringLiteral( "shp" ) : QStringLiteral( "dbf" );
319 }
320}
321
323{
324 const QString userDefault = QgsProcessingUtils::defaultRasterFormat();
325
326 const QList<QPair<QString, QString>> formatAndExtensions = supportedOutputRasterLayerFormatAndExtensions();
327 for ( const QPair<QString, QString> &formatAndExt : std::as_const( formatAndExtensions ) )
328 {
329 if ( formatAndExt.first.compare( userDefault, Qt::CaseInsensitive ) == 0 )
330 {
331 // user set default is supported by provider, use that
332 return userDefault;
333 }
334 }
335
336 if ( !formatAndExtensions.empty() )
337 {
338 return formatAndExtensions.at( 0 ).first;
339 }
340 else
341 {
342 // who knows? provider says it has no file support at all...
343 return QStringLiteral( "GTiff" );
344 }
345}
346
348{
349 QString format = defaultRasterFileFormat();
350 QStringList extensions = QgsRasterFileWriter::extensionsForFormat( format );
351 if ( !extensions.isEmpty() )
352 return extensions[0];
353
354 return QStringLiteral( "tif" );
355}
356
358{
359 const QString userDefault = QgsProcessingUtils::defaultPointCloudExtension();
360
361 const QStringList supportedExtensions = supportedOutputPointCloudLayerExtensions();
362 if ( supportedExtensions.contains( userDefault, Qt::CaseInsensitive ) )
363 {
364 // user set default is supported by provider, use that
365 return userDefault;
366 }
367 else if ( !supportedExtensions.empty() )
368 {
369 return supportedExtensions.at( 0 );
370 }
371 else
372 {
373 // who knows? provider says it has no file support at all...
374 return QStringLiteral( "las" );
375 }
376}
377
379{
380 const QString userDefault = QgsProcessingUtils::defaultVectorTileExtension();
381
382 const QStringList supportedExtensions = supportedOutputVectorTileLayerExtensions();
383 if ( supportedExtensions.contains( userDefault, Qt::CaseInsensitive ) )
384 {
385 // user set default is supported by provider, use that
386 return userDefault;
387 }
388 else if ( !supportedExtensions.empty() )
389 {
390 return supportedExtensions.at( 0 );
391 }
392 else
393 {
394 // who knows? provider says it has no file support at all...
395 return QStringLiteral( "mbtiles" );
396 }
397}
398
400{
401 return true;
402}
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 QList< QPair< QString, QString > > supportedOutputRasterLayerFormatAndExtensions() const
Returns a list of (format, file extension) supported by this provider.
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.
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.
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 defaultRasterFileFormat() const
Returns the default file format to use for raster outputs created by the provider.
virtual QString svgIconPath() const
Returns a path to an SVG version of the provider's icon.
static QList< QPair< QString, QString > > supportedOutputRasterLayerFormatAndExtensionsDefault()
Returns a list of (format, file extension) supported by GDAL.
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 defaultRasterFormat()
Returns the default raster format 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 extensionsForFormat(const QString &format)
Returns a list of known file extensions for the given GDAL driver format.
static QList< QgsRasterFileWriter::FilterFormatDetails > supportedFiltersAndFormats(RasterFormatOptions options=SortRecommended)
Returns a list or pairs, with format filter string as first element and GDAL format key as second ele...
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
Details of available filters and formats.