QGIS API Documentation  3.0.2-Girona (307d082)
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 
287  virtual QgsRasterBandStats bandStatistics( int bandNo,
288  int stats = QgsRasterBandStats::All,
289  const QgsRectangle &extent = QgsRectangle(),
290  int sampleSize = 0, QgsRasterBlockFeedback *feedback = nullptr );
291 
296  virtual bool hasStatistics( int bandNo,
297  int stats = QgsRasterBandStats::All,
298  const QgsRectangle &extent = QgsRectangle(),
299  int sampleSize = 0 );
300 
301 
315 #ifndef SIP_RUN
316  virtual QgsRasterHistogram histogram( int bandNo,
317  int binCount = 0,
318  double minimum = std::numeric_limits<double>::quiet_NaN(),
319  double maximum = std::numeric_limits<double>::quiet_NaN(),
320  const QgsRectangle &extent = QgsRectangle(),
321  int sampleSize = 0,
322  bool includeOutOfRange = false,
323  QgsRasterBlockFeedback *feedback = nullptr );
324 #else
325  virtual QgsRasterHistogram histogram( int bandNo,
326  int binCount = 0,
327  SIP_PYOBJECT minimum = Py_None,
328  SIP_PYOBJECT maximum = Py_None,
329  const QgsRectangle &extent = QgsRectangle(),
330  int sampleSize = 0,
331  bool includeOutOfRange = false,
332  QgsRasterBlockFeedback *feedback = nullptr )
333  [QgsRasterHistogram( int bandNo,
334  int binCount = 0,
335  double minimum = 0.0,
336  double maximum = 0.0,
337  const QgsRectangle &extent = QgsRectangle(),
338  int sampleSize = 0,
339  bool includeOutOfRange = false,
340  QgsRasterBlockFeedback *feedback = nullptr )];
341  % MethodCode
342  double minimum;
343  double maximum;
344  if ( a2 == Py_None )
345  {
346  minimum = std::numeric_limits<double>::quiet_NaN();
347  }
348  else
349  {
350  minimum = PyFloat_AsDouble( a2 );
351  }
352 
353  if ( a3 == Py_None )
354  {
355  maximum = std::numeric_limits<double>::quiet_NaN();
356  }
357  else
358  {
359  maximum = PyFloat_AsDouble( a3 );
360  }
361 
362  QgsRasterHistogram h = sipCpp->histogram( a0, a1, minimum, maximum, *a4, a5, a6, a7 );
363  sipRes = &h;
364  % End
365 #endif
366 
367 
372 #ifndef SIP_RUN
373  virtual bool hasHistogram( int bandNo,
374  int binCount,
375  double minimum = std::numeric_limits<double>::quiet_NaN(),
376  double maximum = std::numeric_limits<double>::quiet_NaN(),
377  const QgsRectangle &extent = QgsRectangle(),
378  int sampleSize = 0,
379  bool includeOutOfRange = false );
380 #else
381  virtual bool hasHistogram( int bandNo,
382  int binCount,
383  SIP_PYOBJECT minimum = Py_None,
384  SIP_PYOBJECT maximum = Py_None,
385  const QgsRectangle &extent = QgsRectangle(),
386  int sampleSize = 0,
387  bool includeOutOfRange = false )
388  [bool( int bandNo,
389  int binCount,
390  double minimum = 0.0,
391  double maximum = 0.0,
392  const QgsRectangle &extent = QgsRectangle(),
393  int sampleSize = 0,
394  bool includeOutOfRange = false )];
395  % MethodCode
396  double minimum;
397  double maximum;
398  if ( a2 == Py_None )
399  {
400  minimum = std::numeric_limits<double>::quiet_NaN();
401  }
402  else
403  {
404  minimum = PyFloat_AsDouble( a2 );
405  }
406 
407  if ( a3 == Py_None )
408  {
409  maximum = std::numeric_limits<double>::quiet_NaN();
410  }
411  else
412  {
413  maximum = PyFloat_AsDouble( a3 );
414  }
415 
416  sipRes = sipCpp->hasHistogram( a0, a1, minimum, maximum, *a4, a5, a6 );
417  % End
418 #endif
419 
420 
431  virtual void cumulativeCut( int bandNo,
432  double lowerCount,
433  double upperCount,
434  double &lowerValue,
435  double &upperValue,
436  const QgsRectangle &extent = QgsRectangle(),
437  int sampleSize = 0 );
438 
440  virtual void writeXml( QDomDocument &doc, QDomElement &parentElem ) const { Q_UNUSED( doc ); Q_UNUSED( parentElem ); }
442  virtual void readXml( const QDomElement &filterElem ) { Q_UNUSED( filterElem ); }
443 
444  protected:
445  // QgsRasterInterface used as input
446  QgsRasterInterface *mInput = nullptr;
447 
449  QList<QgsRasterBandStats> mStatistics;
450 
452  QList<QgsRasterHistogram> mHistograms;
453 
454  // On/off state, if off, it does not do anything, replicates input
455  bool mOn = true;
456 
461 #ifndef SIP_RUN
462  void initHistogram( QgsRasterHistogram &histogram,
463  int bandNo,
464  int binCount,
465  double minimum = std::numeric_limits<double>::quiet_NaN(),
466  double maximum = std::numeric_limits<double>::quiet_NaN(),
467  const QgsRectangle &boundingBox = QgsRectangle(),
468  int sampleSize = 0,
469  bool includeOutOfRange = false );
470 #else
471  void initHistogram( QgsRasterHistogram &histogram,
472  int bandNo,
473  int binCount,
474  SIP_PYOBJECT minimum = Py_None,
475  SIP_PYOBJECT maximum = Py_None,
476  const QgsRectangle &boundingBox = QgsRectangle(),
477  int sampleSize = 0,
478  bool includeOutOfRange = false )
479  [void ( QgsRasterHistogram & histogram,
480  int bandNo,
481  int binCount,
482  double minimum = 0.0,
483  double maximum = 0.0,
484  const QgsRectangle &boundingBox = QgsRectangle(),
485  int sampleSize = 0,
486  bool includeOutOfRange = false )];
487  % MethodCode
488  double minimum;
489  double maximum;
490  if ( a3 == Py_None )
491  {
492  minimum = std::numeric_limits<double>::quiet_NaN();
493  }
494  else
495  {
496  minimum = PyFloat_AsDouble( a3 );
497  }
498 
499  if ( a4 == Py_None )
500  {
501  maximum = std::numeric_limits<double>::quiet_NaN();
502  }
503  else
504  {
505  maximum = PyFloat_AsDouble( a4 );
506  }
507 
508 #if defined(SIP_PROTECTED_IS_PUBLIC)
509  sipCpp->initHistogram( *a0, a1, a2, minimum, maximum, *a5, a6, a7 );
510 #else
511  sipCpp->sipProtect_initHistogram( *a0, a1, a2, minimum, maximum, *a5, a6, a7 );
512 #endif
513  % End
514 #endif
515 
516 
517 
519  void initStatistics( QgsRasterBandStats &statistics, int bandNo,
520  int stats = QgsRasterBandStats::All,
521  const QgsRectangle &boundingBox = QgsRectangle(),
522  int binCount = 0 );
523 
524  private:
525 #ifdef SIP_RUN
527  QgsRasterInterface &operator=( const QgsRasterInterface & );
528 #endif
529 
530  Q_DISABLE_COPY( QgsRasterInterface ) // there is clone() for copying
531 };
532 
533 #endif
534 
535 
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:39
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
Get 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()
Get source / raw input, the first in pipe, usually provider.
virtual int xBlockSize() const
Get 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)
Set on/off.
QList< QgsRasterHistogram > mHistograms
List of cached histograms, all bands mixed.
#define SIP_END
Definition: qgis_sip.h:175
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
Is on/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
Get raster size.
virtual const QgsRasterInterface * sourceInput() const
Get source / raw input, the first in pipe, usually provider.
Base class for raster data providers.