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