QGIS API Documentation  2.99.0-Master (f867b65)
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 
40 class CORE_EXPORT QgsRasterBlockFeedback : public QgsFeedback
41 {
42  public:
44  QgsRasterBlockFeedback( QObject *parent = nullptr ) : QgsFeedback( parent ), mPreviewOnly( false ), mRenderPartialOutput( false ) {}
45 
48  virtual void onNewData() {}
49 
53  bool isPreviewOnly() const { return mPreviewOnly; }
56  void setPreviewOnly( bool preview ) { mPreviewOnly = preview; }
57 
60  bool renderPartialOutput() const { return mRenderPartialOutput; }
63  void setRenderPartialOutput( bool enable ) { mRenderPartialOutput = enable; }
64 
65  private:
68  bool mPreviewOnly;
69 
71  bool mRenderPartialOutput;
72 };
73 
74 
78 class CORE_EXPORT QgsRasterInterface
79 {
80 #ifdef SIP_RUN
81 // QgsRasterInterface subclasses
83 #include <qgshuesaturationfilter.h>
84 #include <qgsrasterdataprovider.h>
85 #include <qgsrasternuller.h>
86 #include <qgsrasterprojector.h>
87 #include <qgsrasterrenderer.h>
89 
90 // QgsRasterRenderer subclasses
91 #include <qgshillshaderenderer.h>
97 #endif
98 
99 
100 #ifdef SIP_RUN
102  if ( dynamic_cast<QgsBrightnessContrastFilter *>( sipCpp ) )
103  sipType = sipType_QgsBrightnessContrastFilter;
104  else if ( dynamic_cast<QgsHueSaturationFilter *>( sipCpp ) )
105  sipType = sipType_QgsHueSaturationFilter;
106  else if ( dynamic_cast<QgsRasterDataProvider *>( sipCpp ) )
107  {
108  sipType = sipType_QgsRasterDataProvider;
109  // use static cast because QgsRasterDataProvider has multiple inheritance
110  // and we would end up with bad pointer otherwise!
111  *sipCppRet = static_cast<QgsRasterDataProvider *>( sipCpp );
112  }
113  else if ( dynamic_cast<QgsRasterNuller *>( sipCpp ) )
114  sipType = sipType_QgsRasterNuller;
115  else if ( dynamic_cast<QgsRasterProjector *>( sipCpp ) )
116  sipType = sipType_QgsRasterProjector;
117  else if ( dynamic_cast<QgsRasterRenderer *>( sipCpp ) )
118  {
119  if ( dynamic_cast<QgsHillshadeRenderer *>( sipCpp ) )
120  sipType = sipType_QgsHillshadeRenderer;
121  else if ( dynamic_cast<QgsMultiBandColorRenderer *>( sipCpp ) )
122  sipType = sipType_QgsMultiBandColorRenderer;
123  else if ( dynamic_cast<QgsPalettedRasterRenderer *>( sipCpp ) )
124  sipType = sipType_QgsPalettedRasterRenderer;
125  else if ( dynamic_cast<QgsSingleBandColorDataRenderer *>( sipCpp ) )
126  sipType = sipType_QgsSingleBandColorDataRenderer;
127  else if ( dynamic_cast<QgsSingleBandGrayRenderer *>( sipCpp ) )
128  sipType = sipType_QgsSingleBandGrayRenderer;
129  else if ( dynamic_cast<QgsSingleBandPseudoColorRenderer *>( sipCpp ) )
130  sipType = sipType_QgsSingleBandPseudoColorRenderer;
131  else
132  sipType = sipType_QgsRasterRenderer;
133  }
134  else if ( dynamic_cast<QgsRasterResampleFilter *>( sipCpp ) )
135  sipType = sipType_QgsRasterResampleFilter;
136  else
137  sipType = 0;
138  SIP_END
139 #endif
140 
141  Q_DECLARE_TR_FUNCTIONS( QgsRasterInterface )
142 
143  public:
146  {
147  NoCapabilities = 0,
148  Size = 1 << 1, // original data source size (and thus resolution) is known, it is not always available, for example for WMS
149  Create = 1 << 2, // create new datasets
150  Remove = 1 << 3, // delete datasets
151  BuildPyramids = 1 << 4, // supports building of pyramids (overviews)
152  Identify = 1 << 5, // at least one identify format supported
153  IdentifyValue = 1 << 6, // numerical values
154  IdentifyText = 1 << 7, // WMS text
155  IdentifyHtml = 1 << 8, // WMS HTML
156  IdentifyFeature = 1 << 9, // WMS GML -> feature
157  };
158 
159  QgsRasterInterface( QgsRasterInterface *input = nullptr );
160 
161  virtual ~QgsRasterInterface() = default;
162 
164  virtual QgsRasterInterface *clone() const = 0 SIP_FACTORY;
165 
167  virtual int capabilities() const
168  {
170  }
171 
175  QString capabilitiesString() const;
176 
178  virtual Qgis::DataType dataType( int bandNo ) const = 0;
179 
182  virtual Qgis::DataType sourceDataType( int bandNo ) const { return mInput ? mInput->sourceDataType( bandNo ) : Qgis::UnknownDataType; }
183 
188  virtual QgsRectangle extent() const { return mInput ? mInput->extent() : QgsRectangle(); }
189 
190  int dataTypeSize( int bandNo ) { return QgsRasterBlock::typeSize( dataType( bandNo ) ); }
191 
193  virtual int bandCount() const = 0;
194 
196  virtual int xBlockSize() const { return mInput ? mInput->xBlockSize() : 0; }
197  virtual int yBlockSize() const { return mInput ? mInput->yBlockSize() : 0; }
198 
200  virtual int xSize() const { return mInput ? mInput->xSize() : 0; }
201  virtual int ySize() const { return mInput ? mInput->ySize() : 0; }
202 
204  virtual QString generateBandName( int bandNumber ) const
205  {
206  return tr( "Band" ) + QStringLiteral( " %1" ) .arg( bandNumber, 1 + static_cast< int >( log10( static_cast< double >( bandCount() ) ) ), 10, QChar( '0' ) );
207  }
208 
218  virtual QgsRasterBlock *block( int bandNo, const QgsRectangle &extent, int width, int height, QgsRasterBlockFeedback *feedback = nullptr ) = 0 SIP_FACTORY;
219 
222  virtual bool setInput( QgsRasterInterface *input ) { mInput = input; return true; }
223 
225  virtual QgsRasterInterface *input() const { return mInput; }
226 
228  virtual bool on() const { return mOn; }
229 
231  virtual void setOn( bool on ) { mOn = on; }
232 
238  virtual const QgsRasterInterface *sourceInput() const SIP_SKIP
239  {
240  QgsDebugMsgLevel( "Entered", 4 );
241  return mInput ? mInput->sourceInput() : this;
242  }
243 
249  {
250  QgsDebugMsgLevel( "Entered", 4 );
251  return mInput ? mInput->sourceInput() : this;
252  }
253 
262  virtual QgsRasterBandStats bandStatistics( int bandNo,
263  int stats = QgsRasterBandStats::All,
264  const QgsRectangle &extent = QgsRectangle(),
265  int sampleSize = 0, QgsRasterBlockFeedback *feedback = nullptr );
266 
270  virtual bool hasStatistics( int bandNo,
271  int stats = QgsRasterBandStats::All,
272  const QgsRectangle &extent = QgsRectangle(),
273  int sampleSize = 0 );
274 
275 
288 #ifndef SIP_RUN
289  virtual QgsRasterHistogram histogram( int bandNo,
290  int binCount = 0,
291  double minimum = std::numeric_limits<double>::quiet_NaN(),
292  double maximum = std::numeric_limits<double>::quiet_NaN(),
293  const QgsRectangle &extent = QgsRectangle(),
294  int sampleSize = 0,
295  bool includeOutOfRange = false,
296  QgsRasterBlockFeedback *feedback = nullptr );
297 #else
298  virtual QgsRasterHistogram histogram( int bandNo,
299  int binCount = 0,
300  SIP_PYOBJECT minimum = Py_None,
301  SIP_PYOBJECT maximum = Py_None,
302  const QgsRectangle &extent = QgsRectangle(),
303  int sampleSize = 0,
304  bool includeOutOfRange = false,
305  QgsRasterBlockFeedback *feedback = nullptr )
306  [QgsRasterHistogram( int bandNo,
307  int binCount = 0,
308  double minimum = 0.0,
309  double maximum = 0.0,
310  const QgsRectangle &extent = QgsRectangle(),
311  int sampleSize = 0,
312  bool includeOutOfRange = false,
313  QgsRasterBlockFeedback *feedback = nullptr )];
314  % MethodCode
315  double minimum;
316  double maximum;
317  if ( a2 == Py_None )
318  {
319  minimum = std::numeric_limits<double>::quiet_NaN();
320  }
321  else
322  {
323  minimum = PyFloat_AsDouble( a2 );
324  }
325 
326  if ( a3 == Py_None )
327  {
328  maximum = std::numeric_limits<double>::quiet_NaN();
329  }
330  else
331  {
332  maximum = PyFloat_AsDouble( a3 );
333  }
334 
335  QgsRasterHistogram h = sipCpp->histogram( a0, a1, minimum, maximum, *a4, a5, a6, a7 );
336  sipRes = &h;
337  % End
338 #endif
339 
340 
344 #ifndef SIP_RUN
345  virtual bool hasHistogram( int bandNo,
346  int binCount,
347  double minimum = std::numeric_limits<double>::quiet_NaN(),
348  double maximum = std::numeric_limits<double>::quiet_NaN(),
349  const QgsRectangle &extent = QgsRectangle(),
350  int sampleSize = 0,
351  bool includeOutOfRange = false );
352 #else
353  virtual bool hasHistogram( int bandNo,
354  int binCount,
355  SIP_PYOBJECT minimum = Py_None,
356  SIP_PYOBJECT maximum = Py_None,
357  const QgsRectangle &extent = QgsRectangle(),
358  int sampleSize = 0,
359  bool includeOutOfRange = false )
360  [bool( int bandNo,
361  int binCount,
362  double minimum = 0.0,
363  double maximum = 0.0,
364  const QgsRectangle &extent = QgsRectangle(),
365  int sampleSize = 0,
366  bool includeOutOfRange = false )];
367  % MethodCode
368  double minimum;
369  double maximum;
370  if ( a2 == Py_None )
371  {
372  minimum = std::numeric_limits<double>::quiet_NaN();
373  }
374  else
375  {
376  minimum = PyFloat_AsDouble( a2 );
377  }
378 
379  if ( a3 == Py_None )
380  {
381  maximum = std::numeric_limits<double>::quiet_NaN();
382  }
383  else
384  {
385  maximum = PyFloat_AsDouble( a3 );
386  }
387 
388  sipRes = sipCpp->hasHistogram( a0, a1, minimum, maximum, *a4, a5, a6 );
389  % End
390 #endif
391 
392 
402  virtual void cumulativeCut( int bandNo,
403  double lowerCount,
404  double upperCount,
405  double &lowerValue,
406  double &upperValue,
407  const QgsRectangle &extent = QgsRectangle(),
408  int sampleSize = 0 );
409 
411  virtual void writeXml( QDomDocument &doc, QDomElement &parentElem ) const { Q_UNUSED( doc ); Q_UNUSED( parentElem ); }
413  virtual void readXml( const QDomElement &filterElem ) { Q_UNUSED( filterElem ); }
414 
415  protected:
416  // QgsRasterInterface used as input
417  QgsRasterInterface *mInput = nullptr;
418 
420  QList<QgsRasterBandStats> mStatistics;
421 
423  QList<QgsRasterHistogram> mHistograms;
424 
425  // On/off state, if off, it does not do anything, replicates input
426  bool mOn;
427 
431 #ifndef SIP_RUN
432  void initHistogram( QgsRasterHistogram &histogram,
433  int bandNo,
434  int binCount,
435  double minimum = std::numeric_limits<double>::quiet_NaN(),
436  double maximum = std::numeric_limits<double>::quiet_NaN(),
437  const QgsRectangle &boundingBox = QgsRectangle(),
438  int sampleSize = 0,
439  bool includeOutOfRange = false );
440 #else
441  void initHistogram( QgsRasterHistogram &histogram,
442  int bandNo,
443  int binCount,
444  SIP_PYOBJECT minimum = Py_None,
445  SIP_PYOBJECT maximum = Py_None,
446  const QgsRectangle &boundingBox = QgsRectangle(),
447  int sampleSize = 0,
448  bool includeOutOfRange = false )
449  [void ( QgsRasterHistogram & histogram,
450  int bandNo,
451  int binCount,
452  double minimum = 0.0,
453  double maximum = 0.0,
454  const QgsRectangle &boundingBox = QgsRectangle(),
455  int sampleSize = 0,
456  bool includeOutOfRange = false )];
457  % MethodCode
458  double minimum;
459  double maximum;
460  if ( a3 == Py_None )
461  {
462  minimum = std::numeric_limits<double>::quiet_NaN();
463  }
464  else
465  {
466  minimum = PyFloat_AsDouble( a3 );
467  }
468 
469  if ( a4 == Py_None )
470  {
471  maximum = std::numeric_limits<double>::quiet_NaN();
472  }
473  else
474  {
475  maximum = PyFloat_AsDouble( a4 );
476  }
477 
478 #if defined(SIP_PROTECTED_IS_PUBLIC)
479  sipCpp->initHistogram( *a0, a1, a2, minimum, maximum, *a5, a6, a7 );
480 #else
481  sipCpp->sipProtect_initHistogram( *a0, a1, a2, minimum, maximum, *a5, a6, a7 );
482 #endif
483  % End
484 #endif
485 
486 
487 
489  void initStatistics( QgsRasterBandStats &statistics, int bandNo,
490  int stats = QgsRasterBandStats::All,
491  const QgsRectangle &boundingBox = QgsRectangle(),
492  int binCount = 0 );
493 
494  private:
495 #ifdef SIP_RUN
497  QgsRasterInterface &operator=( const QgsRasterInterface & );
498 #endif
499 
500  Q_DISABLE_COPY( QgsRasterInterface ) // there is clone() for copying
501 };
502 
503 #endif
504 
505 
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:38
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:74
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:43
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:38
#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:76
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.