QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
qgsrasterinterface.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsrasterface.h - Internal raster processing modules interface
3  --------------------------------------
4  Date : Jun 21, 2012
5  Copyright : (C) 2012 by Radim Blazek
6  email : radim dot blazek 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 #ifndef QGSRASTERINTERFACE_H
19 #define QGSRASTERINTERFACE_H
20 
21 #include "qgis_core.h"
22 #include "qgis_sip.h"
23 #include <limits>
24 
25 #include <QCoreApplication> // for tr()
26 #include <QImage>
27 
28 #include "qgsfeedback.h"
29 #include "qgsrasterbandstats.h"
30 #include "qgsrasterblock.h"
31 #include "qgsrasterhistogram.h"
32 #include "qgsrectangle.h"
33 
40 class CORE_EXPORT QgsRasterBlockFeedback : public QgsFeedback
41 {
42  Q_OBJECT
43 
44  public:
46  QgsRasterBlockFeedback( QObject *parent = nullptr ) : QgsFeedback( parent ) {}
47 
52  virtual void onNewData() {}
53 
59  bool isPreviewOnly() const { return mPreviewOnly; }
60 
65  void setPreviewOnly( bool preview ) { mPreviewOnly = preview; }
66 
71  bool renderPartialOutput() const { return mRenderPartialOutput; }
72 
77  void setRenderPartialOutput( bool enable ) { mRenderPartialOutput = enable; }
78 
86  void appendError( const QString &error ) { mErrors.append( error ); }
87 
94  QStringList errors() const { return mErrors; }
95 
96  private:
97 
102  bool mPreviewOnly = false;
103 
105  bool mRenderPartialOutput = false;
106 
108  QStringList mErrors;
109 };
110 
111 
116 class CORE_EXPORT QgsRasterInterface
117 {
118 #ifdef SIP_RUN
119 // QgsRasterInterface subclasses
121 #include <qgshuesaturationfilter.h>
122 #include <qgsrasterdataprovider.h>
123 #include <qgsrasternuller.h>
124 #include <qgsrasterprojector.h>
125 #include <qgsrasterrenderer.h>
126 #include <qgsrasterresamplefilter.h>
127 
128 // QgsRasterRenderer subclasses
129 #include <qgshillshaderenderer.h>
135 #endif
136 
137 
138 #ifdef SIP_RUN
140  if ( dynamic_cast<QgsBrightnessContrastFilter *>( sipCpp ) )
141  sipType = sipType_QgsBrightnessContrastFilter;
142  else if ( dynamic_cast<QgsHueSaturationFilter *>( sipCpp ) )
143  sipType = sipType_QgsHueSaturationFilter;
144  else if ( dynamic_cast<QgsRasterDataProvider *>( sipCpp ) )
145  {
146  sipType = sipType_QgsRasterDataProvider;
147  // use static cast because QgsRasterDataProvider has multiple inheritance
148  // and we would end up with bad pointer otherwise!
149  *sipCppRet = static_cast<QgsRasterDataProvider *>( sipCpp );
150  }
151  else if ( dynamic_cast<QgsRasterNuller *>( sipCpp ) )
152  sipType = sipType_QgsRasterNuller;
153  else if ( dynamic_cast<QgsRasterProjector *>( sipCpp ) )
154  sipType = sipType_QgsRasterProjector;
155  else if ( dynamic_cast<QgsRasterRenderer *>( sipCpp ) )
156  {
157  if ( dynamic_cast<QgsHillshadeRenderer *>( sipCpp ) )
158  sipType = sipType_QgsHillshadeRenderer;
159  else if ( dynamic_cast<QgsMultiBandColorRenderer *>( sipCpp ) )
160  sipType = sipType_QgsMultiBandColorRenderer;
161  else if ( dynamic_cast<QgsPalettedRasterRenderer *>( sipCpp ) )
162  sipType = sipType_QgsPalettedRasterRenderer;
163  else if ( dynamic_cast<QgsSingleBandColorDataRenderer *>( sipCpp ) )
164  sipType = sipType_QgsSingleBandColorDataRenderer;
165  else if ( dynamic_cast<QgsSingleBandGrayRenderer *>( sipCpp ) )
166  sipType = sipType_QgsSingleBandGrayRenderer;
167  else if ( dynamic_cast<QgsSingleBandPseudoColorRenderer *>( sipCpp ) )
168  sipType = sipType_QgsSingleBandPseudoColorRenderer;
169  else
170  sipType = sipType_QgsRasterRenderer;
171  }
172  else if ( dynamic_cast<QgsRasterResampleFilter *>( sipCpp ) )
173  sipType = sipType_QgsRasterResampleFilter;
174  else
175  sipType = 0;
176  SIP_END
177 #endif
178 
179  Q_DECLARE_TR_FUNCTIONS( QgsRasterInterface )
180 
181  public:
184  {
185  NoCapabilities = 0,
186  Size = 1 << 1, // original data source size (and thus resolution) is known, it is not always available, for example for WMS
187  Create = 1 << 2, // create new datasets
188  Remove = 1 << 3, // delete datasets
189  BuildPyramids = 1 << 4, // supports building of pyramids (overviews)
190  Identify = 1 << 5, // at least one identify format supported
191  IdentifyValue = 1 << 6, // numerical values
192  IdentifyText = 1 << 7, // WMS text
193  IdentifyHtml = 1 << 8, // WMS HTML
194  IdentifyFeature = 1 << 9, // WMS GML -> feature
195  };
196 
197  QgsRasterInterface( QgsRasterInterface *input = nullptr );
198 
199  virtual ~QgsRasterInterface() = default;
200 
202  virtual QgsRasterInterface *clone() const = 0 SIP_FACTORY;
203 
205  virtual int capabilities() const
206  {
208  }
209 
213  QString capabilitiesString() const;
214 
216  virtual Qgis::DataType dataType( int bandNo ) const = 0;
217 
222  virtual Qgis::DataType sourceDataType( int bandNo ) const { return mInput ? mInput->sourceDataType( bandNo ) : Qgis::UnknownDataType; }
223 
228  virtual QgsRectangle extent() const { return mInput ? mInput->extent() : QgsRectangle(); }
229 
230  int dataTypeSize( int bandNo ) { return QgsRasterBlock::typeSize( dataType( bandNo ) ); }
231 
233  virtual int bandCount() const = 0;
234 
236  virtual int xBlockSize() const { return mInput ? mInput->xBlockSize() : 0; }
237  virtual int yBlockSize() const { return mInput ? mInput->yBlockSize() : 0; }
238 
240  virtual int xSize() const { return mInput ? mInput->xSize() : 0; }
241  virtual int ySize() const { return mInput ? mInput->ySize() : 0; }
242 
244  virtual QString generateBandName( int bandNumber ) const
245  {
246  return tr( "Band" ) + QStringLiteral( " %1" ) .arg( bandNumber, 1 + static_cast< int >( std::log10( static_cast< double >( bandCount() ) ) ), 10, QChar( '0' ) );
247  }
248 
259  virtual QgsRasterBlock *block( int bandNo, const QgsRectangle &extent, int width, int height, QgsRasterBlockFeedback *feedback = nullptr ) = 0 SIP_FACTORY;
260 
265  virtual bool setInput( QgsRasterInterface *input ) { mInput = input; return true; }
266 
268  virtual QgsRasterInterface *input() const { return mInput; }
269 
271  virtual bool on() const { return mOn; }
272 
274  virtual void setOn( bool on ) { mOn = on; }
275 
282  virtual const QgsRasterInterface *sourceInput() const SIP_SKIP
283  {
284  QgsDebugMsgLevel( QStringLiteral( "Entered" ), 4 );
285  return mInput ? mInput->sourceInput() : this;
286  }
287 
294  {
295  QgsDebugMsgLevel( QStringLiteral( "Entered" ), 4 );
296  return mInput ? mInput->sourceInput() : this;
297  }
298 
307  virtual QgsRasterBandStats bandStatistics( int bandNo,
308  int stats = QgsRasterBandStats::All,
309  const QgsRectangle &extent = QgsRectangle(),
310  int sampleSize = 0, QgsRasterBlockFeedback *feedback = nullptr );
311 
316  virtual bool hasStatistics( int bandNo,
317  int stats = QgsRasterBandStats::All,
318  const QgsRectangle &extent = QgsRectangle(),
319  int sampleSize = 0 );
320 
321 
335 #ifndef SIP_RUN
336  virtual QgsRasterHistogram histogram( int bandNo,
337  int binCount = 0,
338  double minimum = std::numeric_limits<double>::quiet_NaN(),
339  double maximum = std::numeric_limits<double>::quiet_NaN(),
340  const QgsRectangle &extent = QgsRectangle(),
341  int sampleSize = 0,
342  bool includeOutOfRange = false,
343  QgsRasterBlockFeedback *feedback = nullptr );
344 #else
345  virtual QgsRasterHistogram histogram( int bandNo,
346  int binCount = 0,
347  SIP_PYOBJECT minimum = Py_None,
348  SIP_PYOBJECT maximum = Py_None,
349  const QgsRectangle &extent = QgsRectangle(),
350  int sampleSize = 0,
351  bool includeOutOfRange = false,
352  QgsRasterBlockFeedback *feedback = nullptr )
353  [QgsRasterHistogram( int bandNo,
354  int binCount = 0,
355  double minimum = 0.0,
356  double maximum = 0.0,
357  const QgsRectangle &extent = QgsRectangle(),
358  int sampleSize = 0,
359  bool includeOutOfRange = false,
360  QgsRasterBlockFeedback *feedback = nullptr )];
361  % MethodCode
362  double minimum;
363  double maximum;
364  if ( a2 == Py_None )
365  {
366  minimum = std::numeric_limits<double>::quiet_NaN();
367  }
368  else
369  {
370  minimum = PyFloat_AsDouble( a2 );
371  }
372 
373  if ( a3 == Py_None )
374  {
375  maximum = std::numeric_limits<double>::quiet_NaN();
376  }
377  else
378  {
379  maximum = PyFloat_AsDouble( a3 );
380  }
381 
382  QgsRasterHistogram *h = new QgsRasterHistogram( sipCpp->histogram( a0, a1, minimum, maximum, *a4, a5, a6, a7 ) );
383  return sipConvertFromType( h, sipType_QgsRasterHistogram, Py_None );
384  % End
385 #endif
386 
387 
392 #ifndef SIP_RUN
393  virtual bool hasHistogram( int bandNo,
394  int binCount,
395  double minimum = std::numeric_limits<double>::quiet_NaN(),
396  double maximum = std::numeric_limits<double>::quiet_NaN(),
397  const QgsRectangle &extent = QgsRectangle(),
398  int sampleSize = 0,
399  bool includeOutOfRange = false );
400 #else
401  virtual bool hasHistogram( int bandNo,
402  int binCount,
403  SIP_PYOBJECT minimum = Py_None,
404  SIP_PYOBJECT maximum = Py_None,
405  const QgsRectangle &extent = QgsRectangle(),
406  int sampleSize = 0,
407  bool includeOutOfRange = false )
408  [bool( int bandNo,
409  int binCount,
410  double minimum = 0.0,
411  double maximum = 0.0,
412  const QgsRectangle &extent = QgsRectangle(),
413  int sampleSize = 0,
414  bool includeOutOfRange = false )];
415  % MethodCode
416  double minimum;
417  double maximum;
418  if ( a2 == Py_None )
419  {
420  minimum = std::numeric_limits<double>::quiet_NaN();
421  }
422  else
423  {
424  minimum = PyFloat_AsDouble( a2 );
425  }
426 
427  if ( a3 == Py_None )
428  {
429  maximum = std::numeric_limits<double>::quiet_NaN();
430  }
431  else
432  {
433  maximum = PyFloat_AsDouble( a3 );
434  }
435 
436  sipRes = sipCpp->hasHistogram( a0, a1, minimum, maximum, *a4, a5, a6 );
437  % End
438 #endif
439 
440 
451  virtual void cumulativeCut( int bandNo,
452  double lowerCount,
453  double upperCount,
454  double &lowerValue,
455  double &upperValue,
456  const QgsRectangle &extent = QgsRectangle(),
457  int sampleSize = 0 );
458 
460  virtual void writeXml( QDomDocument &doc, QDomElement &parentElem ) const { Q_UNUSED( doc ) Q_UNUSED( parentElem ); }
462  virtual void readXml( const QDomElement &filterElem ) { Q_UNUSED( filterElem ) }
463 
464  protected:
465  // QgsRasterInterface used as input
466  QgsRasterInterface *mInput = nullptr;
467 
469  QList<QgsRasterBandStats> mStatistics;
470 
472  QList<QgsRasterHistogram> mHistograms;
473 
474  // On/off state, if off, it does not do anything, replicates input
475  bool mOn = true;
476 
481 #ifndef SIP_RUN
482  void initHistogram( QgsRasterHistogram &histogram,
483  int bandNo,
484  int binCount,
485  double minimum = std::numeric_limits<double>::quiet_NaN(),
486  double maximum = std::numeric_limits<double>::quiet_NaN(),
487  const QgsRectangle &boundingBox = QgsRectangle(),
488  int sampleSize = 0,
489  bool includeOutOfRange = false );
490 #else
491  void initHistogram( QgsRasterHistogram &histogram,
492  int bandNo,
493  int binCount,
494  SIP_PYOBJECT minimum = Py_None,
495  SIP_PYOBJECT maximum = Py_None,
496  const QgsRectangle &boundingBox = QgsRectangle(),
497  int sampleSize = 0,
498  bool includeOutOfRange = false )
499  [void ( QgsRasterHistogram & histogram,
500  int bandNo,
501  int binCount,
502  double minimum = 0.0,
503  double maximum = 0.0,
504  const QgsRectangle &boundingBox = QgsRectangle(),
505  int sampleSize = 0,
506  bool includeOutOfRange = false )];
507  % MethodCode
508  double minimum;
509  double maximum;
510  if ( a3 == Py_None )
511  {
512  minimum = std::numeric_limits<double>::quiet_NaN();
513  }
514  else
515  {
516  minimum = PyFloat_AsDouble( a3 );
517  }
518 
519  if ( a4 == Py_None )
520  {
521  maximum = std::numeric_limits<double>::quiet_NaN();
522  }
523  else
524  {
525  maximum = PyFloat_AsDouble( a4 );
526  }
527 
528 #if defined(SIP_PROTECTED_IS_PUBLIC)
529  sipCpp->initHistogram( *a0, a1, a2, minimum, maximum, *a5, a6, a7 );
530 #else
531  sipCpp->sipProtect_initHistogram( *a0, a1, a2, minimum, maximum, *a5, a6, a7 );
532 #endif
533  % End
534 #endif
535 
536 
537 
539  void initStatistics( QgsRasterBandStats &statistics, int bandNo,
540  int stats = QgsRasterBandStats::All,
541  const QgsRectangle &boundingBox = QgsRectangle(),
542  int binCount = 0 );
543 
544  private:
545 #ifdef SIP_RUN
547  QgsRasterInterface &operator=( const QgsRasterInterface & );
548 #endif
549 
550  Q_DISABLE_COPY( QgsRasterInterface ) // there is clone() for copying
551 };
552 
553 #endif
554 
555 
virtual void onNewData()
May be emitted by raster data provider to indicate that some partial data are available and a new pre...
A rectangle specified with double values.
Definition: qgsrectangle.h:41
virtual void readXml(const QDomElement &filterElem)
Sets base class members from xml. Usually called from create() methods of subclasses.
bool renderPartialOutput() const
Whether our painter is drawing to a temporary image used just by this layer.
virtual QgsRectangle extent() const
Gets the extent of the interface.
virtual QgsRasterInterface * input() const
Current input.
DataType
Raster data types.
Definition: qgis.h:80
virtual int ySize() const
Capability
If you add to this, please also add to capabilitiesString()
void setRenderPartialOutput(bool enable)
Set whether our painter is drawing to a temporary image used just by this layer.
virtual QgsRasterInterface * sourceInput()
Gets source / raw input, the first in pipe, usually provider.
virtual int xBlockSize() const
Gets block size.
Base class for feedback objects to be used for cancellation of something running in a worker thread...
Definition: qgsfeedback.h:44
The RasterBandStats struct is a container for statistics about a single raster band.
Raster data container.
void setPreviewOnly(bool preview)
set flag whether the block request is for preview purposes only
#define QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:39
#define SIP_SKIP
Definition: qgis_sip.h:126
virtual int capabilities() const
Returns a bitmask containing the supported capabilities.
virtual void setOn(bool on)
Sets whether the interface is on or off.
QList< QgsRasterHistogram > mHistograms
List of cached histograms, all bands mixed.
void appendError(const QString &error)
Appends an error message to the stored list of errors.
#define SIP_END
Definition: qgis_sip.h:189
Unknown or unspecified type.
Definition: qgis.h:82
virtual Qgis::DataType sourceDataType(int bandNo) const
Returns source data type for the band specified by number, source data type may be shorter than dataT...
QgsRasterBlockFeedback(QObject *parent=nullptr)
Construct a new raster block feedback object.
static int typeSize(int dataType)
#define SIP_FACTORY
Definition: qgis_sip.h:76
Base class for processing filters like renderers, reprojector, resampler etc.
virtual bool setInput(QgsRasterInterface *input)
Set input.
virtual bool on() const
Returns whether the interface is on or off.
virtual int yBlockSize() const
QStringList errors() const
Returns a list of any errors encountered while retrieving the raster block.
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:172
bool isPreviewOnly() const
Whether the raster provider should return only data that are already available without waiting for fu...
int dataTypeSize(int bandNo)
The QgsRasterHistogram is a container for histogram of a single raster band.
QList< QgsRasterBandStats > mStatistics
List of cached statistics, all bands mixed.
virtual void writeXml(QDomDocument &doc, QDomElement &parentElem) const
Write base class members to xml.
virtual QString generateBandName(int bandNumber) const
helper function to create zero padded band names
Feedback object tailored for raster block reading.
virtual int xSize() const
Gets raster size.
virtual const QgsRasterInterface * sourceInput() const
Gets source / raw input, the first in pipe, usually provider.
Base class for raster data providers.