QGIS API Documentation 4.5.0-Master (3b5d115045b)
Loading...
Searching...
No Matches
qgsprocessingfeedback.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsprocessingfeedback.cpp
3 -------------------------
4 begin : June 2017
5 copyright : (C) 2017 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
18#include "qgsconfig.h"
20
21#include <gdal_version.h>
22#include <ogr_api.h>
23#include <proj.h>
24
25#include "qgsgeos.h"
26#include "qgsmessagelog.h"
28
29#include <QString>
30
31#include "moc_qgsprocessingfeedback.cpp"
32
33using namespace Qt::StringLiterals;
34
35#ifdef HAVE_PDAL_QGIS
36#include <pdal/pdal.hpp>
37#endif
38
39#ifdef HAVE_SFCGAL
40#include <SFCGAL/capi/sfcgal_c.h>
41#endif
42
43#ifdef WITH_GEOGRAPHICLIB
44#include <GeographicLib/Constants.hpp>
45#endif
46
48 : mLogFeedback( logFeedback )
49{}
50
51void QgsProcessingFeedback::setProgressText( const QString &text )
52{
53 mHtmlLog.append( text.toHtmlEscaped().replace( '\n', "<br>"_L1 ) + u"<br/>"_s );
54 mTextLog.append( text + '\n' );
55
56 emit progressTextChanged( text );
57}
58
59void QgsProcessingFeedback::log( const QString &htmlMessage, const QString &textMessage )
60{
61 constexpr int MESSAGE_COUNT_LIMIT = 10000;
62 // Avoid logging too many messages, which might blow memory.
63 if ( mMessageLoggedCount == MESSAGE_COUNT_LIMIT )
64 return;
65 ++mMessageLoggedCount;
66 if ( mMessageLoggedCount == MESSAGE_COUNT_LIMIT )
67 {
68 mHtmlLog.append( u"<span style=\"color:red\">%1</span><br/>"_s.arg( tr( "Message log truncated" ) ) );
69 mTextLog.append( tr( "Message log truncated" ) + '\n' );
70 }
71 else
72 {
73 mHtmlLog.append( htmlMessage );
74 mTextLog.append( textMessage );
75 }
76}
77
78void QgsProcessingFeedback::reportError( const QString &error, bool fatalError )
79{
80 if ( mLogFeedback )
81 QgsMessageLog::logMessage( error, tr( "Processing" ), Qgis::MessageLevel::Critical );
82
83 log( u"<span style=\"color:red\">%1</span><br/>"_s.arg( error.toHtmlEscaped() ).replace( '\n', "<br>"_L1 ), error + '\n' );
84
85 emit errorReported( error, fatalError );
86}
87
88void QgsProcessingFeedback::pushWarning( const QString &warning )
89{
90 if ( mLogFeedback )
91 QgsMessageLog::logMessage( warning, tr( "Processing" ), Qgis::MessageLevel::Warning );
92
93 log( u"<span style=\"color:#b85a20;\">%1</span><br/>"_s.arg( warning.toHtmlEscaped() ).replace( '\n', "<br>"_L1 ) + u"<br/>"_s, warning + '\n' );
94
95 emit warningPushed( warning );
96}
97
98void QgsProcessingFeedback::pushInfo( const QString &info )
99{
100 if ( mLogFeedback )
101 QgsMessageLog::logMessage( info, tr( "Processing" ), Qgis::MessageLevel::Info );
102
103 mHtmlLog.append( info.toHtmlEscaped().replace( '\n', "<br>"_L1 ) + u"<br/>"_s );
104 mTextLog.append( info + '\n' );
105
106 emit infoPushed( info );
107}
108
109void QgsProcessingFeedback::pushFormattedMessage( const QString &html, const QString &text )
110{
111 if ( mLogFeedback )
112 QgsMessageLog::logMessage( text, tr( "Processing" ), Qgis::MessageLevel::Info );
113
114 mHtmlLog.append( html + u"<br/>"_s );
115 mTextLog.append( text + '\n' );
116
117 emit formattedMessagePushed( html );
118}
119
120void QgsProcessingFeedback::pushCommandInfo( const QString &info )
121{
122 if ( mLogFeedback )
123 QgsMessageLog::logMessage( info, tr( "Processing" ), Qgis::MessageLevel::Info );
124
125 log( u"<code>%1</code><br/>"_s.arg( info.toHtmlEscaped().replace( '\n', "<br>"_L1 ) ), info + '\n' );
126
127 emit commandInfoPushed( info );
128}
129
130void QgsProcessingFeedback::pushDebugInfo( const QString &info )
131{
132 if ( mLogFeedback )
133 QgsMessageLog::logMessage( info, tr( "Processing" ), Qgis::MessageLevel::Info );
134
135 log( u"<span style=\"color:#777\">%1</span><br/>"_s.arg( info.toHtmlEscaped().replace( '\n', "<br>"_L1 ) ), info + '\n' );
136
137 emit debugInfoPushed( info );
138}
139
140void QgsProcessingFeedback::pushConsoleInfo( const QString &info )
141{
142 if ( mLogFeedback )
143 QgsMessageLog::logMessage( info, tr( "Processing" ), Qgis::MessageLevel::Info );
144
145 log( u"<code style=\"color:#777\">%1</code><br/>"_s.arg( info.toHtmlEscaped().replace( '\n', "<br>"_L1 ) ), info + '\n' );
146
147 emit consoleInfoPushed( info );
148}
149
151{
152 pushDebugInfo( tr( "QGIS version: %1" ).arg( Qgis::version() ) );
153 if ( QString( Qgis::devVersion() ) != "exported"_L1 )
154 {
155 pushDebugInfo( tr( "QGIS code revision: %1" ).arg( Qgis::devVersion() ) );
156 }
157 pushDebugInfo( tr( "Qt version: %1" ).arg( qVersion() ) );
158 pushDebugInfo( tr( "Python version: %1" ).arg( PYTHON_VERSION ) );
159 pushDebugInfo( tr( "GDAL version: %1" ).arg( GDALVersionInfo( "RELEASE_NAME" ) ) );
160 pushDebugInfo( tr( "GEOS version: %1" ).arg( GEOSversion() ) );
161
162 const PJ_INFO info = proj_info();
163 pushDebugInfo( tr( "PROJ version: %1" ).arg( info.release ) );
164
165#ifdef HAVE_PDAL_QGIS
166#if PDAL_VERSION_MAJOR_INT > 1 || ( PDAL_VERSION_MAJOR_INT == 1 && PDAL_VERSION_MINOR_INT >= 7 )
167 pushDebugInfo( tr( "PDAL version: %1" ).arg( QString::fromStdString( pdal::Config::fullVersionString() ) ) );
168#else
169 pushDebugInfo( tr( "PDAL version: %1" ).arg( QString::fromStdString( pdal::GetFullVersionString() ) ) );
170#endif
171#endif
172
173#ifdef HAVE_SFCGAL
174 pushDebugInfo( tr( "SFCGAL version: %1" ).arg( sfcgal_version() ) );
175#else
176 pushDebugInfo( tr( "No support for SFCGAL" ) );
177#endif
178
179#ifdef WITH_GEOGRAPHICLIB
180 pushDebugInfo( tr( "GeographicLib version: %1.%2.%3" ).arg( GEOGRAPHICLIB_VERSION_MAJOR ).arg( GEOGRAPHICLIB_VERSION_MINOR ).arg( GEOGRAPHICLIB_VERSION_PATCH ) );
181#else
182 pushDebugInfo( tr( "No support for GeographicLib" ) );
183#endif
184
185 if ( provider && !provider->versionInfo().isEmpty() )
186 {
187 pushDebugInfo( tr( "%1 version: %2" ).arg( provider->name(), provider->versionInfo() ) );
188 }
189}
190
192{
193 if ( results.empty() )
194 return;
195
196 pushInfo( tr( "Results:" ) );
197
198 const QList< const QgsProcessingOutputDefinition * > outputs = algorithm->outputDefinitions();
199 for ( const QgsProcessingOutputDefinition *output : outputs )
200 {
201 const QString outputName = output->name();
202 if ( outputName == "CHILD_RESULTS"_L1 || outputName == "CHILD_INPUTS"_L1 )
203 continue;
204
205 if ( !results.contains( outputName ) )
206 continue;
207
208 bool ok = false;
209 const QString textValue = output->valueAsString( results.value( output->name() ), context, ok );
210 const QString formattedValue = output->valueAsFormattedString( results.value( output->name() ), context, ok );
211 if ( ok )
212 {
213 pushFormattedMessage( u"<code>&nbsp;&nbsp;%1: %2</code>"_s.arg( output->name(), formattedValue ), u" %1: %2"_s.arg( output->name(), textValue ) );
214 }
215 }
216}
217
219{
220 return mHtmlLog;
221}
222
224{
225 return mTextLog;
226}
227
228void QgsProcessingFeedback::reportSourceLoaded( const QString &parameterName, long long featureCount )
229{
230 emit sourceLoaded( parameterName, featureCount );
231}
232
233void QgsProcessingFeedback::featureAddedToSink( const QString &output )
234{
235 long long countAtLastSignal = 0;
236 long long previousCount = 0;
237 auto it = mSinkFeatureCounts.find( output );
238 if ( it == mSinkFeatureCounts.end() )
239 {
240 it = mSinkFeatureCounts.insert( output, SinkStats() );
241 }
242 else
243 {
244 countAtLastSignal = it.value().countAtLastSignal;
245 previousCount = it.value().featureCount;
246 }
247
248 const long long newCount = previousCount + 1;
249 it.value().featureCount = newCount;
250 if ( newCount - countAtLastSignal >= 100 )
251 {
252 emit sinkFeatureCountChanged( output, newCount );
253 it.value().countAtLastSignal = newCount;
254 }
255}
256
258{
259 long long previousCount = 0;
260 auto it = mSinkFeatureCounts.find( output );
261 if ( it == mSinkFeatureCounts.end() )
262 {
263 it = mSinkFeatureCounts.insert( output, SinkStats() );
264 }
265 else
266 {
267 previousCount = it.value().featureCount;
268 }
269
270 emit sinkFeatureCountChanged( output, previousCount );
271 it.value().countAtLastSignal = previousCount;
272}
273
275{
276 mSinkFeatureCounts.clear();
277}
278
280 : mChildSteps( childAlgorithmCount )
281 , mFeedback( feedback )
282{
283 if ( mFeedback )
284 {
285 connect( mFeedback, &QgsFeedback::canceled, this, &QgsFeedback::cancel, Qt::DirectConnection );
286 connect( this, &QgsFeedback::progressChanged, this, &QgsProcessingMultiStepFeedback::updateOverallProgress );
287 }
288
289 // initialize with equal weights
290 const double equalWeight = mChildSteps > 0 ? 1.0 / mChildSteps : 0.0;
291 for ( int i = 0; i < mChildSteps; ++i )
292 {
293 mStepWeights << equalWeight;
294 }
295}
296
298{
299 mCurrentStep = step;
300
301 // calculate the base progress (sum of all previous steps)
302 mCurrentStepBaseProgress = 0.0;
303 for ( int i = 0; i < mCurrentStep && i < mStepWeights.count(); ++i )
304 {
305 mCurrentStepBaseProgress += mStepWeights.at( i ) * 100.0;
306 }
307
308 emit progressChanged( 0 );
309 if ( mFeedback )
310 mFeedback->setProgress( mCurrentStepBaseProgress );
311}
312
313void QgsProcessingMultiStepFeedback::setStepWeights( const QList<double> &weights )
314{
315 if ( weights.size() != mChildSteps )
316 {
317 return;
318 }
319
320 const double totalWeight = std::reduce( weights.begin(), weights.end() );
321
322 mStepWeights.clear();
323 if ( totalWeight > 0.0 )
324 {
325 for ( double w : weights )
326 {
327 mStepWeights << ( w / totalWeight );
328 }
329 }
330 else
331 {
332 // fallback to equal weights if total weight is 0
333 const double equalWeight = mChildSteps > 0 ? 1.0 / mChildSteps : 0.0;
334 for ( int i = 0; i < mChildSteps; ++i )
335 {
336 mStepWeights << equalWeight;
337 }
338 }
339}
340
342{
343 if ( mFeedback )
344 mFeedback->setProgressText( text );
345}
346
347void QgsProcessingMultiStepFeedback::reportError( const QString &error, bool fatalError )
348{
349 if ( mFeedback )
350 mFeedback->reportError( error, fatalError );
351}
352
354{
355 if ( mFeedback )
356 mFeedback->pushWarning( warning );
357}
358
360{
361 if ( mFeedback )
362 mFeedback->pushInfo( info );
363}
364
366{
367 if ( mFeedback )
368 mFeedback->pushCommandInfo( info );
369}
370
372{
373 if ( mFeedback )
374 mFeedback->pushDebugInfo( info );
375}
376
378{
379 if ( mFeedback )
380 mFeedback->pushConsoleInfo( info );
381}
382
383void QgsProcessingMultiStepFeedback::pushFormattedMessage( const QString &html, const QString &text )
384{
385 if ( mFeedback )
386 mFeedback->pushFormattedMessage( html, text );
387}
388
390{
391 if ( mFeedback )
392 return mFeedback->htmlLog();
393 return QString();
394}
395
397{
398 if ( mFeedback )
399 return mFeedback->textLog();
400 return QString();
401}
402
403void QgsProcessingMultiStepFeedback::updateOverallProgress( double progress )
404{
405 if ( !mFeedback )
406 return;
407
408 const double currentStepWeight = mStepWeights.value( mCurrentStep, 0 );
409
410 mFeedback->setProgress( mCurrentStepBaseProgress + progress * currentStepWeight );
411}
static QString version()
Version string.
Definition qgis.cpp:682
@ Warning
Warning message.
Definition qgis.h:162
@ Critical
Critical/error message.
Definition qgis.h:163
@ Info
Information message.
Definition qgis.h:161
static QString devVersion()
The development version.
Definition qgis.cpp:699
void progressChanged(double progress)
Emitted when the feedback object reports a progress change.
void canceled()
Internal routines can connect to this signal if they use event loop.
void cancel()
Tells the internal routines that the current operation should be canceled. This should be run by the ...
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition qgsfeedback.h:65
double progress() const
Returns the current progress reported by the feedback object.
Definition qgsfeedback.h:81
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(), Qgis::StringFormat format=Qgis::StringFormat::PlainText)
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.
void featureAddedToSink(const QString &output)
Reports that a feature was added to the the sink associated with the specified algorithm output.
virtual void pushCommandInfo(const QString &info)
Pushes an informational message containing a command from the algorithm.
void warningPushed(const QString &text)
Emitted when an warning is pushed.
virtual void pushInfo(const QString &info)
Pushes a general informational message from the algorithm.
virtual void pushWarning(const QString &warning)
Pushes a warning informational message from the algorithm.
void infoPushed(const QString &text)
Emitted when information text is pushed.
void errorReported(const QString &text, bool fatalError)
Emitted when an error is reported.
void progressTextChanged(const QString &text)
Emitted when the progress text is changed.
void debugInfoPushed(const QString &text)
Emitted when debug information text is pushed.
void sinkFeatureCountChanged(const QString &output, long long featureCount)
Emitted when the count of features pushed to a sink has changed.
void featureSinkFinalized(const QString &output)
Reports that a feature sink has been finalized.
void pushVersionInfo(const QgsProcessingProvider *provider=nullptr)
Pushes a summary of the QGIS (and underlying library) version information to the log.
virtual QString textLog() const
Returns the plain text contents of the log, which contains all messages pushed to the feedback object...
QgsProcessingFeedback(bool logFeedback=true)
Constructor for QgsProcessingFeedback.
void pushFormattedResults(const QgsProcessingAlgorithm *algorithm, QgsProcessingContext &context, const QVariantMap &results)
Pushes a summary of the execution results to the log.
virtual QString htmlLog() const
Returns the HTML formatted contents of the log, which contains all messages pushed to the feedback ob...
void sourceLoaded(const QString &parameterName, long long featureCount)
Emitted when a feature source was retrieved for the specified algorithm input parameter.
virtual void pushDebugInfo(const QString &info)
Pushes an informational message containing debugging helpers from the algorithm.
void reportSourceLoaded(const QString &parameterName, long long featureCount)
Reports that a feature source was retrieved for the specified algorithm input parameter.
void formattedMessagePushed(const QString &html)
Emitted when a formatted html message is pushed.
virtual void pushFormattedMessage(const QString &html, const QString &text)
Pushes a pre-formatted message from the algorithm.
void resetFeatureSinkCounts()
Resets all stored feature sink counts.
virtual void reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.
void commandInfoPushed(const QString &text)
Emitted when command information text is pushed.
void consoleInfoPushed(const QString &text)
Emitted when console information text is pushed.
virtual void pushConsoleInfo(const QString &info)
Pushes a console feedback message from the algorithm.
virtual void setProgressText(const QString &text)
Sets a progress report text string.
QgsProcessingMultiStepFeedback(int steps, QgsProcessingFeedback *feedback)
Constructor for QgsProcessingMultiStepFeedback, for a process with the specified number of steps.
void pushDebugInfo(const QString &info) override
Pushes an informational message containing debugging helpers from the algorithm.
void pushFormattedMessage(const QString &html, const QString &text) override
Pushes a pre-formatted message from the algorithm.
void pushConsoleInfo(const QString &info) override
Pushes a console feedback message from the algorithm.
void pushInfo(const QString &info) override
Pushes a general informational message from the algorithm.
void pushWarning(const QString &warning) override
Pushes a warning informational message from the algorithm.
void setProgressText(const QString &text) override
Sets a progress report text string.
void setCurrentStep(int step)
Sets the step which is being executed.
void setStepWeights(const QList< double > &weights)
Sets the relative weights for each step.
void reportError(const QString &error, bool fatalError=false) override
Reports that the algorithm encountered an error while executing.
QString textLog() const override
Returns the plain text contents of the log, which contains all messages pushed to the feedback object...
void pushCommandInfo(const QString &info) override
Pushes an informational message containing a command from the algorithm.
QString htmlLog() const override
Returns the HTML formatted contents of the log, which contains all messages pushed to the feedback ob...
Base class for the definition of processing outputs.
Abstract base class for processing providers.
virtual QString versionInfo() const
Returns a version information string for the provider, or an empty string if this is not applicable (...
virtual QString name() const =0
Returns the provider name, which is used to describe the provider within the GUI.
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