QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
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,
187  Create = 1 << 2,
188  Remove = 1 << 3,
189  BuildPyramids = 1 << 4,
190  Identify = 1 << 5,
191  IdentifyValue = 1 << 6,
192  IdentifyText = 1 << 7,
193  IdentifyHtml = 1 << 8,
194  IdentifyFeature = 1 << 9,
195  Prefetch = 1 << 10,
196  };
197 
198  QgsRasterInterface( QgsRasterInterface *input = nullptr );
199 
200  virtual ~QgsRasterInterface() = default;
201 
203  virtual QgsRasterInterface *clone() const = 0 SIP_FACTORY;
204 
206  virtual int capabilities() const
207  {
209  }
210 
214  QString capabilitiesString() const;
215 
217  virtual Qgis::DataType dataType( int bandNo ) const = 0;
218 
223  virtual Qgis::DataType sourceDataType( int bandNo ) const { return mInput ? mInput->sourceDataType( bandNo ) : Qgis::UnknownDataType; }
224 
229  virtual QgsRectangle extent() const { return mInput ? mInput->extent() : QgsRectangle(); }
230 
231  int dataTypeSize( int bandNo ) { return QgsRasterBlock::typeSize( dataType( bandNo ) ); }
232 
234  virtual int bandCount() const = 0;
235 
237  virtual int xBlockSize() const { return mInput ? mInput->xBlockSize() : 0; }
238  virtual int yBlockSize() const { return mInput ? mInput->yBlockSize() : 0; }
239 
241  virtual int xSize() const { return mInput ? mInput->xSize() : 0; }
242  virtual int ySize() const { return mInput ? mInput->ySize() : 0; }
243 
245  virtual QString generateBandName( int bandNumber ) const;
246 
252  virtual QString colorInterpretationName( int bandNumber ) const;
253 
259  QString displayBandName( int bandNumber ) const;
260 
271  virtual QgsRasterBlock *block( int bandNo, const QgsRectangle &extent, int width, int height, QgsRasterBlockFeedback *feedback = nullptr ) = 0 SIP_FACTORY;
272 
277  virtual bool setInput( QgsRasterInterface *input ) { mInput = input; return true; }
278 
280  virtual QgsRasterInterface *input() const { return mInput; }
281 
283  virtual bool on() const { return mOn; }
284 
286  virtual void setOn( bool on ) { mOn = on; }
287 
294  virtual const QgsRasterInterface *sourceInput() const SIP_SKIP
295  {
296  QgsDebugMsgLevel( QStringLiteral( "Entered" ), 4 );
297  return mInput ? mInput->sourceInput() : this;
298  }
299 
306  {
307  QgsDebugMsgLevel( QStringLiteral( "Entered" ), 4 );
308  return mInput ? mInput->sourceInput() : this;
309  }
310 
319  virtual QgsRasterBandStats bandStatistics( int bandNo,
320  int stats = QgsRasterBandStats::All,
321  const QgsRectangle &extent = QgsRectangle(),
322  int sampleSize = 0, QgsRasterBlockFeedback *feedback = nullptr );
323 
328  virtual bool hasStatistics( int bandNo,
329  int stats = QgsRasterBandStats::All,
330  const QgsRectangle &extent = QgsRectangle(),
331  int sampleSize = 0 );
332 
333 
347 #ifndef SIP_RUN
348  virtual QgsRasterHistogram histogram( int bandNo,
349  int binCount = 0,
350  double minimum = std::numeric_limits<double>::quiet_NaN(),
351  double maximum = std::numeric_limits<double>::quiet_NaN(),
352  const QgsRectangle &extent = QgsRectangle(),
353  int sampleSize = 0,
354  bool includeOutOfRange = false,
355  QgsRasterBlockFeedback *feedback = nullptr );
356 #else
357  virtual QgsRasterHistogram histogram( int bandNo,
358  int binCount = 0,
359  SIP_PYOBJECT minimum = Py_None,
360  SIP_PYOBJECT maximum = Py_None,
361  const QgsRectangle &extent = QgsRectangle(),
362  int sampleSize = 0,
363  bool includeOutOfRange = false,
364  QgsRasterBlockFeedback *feedback = nullptr )
365  [QgsRasterHistogram( int bandNo,
366  int binCount = 0,
367  double minimum = 0.0,
368  double maximum = 0.0,
369  const QgsRectangle &extent = QgsRectangle(),
370  int sampleSize = 0,
371  bool includeOutOfRange = false,
372  QgsRasterBlockFeedback *feedback = nullptr )];
373  % MethodCode
374  double minimum;
375  double maximum;
376  if ( a2 == Py_None )
377  {
378  minimum = std::numeric_limits<double>::quiet_NaN();
379  }
380  else
381  {
382  minimum = PyFloat_AsDouble( a2 );
383  }
384 
385  if ( a3 == Py_None )
386  {
387  maximum = std::numeric_limits<double>::quiet_NaN();
388  }
389  else
390  {
391  maximum = PyFloat_AsDouble( a3 );
392  }
393 
394  QgsRasterHistogram *h = new QgsRasterHistogram( sipCpp->histogram( a0, a1, minimum, maximum, *a4, a5, a6, a7 ) );
395  return sipConvertFromType( h, sipType_QgsRasterHistogram, Py_None );
396  % End
397 #endif
398 
399 
404 #ifndef SIP_RUN
405  virtual bool hasHistogram( int bandNo,
406  int binCount,
407  double minimum = std::numeric_limits<double>::quiet_NaN(),
408  double maximum = std::numeric_limits<double>::quiet_NaN(),
409  const QgsRectangle &extent = QgsRectangle(),
410  int sampleSize = 0,
411  bool includeOutOfRange = false );
412 #else
413  virtual bool hasHistogram( int bandNo,
414  int binCount,
415  SIP_PYOBJECT minimum = Py_None,
416  SIP_PYOBJECT maximum = Py_None,
417  const QgsRectangle &extent = QgsRectangle(),
418  int sampleSize = 0,
419  bool includeOutOfRange = false )
420  [bool( int bandNo,
421  int binCount,
422  double minimum = 0.0,
423  double maximum = 0.0,
424  const QgsRectangle &extent = QgsRectangle(),
425  int sampleSize = 0,
426  bool includeOutOfRange = false )];
427  % MethodCode
428  double minimum;
429  double maximum;
430  if ( a2 == Py_None )
431  {
432  minimum = std::numeric_limits<double>::quiet_NaN();
433  }
434  else
435  {
436  minimum = PyFloat_AsDouble( a2 );
437  }
438 
439  if ( a3 == Py_None )
440  {
441  maximum = std::numeric_limits<double>::quiet_NaN();
442  }
443  else
444  {
445  maximum = PyFloat_AsDouble( a3 );
446  }
447 
448  sipRes = sipCpp->hasHistogram( a0, a1, minimum, maximum, *a4, a5, a6 );
449  % End
450 #endif
451 
452 
463  virtual void cumulativeCut( int bandNo,
464  double lowerCount,
465  double upperCount,
466  double &lowerValue,
467  double &upperValue,
468  const QgsRectangle &extent = QgsRectangle(),
469  int sampleSize = 0 );
470 
472  virtual void writeXml( QDomDocument &doc, QDomElement &parentElem ) const { Q_UNUSED( doc ) Q_UNUSED( parentElem ); }
474  virtual void readXml( const QDomElement &filterElem ) { Q_UNUSED( filterElem ) }
475 
476  protected:
477  // QgsRasterInterface used as input
478  QgsRasterInterface *mInput = nullptr;
479 
481  QList<QgsRasterBandStats> mStatistics;
482 
484  QList<QgsRasterHistogram> mHistograms;
485 
486  // On/off state, if off, it does not do anything, replicates input
487  bool mOn = true;
488 
493 #ifndef SIP_RUN
494  void initHistogram( QgsRasterHistogram &histogram,
495  int bandNo,
496  int binCount,
497  double minimum = std::numeric_limits<double>::quiet_NaN(),
498  double maximum = std::numeric_limits<double>::quiet_NaN(),
499  const QgsRectangle &boundingBox = QgsRectangle(),
500  int sampleSize = 0,
501  bool includeOutOfRange = false );
502 #else
503  void initHistogram( QgsRasterHistogram &histogram,
504  int bandNo,
505  int binCount,
506  SIP_PYOBJECT minimum = Py_None,
507  SIP_PYOBJECT maximum = Py_None,
508  const QgsRectangle &boundingBox = QgsRectangle(),
509  int sampleSize = 0,
510  bool includeOutOfRange = false )
511  [void ( QgsRasterHistogram & histogram,
512  int bandNo,
513  int binCount,
514  double minimum = 0.0,
515  double maximum = 0.0,
516  const QgsRectangle &boundingBox = QgsRectangle(),
517  int sampleSize = 0,
518  bool includeOutOfRange = false )];
519  % MethodCode
520  double minimum;
521  double maximum;
522  if ( a3 == Py_None )
523  {
524  minimum = std::numeric_limits<double>::quiet_NaN();
525  }
526  else
527  {
528  minimum = PyFloat_AsDouble( a3 );
529  }
530 
531  if ( a4 == Py_None )
532  {
533  maximum = std::numeric_limits<double>::quiet_NaN();
534  }
535  else
536  {
537  maximum = PyFloat_AsDouble( a4 );
538  }
539 
540 #if defined(SIP_PROTECTED_IS_PUBLIC)
541  sipCpp->initHistogram( *a0, a1, a2, minimum, maximum, *a5, a6, a7 );
542 #else
543  sipCpp->sipProtect_initHistogram( *a0, a1, a2, minimum, maximum, *a5, a6, a7 );
544 #endif
545  % End
546 #endif
547 
548 
549 
551  void initStatistics( QgsRasterBandStats &statistics, int bandNo,
552  int stats = QgsRasterBandStats::All,
553  const QgsRectangle &boundingBox = QgsRectangle(),
554  int binCount = 0 );
555 
556  private:
557 #ifdef SIP_RUN
559  QgsRasterInterface &operator=( const QgsRasterInterface & );
560 #endif
561 
562  Q_DISABLE_COPY( QgsRasterInterface ) // there is clone() for copying
563 };
564 
565 #endif
566 
567 
DataType
Raster data types.
Definition: qgis.h:102
@ UnknownDataType
Unknown or unspecified type.
Definition: qgis.h:103
Brightness/contrast and gamma correction filter pipe for rasters.
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition: qgsfeedback.h:45
A renderer for generating live hillshade models.
Color and saturation filter pipe for rasters.
Renderer for multiband images with the color components.
Renderer for paletted raster images.
The RasterBandStats struct is a container for statistics about a single raster band.
Feedback object tailored for raster block reading.
bool isPreviewOnly() const
Whether the raster provider should return only data that are already available without waiting for fu...
QStringList errors() const
Returns a list of any errors encountered while retrieving the raster block.
virtual void onNewData()
May be emitted by raster data provider to indicate that some partial data are available and a new pre...
QgsRasterBlockFeedback(QObject *parent=nullptr)
Construct a new raster block feedback object.
void setPreviewOnly(bool preview)
set flag whether the block request is for preview purposes only
bool renderPartialOutput() const
Whether our painter is drawing to a temporary image used just by this layer.
void appendError(const QString &error)
Appends an error message to the stored list of errors.
void setRenderPartialOutput(bool enable)
Set whether our painter is drawing to a temporary image used just by this layer.
Raster data container.
static int typeSize(int dataType) SIP_HOLDGIL
Returns the size in bytes for the specified dataType.
Base class for raster data providers.
The QgsRasterHistogram is a container for histogram of a single raster band.
Base class for processing filters like renderers, reprojector, resampler etc.
virtual QgsRasterBlock * block(int bandNo, const QgsRectangle &extent, int width, int height, QgsRasterBlockFeedback *feedback=nullptr)=0
Read block of data using given extent and size.
virtual QgsRasterInterface * clone() const =0
Clone itself, create deep copy.
virtual int yBlockSize() const
int dataTypeSize(int bandNo)
virtual QgsRasterInterface * sourceInput()
Gets source / raw input, the first in pipe, usually provider.
virtual Qgis::DataType dataType(int bandNo) const =0
Returns data type for the band specified by number.
QList< QgsRasterBandStats > mStatistics
List of cached statistics, all bands mixed.
Capability
If you add to this, please also add to capabilitiesString()
virtual QgsRasterInterface * input() const
Current input.
virtual int xSize() const
Gets raster size.
virtual void setOn(bool on)
Sets whether the interface is on or off.
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...
virtual int xBlockSize() const
Gets block size.
virtual const QgsRasterInterface * sourceInput() const
Gets source / raw input, the first in pipe, usually provider.
virtual int bandCount() const =0
Gets number of bands.
virtual bool on() const
Returns whether the interface is on or off.
virtual ~QgsRasterInterface()=default
virtual void readXml(const QDomElement &filterElem)
Sets base class members from xml. Usually called from create() methods of subclasses.
virtual int capabilities() const
Returns a bitmask containing the supported capabilities.
virtual int ySize() const
virtual QgsRectangle extent() const
Gets the extent of the interface.
QList< QgsRasterHistogram > mHistograms
List of cached histograms, all bands mixed.
virtual void writeXml(QDomDocument &doc, QDomElement &parentElem) const
Write base class members to xml.
virtual bool setInput(QgsRasterInterface *input)
Set input.
Raster pipe that deals with null values.
QgsRasterProjector implements approximate projection support for it calculates grid of points in sour...
Raster renderer pipe that applies colors to a raster.
Resample filter pipe for rasters.
A rectangle specified with double values.
Definition: qgsrectangle.h:42
Raster renderer pipe for single band color.
Raster renderer pipe for single band gray.
Raster renderer pipe for single band pseudocolor.
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:177
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_FACTORY
Definition: qgis_sip.h:76
#define SIP_END
Definition: qgis_sip.h:194
#define QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:39