QGIS API Documentation  3.6.0-Noosa (5873452)
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 
79  private:
80 
85  bool mPreviewOnly = false;
86 
88  bool mRenderPartialOutput = false;
89 };
90 
91 
96 class CORE_EXPORT QgsRasterInterface
97 {
98 #ifdef SIP_RUN
99 // QgsRasterInterface subclasses
101 #include <qgshuesaturationfilter.h>
102 #include <qgsrasterdataprovider.h>
103 #include <qgsrasternuller.h>
104 #include <qgsrasterprojector.h>
105 #include <qgsrasterrenderer.h>
106 #include <qgsrasterresamplefilter.h>
107 
108 // QgsRasterRenderer subclasses
109 #include <qgshillshaderenderer.h>
115 #endif
116 
117 
118 #ifdef SIP_RUN
120  if ( dynamic_cast<QgsBrightnessContrastFilter *>( sipCpp ) )
121  sipType = sipType_QgsBrightnessContrastFilter;
122  else if ( dynamic_cast<QgsHueSaturationFilter *>( sipCpp ) )
123  sipType = sipType_QgsHueSaturationFilter;
124  else if ( dynamic_cast<QgsRasterDataProvider *>( sipCpp ) )
125  {
126  sipType = sipType_QgsRasterDataProvider;
127  // use static cast because QgsRasterDataProvider has multiple inheritance
128  // and we would end up with bad pointer otherwise!
129  *sipCppRet = static_cast<QgsRasterDataProvider *>( sipCpp );
130  }
131  else if ( dynamic_cast<QgsRasterNuller *>( sipCpp ) )
132  sipType = sipType_QgsRasterNuller;
133  else if ( dynamic_cast<QgsRasterProjector *>( sipCpp ) )
134  sipType = sipType_QgsRasterProjector;
135  else if ( dynamic_cast<QgsRasterRenderer *>( sipCpp ) )
136  {
137  if ( dynamic_cast<QgsHillshadeRenderer *>( sipCpp ) )
138  sipType = sipType_QgsHillshadeRenderer;
139  else if ( dynamic_cast<QgsMultiBandColorRenderer *>( sipCpp ) )
140  sipType = sipType_QgsMultiBandColorRenderer;
141  else if ( dynamic_cast<QgsPalettedRasterRenderer *>( sipCpp ) )
142  sipType = sipType_QgsPalettedRasterRenderer;
143  else if ( dynamic_cast<QgsSingleBandColorDataRenderer *>( sipCpp ) )
144  sipType = sipType_QgsSingleBandColorDataRenderer;
145  else if ( dynamic_cast<QgsSingleBandGrayRenderer *>( sipCpp ) )
146  sipType = sipType_QgsSingleBandGrayRenderer;
147  else if ( dynamic_cast<QgsSingleBandPseudoColorRenderer *>( sipCpp ) )
148  sipType = sipType_QgsSingleBandPseudoColorRenderer;
149  else
150  sipType = sipType_QgsRasterRenderer;
151  }
152  else if ( dynamic_cast<QgsRasterResampleFilter *>( sipCpp ) )
153  sipType = sipType_QgsRasterResampleFilter;
154  else
155  sipType = 0;
156  SIP_END
157 #endif
158 
159  Q_DECLARE_TR_FUNCTIONS( QgsRasterInterface )
160 
161  public:
164  {
165  NoCapabilities = 0,
166  Size = 1 << 1, // original data source size (and thus resolution) is known, it is not always available, for example for WMS
167  Create = 1 << 2, // create new datasets
168  Remove = 1 << 3, // delete datasets
169  BuildPyramids = 1 << 4, // supports building of pyramids (overviews)
170  Identify = 1 << 5, // at least one identify format supported
171  IdentifyValue = 1 << 6, // numerical values
172  IdentifyText = 1 << 7, // WMS text
173  IdentifyHtml = 1 << 8, // WMS HTML
174  IdentifyFeature = 1 << 9, // WMS GML -> feature
175  };
176 
177  QgsRasterInterface( QgsRasterInterface *input = nullptr );
178 
179  virtual ~QgsRasterInterface() = default;
180 
182  virtual QgsRasterInterface *clone() const = 0 SIP_FACTORY;
183 
185  virtual int capabilities() const
186  {
188  }
189 
193  QString capabilitiesString() const;
194 
196  virtual Qgis::DataType dataType( int bandNo ) const = 0;
197 
201  virtual Qgis::DataType sourceDataType( int bandNo ) const { return mInput ? mInput->sourceDataType( bandNo ) : Qgis::UnknownDataType; }
202 
207  virtual QgsRectangle extent() const { return mInput ? mInput->extent() : QgsRectangle(); }
208 
209  int dataTypeSize( int bandNo ) { return QgsRasterBlock::typeSize( dataType( bandNo ) ); }
210 
212  virtual int bandCount() const = 0;
213 
215  virtual int xBlockSize() const { return mInput ? mInput->xBlockSize() : 0; }
216  virtual int yBlockSize() const { return mInput ? mInput->yBlockSize() : 0; }
217 
219  virtual int xSize() const { return mInput ? mInput->xSize() : 0; }
220  virtual int ySize() const { return mInput ? mInput->ySize() : 0; }
221 
223  virtual QString generateBandName( int bandNumber ) const
224  {
225  return tr( "Band" ) + QStringLiteral( " %1" ) .arg( bandNumber, 1 + static_cast< int >( std::log10( static_cast< double >( bandCount() ) ) ), 10, QChar( '0' ) );
226  }
227 
238  virtual QgsRasterBlock *block( int bandNo, const QgsRectangle &extent, int width, int height, QgsRasterBlockFeedback *feedback = nullptr ) = 0 SIP_FACTORY;
239 
243  virtual bool setInput( QgsRasterInterface *input ) { mInput = input; return true; }
244 
246  virtual QgsRasterInterface *input() const { return mInput; }
247 
249  virtual bool on() const { return mOn; }
250 
252  virtual void setOn( bool on ) { mOn = on; }
253 
260  virtual const QgsRasterInterface *sourceInput() const SIP_SKIP
261  {
262  QgsDebugMsgLevel( QStringLiteral( "Entered" ), 4 );
263  return mInput ? mInput->sourceInput() : this;
264  }
265 
272  {
273  QgsDebugMsgLevel( QStringLiteral( "Entered" ), 4 );
274  return mInput ? mInput->sourceInput() : this;
275  }
276 
285  virtual QgsRasterBandStats bandStatistics( int bandNo,
286  int stats = QgsRasterBandStats::All,
287  const QgsRectangle &extent = QgsRectangle(),
288  int sampleSize = 0, QgsRasterBlockFeedback *feedback = nullptr );
289 
294  virtual bool hasStatistics( int bandNo,
295  int stats = QgsRasterBandStats::All,
296  const QgsRectangle &extent = QgsRectangle(),
297  int sampleSize = 0 );
298 
299 
313 #ifndef SIP_RUN
314  virtual QgsRasterHistogram histogram( int bandNo,
315  int binCount = 0,
316  double minimum = std::numeric_limits<double>::quiet_NaN(),
317  double maximum = std::numeric_limits<double>::quiet_NaN(),
318  const QgsRectangle &extent = QgsRectangle(),
319  int sampleSize = 0,
320  bool includeOutOfRange = false,
321  QgsRasterBlockFeedback *feedback = nullptr );
322 #else
323  virtual QgsRasterHistogram histogram( int bandNo,
324  int binCount = 0,
325  SIP_PYOBJECT minimum = Py_None,
326  SIP_PYOBJECT maximum = Py_None,
327  const QgsRectangle &extent = QgsRectangle(),
328  int sampleSize = 0,
329  bool includeOutOfRange = false,
330  QgsRasterBlockFeedback *feedback = nullptr )
331  [QgsRasterHistogram( int bandNo,
332  int binCount = 0,
333  double minimum = 0.0,
334  double maximum = 0.0,
335  const QgsRectangle &extent = QgsRectangle(),
336  int sampleSize = 0,
337  bool includeOutOfRange = false,
338  QgsRasterBlockFeedback *feedback = nullptr )];
339  % MethodCode
340  double minimum;
341  double maximum;
342  if ( a2 == Py_None )
343  {
344  minimum = std::numeric_limits<double>::quiet_NaN();
345  }
346  else
347  {
348  minimum = PyFloat_AsDouble( a2 );
349  }
350 
351  if ( a3 == Py_None )
352  {
353  maximum = std::numeric_limits<double>::quiet_NaN();
354  }
355  else
356  {
357  maximum = PyFloat_AsDouble( a3 );
358  }
359 
360  QgsRasterHistogram h = sipCpp->histogram( a0, a1, minimum, maximum, *a4, a5, a6, a7 );
361  sipRes = &h;
362  % End
363 #endif
364 
365 
370 #ifndef SIP_RUN
371  virtual bool hasHistogram( int bandNo,
372  int binCount,
373  double minimum = std::numeric_limits<double>::quiet_NaN(),
374  double maximum = std::numeric_limits<double>::quiet_NaN(),
375  const QgsRectangle &extent = QgsRectangle(),
376  int sampleSize = 0,
377  bool includeOutOfRange = false );
378 #else
379  virtual bool hasHistogram( int bandNo,
380  int binCount,
381  SIP_PYOBJECT minimum = Py_None,
382  SIP_PYOBJECT maximum = Py_None,
383  const QgsRectangle &extent = QgsRectangle(),
384  int sampleSize = 0,
385  bool includeOutOfRange = false )
386  [bool( int bandNo,
387  int binCount,
388  double minimum = 0.0,
389  double maximum = 0.0,
390  const QgsRectangle &extent = QgsRectangle(),
391  int sampleSize = 0,
392  bool includeOutOfRange = false )];
393  % MethodCode
394  double minimum;
395  double maximum;
396  if ( a2 == Py_None )
397  {
398  minimum = std::numeric_limits<double>::quiet_NaN();
399  }
400  else
401  {
402  minimum = PyFloat_AsDouble( a2 );
403  }
404 
405  if ( a3 == Py_None )
406  {
407  maximum = std::numeric_limits<double>::quiet_NaN();
408  }
409  else
410  {
411  maximum = PyFloat_AsDouble( a3 );
412  }
413 
414  sipRes = sipCpp->hasHistogram( a0, a1, minimum, maximum, *a4, a5, a6 );
415  % End
416 #endif
417 
418 
429  virtual void cumulativeCut( int bandNo,
430  double lowerCount,
431  double upperCount,
432  double &lowerValue,
433  double &upperValue,
434  const QgsRectangle &extent = QgsRectangle(),
435  int sampleSize = 0 );
436 
438  virtual void writeXml( QDomDocument &doc, QDomElement &parentElem ) const { Q_UNUSED( doc ); Q_UNUSED( parentElem ); }
440  virtual void readXml( const QDomElement &filterElem ) { Q_UNUSED( filterElem ); }
441 
442  protected:
443  // QgsRasterInterface used as input
444  QgsRasterInterface *mInput = nullptr;
445 
447  QList<QgsRasterBandStats> mStatistics;
448 
450  QList<QgsRasterHistogram> mHistograms;
451 
452  // On/off state, if off, it does not do anything, replicates input
453  bool mOn = true;
454 
459 #ifndef SIP_RUN
460  void initHistogram( QgsRasterHistogram &histogram,
461  int bandNo,
462  int binCount,
463  double minimum = std::numeric_limits<double>::quiet_NaN(),
464  double maximum = std::numeric_limits<double>::quiet_NaN(),
465  const QgsRectangle &boundingBox = QgsRectangle(),
466  int sampleSize = 0,
467  bool includeOutOfRange = false );
468 #else
469  void initHistogram( QgsRasterHistogram &histogram,
470  int bandNo,
471  int binCount,
472  SIP_PYOBJECT minimum = Py_None,
473  SIP_PYOBJECT maximum = Py_None,
474  const QgsRectangle &boundingBox = QgsRectangle(),
475  int sampleSize = 0,
476  bool includeOutOfRange = false )
477  [void ( QgsRasterHistogram & histogram,
478  int bandNo,
479  int binCount,
480  double minimum = 0.0,
481  double maximum = 0.0,
482  const QgsRectangle &boundingBox = QgsRectangle(),
483  int sampleSize = 0,
484  bool includeOutOfRange = false )];
485  % MethodCode
486  double minimum;
487  double maximum;
488  if ( a3 == Py_None )
489  {
490  minimum = std::numeric_limits<double>::quiet_NaN();
491  }
492  else
493  {
494  minimum = PyFloat_AsDouble( a3 );
495  }
496 
497  if ( a4 == Py_None )
498  {
499  maximum = std::numeric_limits<double>::quiet_NaN();
500  }
501  else
502  {
503  maximum = PyFloat_AsDouble( a4 );
504  }
505 
506 #if defined(SIP_PROTECTED_IS_PUBLIC)
507  sipCpp->initHistogram( *a0, a1, a2, minimum, maximum, *a5, a6, a7 );
508 #else
509  sipCpp->sipProtect_initHistogram( *a0, a1, a2, minimum, maximum, *a5, a6, a7 );
510 #endif
511  % End
512 #endif
513 
514 
515 
517  void initStatistics( QgsRasterBandStats &statistics, int bandNo,
518  int stats = QgsRasterBandStats::All,
519  const QgsRectangle &boundingBox = QgsRectangle(),
520  int binCount = 0 );
521 
522  private:
523 #ifdef SIP_RUN
525  QgsRasterInterface &operator=( const QgsRasterInterface & );
526 #endif
527 
528  Q_DISABLE_COPY( QgsRasterInterface ) // there is clone() for copying
529 };
530 
531 #endif
532 
533 
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:79
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 cancelation 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:119
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.
#define SIP_END
Definition: qgis_sip.h:182
Unknown or unspecified type.
Definition: qgis.h:81
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:69
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
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:165
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.