QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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 
87  void appendError( const QString &error ) { mErrors.append( error ); }
88 
95  QStringList errors() const { return mErrors; }
96 
97  private:
98 
103  bool mPreviewOnly = false;
104 
106  bool mRenderPartialOutput = false;
107 
109  QStringList mErrors;
110 };
111 
112 
117 class CORE_EXPORT QgsRasterInterface
118 {
119 #ifdef SIP_RUN
120 // QgsRasterInterface subclasses
122 #include <qgshuesaturationfilter.h>
123 #include <qgsrasterdataprovider.h>
124 #include <qgsrasternuller.h>
125 #include <qgsrasterprojector.h>
126 #include <qgsrasterrenderer.h>
127 #include <qgsrasterresamplefilter.h>
128 
129 // QgsRasterRenderer subclasses
130 #include <qgshillshaderenderer.h>
136 #endif
137 
138 
139 #ifdef SIP_RUN
141  if ( dynamic_cast<QgsBrightnessContrastFilter *>( sipCpp ) )
142  sipType = sipType_QgsBrightnessContrastFilter;
143  else if ( dynamic_cast<QgsHueSaturationFilter *>( sipCpp ) )
144  sipType = sipType_QgsHueSaturationFilter;
145  else if ( dynamic_cast<QgsRasterDataProvider *>( sipCpp ) )
146  {
147  sipType = sipType_QgsRasterDataProvider;
148  // use static cast because QgsRasterDataProvider has multiple inheritance
149  // and we would end up with bad pointer otherwise!
150  *sipCppRet = static_cast<QgsRasterDataProvider *>( sipCpp );
151  }
152  else if ( dynamic_cast<QgsRasterNuller *>( sipCpp ) )
153  sipType = sipType_QgsRasterNuller;
154  else if ( dynamic_cast<QgsRasterProjector *>( sipCpp ) )
155  sipType = sipType_QgsRasterProjector;
156  else if ( dynamic_cast<QgsRasterRenderer *>( sipCpp ) )
157  {
158  if ( dynamic_cast<QgsHillshadeRenderer *>( sipCpp ) )
159  sipType = sipType_QgsHillshadeRenderer;
160  else if ( dynamic_cast<QgsMultiBandColorRenderer *>( sipCpp ) )
161  sipType = sipType_QgsMultiBandColorRenderer;
162  else if ( dynamic_cast<QgsPalettedRasterRenderer *>( sipCpp ) )
163  sipType = sipType_QgsPalettedRasterRenderer;
164  else if ( dynamic_cast<QgsSingleBandColorDataRenderer *>( sipCpp ) )
165  sipType = sipType_QgsSingleBandColorDataRenderer;
166  else if ( dynamic_cast<QgsSingleBandGrayRenderer *>( sipCpp ) )
167  sipType = sipType_QgsSingleBandGrayRenderer;
168  else if ( dynamic_cast<QgsSingleBandPseudoColorRenderer *>( sipCpp ) )
169  sipType = sipType_QgsSingleBandPseudoColorRenderer;
170  else
171  sipType = sipType_QgsRasterRenderer;
172  }
173  else if ( dynamic_cast<QgsRasterResampleFilter *>( sipCpp ) )
174  sipType = sipType_QgsRasterResampleFilter;
175  else
176  sipType = 0;
177  SIP_END
178 #endif
179 
180  Q_DECLARE_TR_FUNCTIONS( QgsRasterInterface )
181 
182  public:
185  {
186  NoCapabilities = 0,
187  Size = 1 << 1, // original data source size (and thus resolution) is known, it is not always available, for example for WMS
188  Create = 1 << 2, // create new datasets
189  Remove = 1 << 3, // delete datasets
190  BuildPyramids = 1 << 4, // supports building of pyramids (overviews)
191  Identify = 1 << 5, // at least one identify format supported
192  IdentifyValue = 1 << 6, // numerical values
193  IdentifyText = 1 << 7, // WMS text
194  IdentifyHtml = 1 << 8, // WMS HTML
195  IdentifyFeature = 1 << 9, // WMS GML -> feature
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 
222  virtual Qgis::DataType sourceDataType( int bandNo ) const { return mInput ? mInput->sourceDataType( bandNo ) : Qgis::UnknownDataType; }
223 
228  virtual QgsRectangle extent() const { return mInput ? mInput->extent() : QgsRectangle(); }
229 
230  int dataTypeSize( int bandNo ) { return QgsRasterBlock::typeSize( dataType( bandNo ) ); }
231 
233  virtual int bandCount() const = 0;
234 
236  virtual int xBlockSize() const { return mInput ? mInput->xBlockSize() : 0; }
237  virtual int yBlockSize() const { return mInput ? mInput->yBlockSize() : 0; }
238 
240  virtual int xSize() const { return mInput ? mInput->xSize() : 0; }
241  virtual int ySize() const { return mInput ? mInput->ySize() : 0; }
242 
244  virtual QString generateBandName( int bandNumber ) const
245  {
246  return tr( "Band" ) + QStringLiteral( " %1" ) .arg( bandNumber, 1 + static_cast< int >( std::log10( static_cast< double >( bandCount() ) ) ), 10, QChar( '0' ) );
247  }
248 
259  virtual QgsRasterBlock *block( int bandNo, const QgsRectangle &extent, int width, int height, QgsRasterBlockFeedback *feedback = nullptr ) = 0 SIP_FACTORY;
260 
264  virtual bool setInput( QgsRasterInterface *input ) { mInput = input; return true; }
265 
267  virtual QgsRasterInterface *input() const { return mInput; }
268 
270  virtual bool on() const { return mOn; }
271 
273  virtual void setOn( bool on ) { mOn = on; }
274 
281  virtual const QgsRasterInterface *sourceInput() const SIP_SKIP
282  {
283  QgsDebugMsgLevel( QStringLiteral( "Entered" ), 4 );
284  return mInput ? mInput->sourceInput() : this;
285  }
286 
293  {
294  QgsDebugMsgLevel( QStringLiteral( "Entered" ), 4 );
295  return mInput ? mInput->sourceInput() : this;
296  }
297 
306  virtual QgsRasterBandStats bandStatistics( int bandNo,
307  int stats = QgsRasterBandStats::All,
308  const QgsRectangle &extent = QgsRectangle(),
309  int sampleSize = 0, QgsRasterBlockFeedback *feedback = nullptr );
310 
315  virtual bool hasStatistics( int bandNo,
316  int stats = QgsRasterBandStats::All,
317  const QgsRectangle &extent = QgsRectangle(),
318  int sampleSize = 0 );
319 
320 
334 #ifndef SIP_RUN
335  virtual QgsRasterHistogram histogram( int bandNo,
336  int binCount = 0,
337  double minimum = std::numeric_limits<double>::quiet_NaN(),
338  double maximum = std::numeric_limits<double>::quiet_NaN(),
339  const QgsRectangle &extent = QgsRectangle(),
340  int sampleSize = 0,
341  bool includeOutOfRange = false,
342  QgsRasterBlockFeedback *feedback = nullptr );
343 #else
344  virtual QgsRasterHistogram histogram( int bandNo,
345  int binCount = 0,
346  SIP_PYOBJECT minimum = Py_None,
347  SIP_PYOBJECT maximum = Py_None,
348  const QgsRectangle &extent = QgsRectangle(),
349  int sampleSize = 0,
350  bool includeOutOfRange = false,
351  QgsRasterBlockFeedback *feedback = nullptr )
352  [QgsRasterHistogram( int bandNo,
353  int binCount = 0,
354  double minimum = 0.0,
355  double maximum = 0.0,
356  const QgsRectangle &extent = QgsRectangle(),
357  int sampleSize = 0,
358  bool includeOutOfRange = false,
359  QgsRasterBlockFeedback *feedback = nullptr )];
360  % MethodCode
361  double minimum;
362  double maximum;
363  if ( a2 == Py_None )
364  {
365  minimum = std::numeric_limits<double>::quiet_NaN();
366  }
367  else
368  {
369  minimum = PyFloat_AsDouble( a2 );
370  }
371 
372  if ( a3 == Py_None )
373  {
374  maximum = std::numeric_limits<double>::quiet_NaN();
375  }
376  else
377  {
378  maximum = PyFloat_AsDouble( a3 );
379  }
380 
381  QgsRasterHistogram *h = new QgsRasterHistogram( sipCpp->histogram( a0, a1, minimum, maximum, *a4, a5, a6, a7 ) );
382  return sipConvertFromType( h, sipType_QgsRasterHistogram, Py_None );
383  % End
384 #endif
385 
386 
391 #ifndef SIP_RUN
392  virtual bool hasHistogram( int bandNo,
393  int binCount,
394  double minimum = std::numeric_limits<double>::quiet_NaN(),
395  double maximum = std::numeric_limits<double>::quiet_NaN(),
396  const QgsRectangle &extent = QgsRectangle(),
397  int sampleSize = 0,
398  bool includeOutOfRange = false );
399 #else
400  virtual bool hasHistogram( int bandNo,
401  int binCount,
402  SIP_PYOBJECT minimum = Py_None,
403  SIP_PYOBJECT maximum = Py_None,
404  const QgsRectangle &extent = QgsRectangle(),
405  int sampleSize = 0,
406  bool includeOutOfRange = false )
407  [bool( int bandNo,
408  int binCount,
409  double minimum = 0.0,
410  double maximum = 0.0,
411  const QgsRectangle &extent = QgsRectangle(),
412  int sampleSize = 0,
413  bool includeOutOfRange = false )];
414  % MethodCode
415  double minimum;
416  double maximum;
417  if ( a2 == Py_None )
418  {
419  minimum = std::numeric_limits<double>::quiet_NaN();
420  }
421  else
422  {
423  minimum = PyFloat_AsDouble( a2 );
424  }
425 
426  if ( a3 == Py_None )
427  {
428  maximum = std::numeric_limits<double>::quiet_NaN();
429  }
430  else
431  {
432  maximum = PyFloat_AsDouble( a3 );
433  }
434 
435  sipRes = sipCpp->hasHistogram( a0, a1, minimum, maximum, *a4, a5, a6 );
436  % End
437 #endif
438 
439 
450  virtual void cumulativeCut( int bandNo,
451  double lowerCount,
452  double upperCount,
453  double &lowerValue,
454  double &upperValue,
455  const QgsRectangle &extent = QgsRectangle(),
456  int sampleSize = 0 );
457 
459  virtual void writeXml( QDomDocument &doc, QDomElement &parentElem ) const { Q_UNUSED( doc ); Q_UNUSED( parentElem ); }
461  virtual void readXml( const QDomElement &filterElem ) { Q_UNUSED( filterElem ); }
462 
463  protected:
464  // QgsRasterInterface used as input
465  QgsRasterInterface *mInput = nullptr;
466 
468  QList<QgsRasterBandStats> mStatistics;
469 
471  QList<QgsRasterHistogram> mHistograms;
472 
473  // On/off state, if off, it does not do anything, replicates input
474  bool mOn = true;
475 
480 #ifndef SIP_RUN
481  void initHistogram( QgsRasterHistogram &histogram,
482  int bandNo,
483  int binCount,
484  double minimum = std::numeric_limits<double>::quiet_NaN(),
485  double maximum = std::numeric_limits<double>::quiet_NaN(),
486  const QgsRectangle &boundingBox = QgsRectangle(),
487  int sampleSize = 0,
488  bool includeOutOfRange = false );
489 #else
490  void initHistogram( QgsRasterHistogram &histogram,
491  int bandNo,
492  int binCount,
493  SIP_PYOBJECT minimum = Py_None,
494  SIP_PYOBJECT maximum = Py_None,
495  const QgsRectangle &boundingBox = QgsRectangle(),
496  int sampleSize = 0,
497  bool includeOutOfRange = false )
498  [void ( QgsRasterHistogram & histogram,
499  int bandNo,
500  int binCount,
501  double minimum = 0.0,
502  double maximum = 0.0,
503  const QgsRectangle &boundingBox = QgsRectangle(),
504  int sampleSize = 0,
505  bool includeOutOfRange = false )];
506  % MethodCode
507  double minimum;
508  double maximum;
509  if ( a3 == Py_None )
510  {
511  minimum = std::numeric_limits<double>::quiet_NaN();
512  }
513  else
514  {
515  minimum = PyFloat_AsDouble( a3 );
516  }
517 
518  if ( a4 == Py_None )
519  {
520  maximum = std::numeric_limits<double>::quiet_NaN();
521  }
522  else
523  {
524  maximum = PyFloat_AsDouble( a4 );
525  }
526 
527 #if defined(SIP_PROTECTED_IS_PUBLIC)
528  sipCpp->initHistogram( *a0, a1, a2, minimum, maximum, *a5, a6, a7 );
529 #else
530  sipCpp->sipProtect_initHistogram( *a0, a1, a2, minimum, maximum, *a5, a6, a7 );
531 #endif
532  % End
533 #endif
534 
535 
536 
538  void initStatistics( QgsRasterBandStats &statistics, int bandNo,
539  int stats = QgsRasterBandStats::All,
540  const QgsRectangle &boundingBox = QgsRectangle(),
541  int binCount = 0 );
542 
543  private:
544 #ifdef SIP_RUN
546  QgsRasterInterface &operator=( const QgsRasterInterface & );
547 #endif
548 
549  Q_DISABLE_COPY( QgsRasterInterface ) // there is clone() for copying
550 };
551 
552 #endif
553 
554 
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.
virtual int xBlockSize() const
Gets block size.
virtual int yBlockSize() const
DataType
Raster data types.
Definition: qgis.h:92
Capability
If you add to this, please also add to capabilitiesString()
virtual QgsRasterInterface * input() const
Current input.
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 ySize() const
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...
Base class for feedback objects to be used for cancellation of something running in a worker thread...
Definition: qgsfeedback.h:44
The RasterBandStats struct is a container for statistics about a single raster band.
QStringList errors() const
Returns a list of any errors encountered while retrieving the raster block.
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 void setOn(bool on)
Sets whether the interface is on or off.
QList< QgsRasterHistogram > mHistograms
List of cached histograms, all bands mixed.
void appendError(const QString &error)
Appends an error message to the stored list of errors.
virtual void writeXml(QDomDocument &doc, QDomElement &parentElem) const
Write base class members to xml.
#define SIP_END
Definition: qgis_sip.h:182
bool renderPartialOutput() const
Whether our painter is drawing to a temporary image used just by this layer.
Unknown or unspecified type.
Definition: qgis.h:94
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 int capabilities() const
Returns a bitmask containing the supported capabilities.
virtual QString generateBandName(int bandNumber) const
helper function to create zero padded band names
virtual bool setInput(QgsRasterInterface *input)
Set input.
virtual int xSize() const
Gets raster size.
virtual QgsRectangle extent() const
Gets the extent of the interface.
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:165
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 const QgsRasterInterface * sourceInput() const
Gets source / raw input, the first in pipe, usually provider.
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...
virtual bool on() const
Returns whether the interface is on or off.
Base class for raster data providers.