QGIS API Documentation  3.0.2-Girona (307d082)
qgsrasterdataprovider.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsrasterdataprovider.h - DataProvider Interface for raster layers
3  --------------------------------------
4  Date : Mar 11, 2005
5  Copyright : (C) 2005 by Brendan Morley
6  email : morb at ozemail dot com dot au
7 
8  async legend fetcher : Sandro Santilli < strk at keybit dot net >
9 
10  ***************************************************************************/
11 
12 /***************************************************************************
13  * *
14  * This program is free software; you can redistribute it and/or modify *
15  * it under the terms of the GNU General Public License as published by *
16  * the Free Software Foundation; either version 2 of the License, or *
17  * (at your option) any later version. *
18  * *
19  ***************************************************************************/
20 
21 /* Thank you to Marco Hugentobler for the original vector DataProvider */
22 
23 #ifndef QGSRASTERDATAPROVIDER_H
24 #define QGSRASTERDATAPROVIDER_H
25 
26 #include "qgis_core.h"
27 #include "qgis_sip.h"
28 #include <cmath>
29 
30 #include <QDateTime>
31 #include <QVariant>
32 #include <QImage>
33 
34 #include "qgscolorrampshader.h"
35 #include "qgsdataprovider.h"
36 #include "qgsfields.h"
37 #include "qgsraster.h"
38 #include "qgsrasterinterface.h"
39 #include "qgsrasterpyramid.h"
40 #include "qgsrasterrange.h"
41 #include "qgsrectangle.h"
42 #include "qgsrasteriterator.h"
43 
44 class QImage;
45 class QByteArray;
46 
47 class QgsPointXY;
49 class QgsMapSettings;
50 
56 class CORE_EXPORT QgsImageFetcher : public QObject
57 {
58  Q_OBJECT
59  public:
61  QgsImageFetcher( QObject *parent = nullptr ) : QObject( parent ) {}
62 
66  virtual void start() = 0;
67 
68  signals:
69 
73  void finish( const QImage &legend );
75  void progress( qint64 received, qint64 total );
77  void error( const QString &msg );
78 };
79 
80 
85 class CORE_EXPORT QgsRasterDataProvider : public QgsDataProvider, public QgsRasterInterface
86 {
87  Q_OBJECT
88 
89  public:
90 
96  {
97  NoProviderCapabilities = 0,
98  ReadLayerMetadata = 1 << 1,
99  WriteLayerMetadata = 1 << 2,
100  };
101 
103  Q_DECLARE_FLAGS( ProviderCapabilities, ProviderCapability )
104 
106 
107  QgsRasterDataProvider( const QString &uri );
108 
109  QgsRasterInterface *clone() const override = 0;
110 
115  virtual QgsRasterDataProvider::ProviderCapabilities providerCapabilities() const;
116 
117  /* It makes no sense to set input on provider */
118  bool setInput( QgsRasterInterface *input ) override { Q_UNUSED( input ); return false; }
119 
120  QgsRectangle extent() const override = 0;
121 
123  Qgis::DataType dataType( int bandNo ) const override = 0;
124 
129  Qgis::DataType sourceDataType( int bandNo ) const override = 0;
130 
132  virtual int colorInterpretation( int bandNo ) const
133  {
134  Q_UNUSED( bandNo );
136  }
137 
138  QString colorName( int colorInterpretation ) const
139  {
140  // Modified copy from GDAL
141  switch ( colorInterpretation )
142  {
144  return QStringLiteral( "Undefined" );
145 
147  return QStringLiteral( "Gray" );
148 
150  return QStringLiteral( "Palette" );
151 
152  case QgsRaster::RedBand:
153  return QStringLiteral( "Red" );
154 
156  return QStringLiteral( "Green" );
157 
158  case QgsRaster::BlueBand:
159  return QStringLiteral( "Blue" );
160 
162  return QStringLiteral( "Alpha" );
163 
164  case QgsRaster::HueBand:
165  return QStringLiteral( "Hue" );
166 
168  return QStringLiteral( "Saturation" );
169 
171  return QStringLiteral( "Lightness" );
172 
173  case QgsRaster::CyanBand:
174  return QStringLiteral( "Cyan" );
175 
177  return QStringLiteral( "Magenta" );
178 
180  return QStringLiteral( "Yellow" );
181 
183  return QStringLiteral( "Black" );
184 
186  return QStringLiteral( "YCbCr_Y" );
187 
189  return QStringLiteral( "YCbCr_Cb" );
190 
192  return QStringLiteral( "YCbCr_Cr" );
193 
194  default:
195  return QStringLiteral( "Unknown" );
196  }
197  }
199  virtual bool reload() { return true; }
200 
201  virtual QString colorInterpretationName( int bandNo ) const
202  {
203  return colorName( colorInterpretation( bandNo ) );
204  }
205 
210  virtual double bandScale( int bandNo ) const { Q_UNUSED( bandNo ); return 1.0; }
211 
216  virtual double bandOffset( int bandNo ) const { Q_UNUSED( bandNo ); return 0.0; }
217 
218  // TODO: remove or make protected all readBlock working with void*
219 
221  QgsRasterBlock *block( int bandNo, const QgsRectangle &boundingBox, int width, int height, QgsRasterBlockFeedback *feedback = nullptr ) override;
222 
224  virtual bool sourceHasNoDataValue( int bandNo ) const { return mSrcHasNoDataValue.value( bandNo - 1 ); }
225 
227  virtual bool useSourceNoDataValue( int bandNo ) const { return mUseSrcNoDataValue.value( bandNo - 1 ); }
228 
230  virtual void setUseSourceNoDataValue( int bandNo, bool use );
231 
233  virtual double sourceNoDataValue( int bandNo ) const { return mSrcNoDataValue.value( bandNo - 1 ); }
234 
235  virtual void setUserNoDataValue( int bandNo, const QgsRasterRangeList &noData );
236 
238  virtual QgsRasterRangeList userNoDataValues( int bandNo ) const { return mUserNoDataValue.value( bandNo - 1 ); }
239 
240  virtual QList<QgsColorRampShader::ColorRampItem> colorTable( int bandNo ) const
241  { Q_UNUSED( bandNo ); return QList<QgsColorRampShader::ColorRampItem>(); }
242 
246  QStringList subLayers() const override
247  {
248  return QStringList();
249  }
250 
252  virtual bool supportsLegendGraphic() const { return false; }
253 
264  virtual QImage getLegendGraphic( double scale = 0, bool forceRefresh = false, const QgsRectangle *visibleExtent = nullptr ) SIP_SKIP
265  {
266  Q_UNUSED( scale );
267  Q_UNUSED( forceRefresh );
268  Q_UNUSED( visibleExtent );
269  return QImage();
270  }
271 
286  {
287  Q_UNUSED( mapSettings );
288  return nullptr;
289  }
290 
292  virtual QString buildPyramids( const QList<QgsRasterPyramid> &pyramidList,
293  const QString &resamplingMethod = "NEAREST",
295  const QStringList &configOptions = QStringList(),
296  QgsRasterBlockFeedback *feedback = nullptr )
297  {
298  Q_UNUSED( pyramidList );
299  Q_UNUSED( resamplingMethod );
300  Q_UNUSED( format );
301  Q_UNUSED( configOptions );
302  Q_UNUSED( feedback );
303  return QStringLiteral( "FAILED_NOT_SUPPORTED" );
304  }
305 
314  virtual QList<QgsRasterPyramid> buildPyramidList( QList<int> overviewList = QList<int>() ) // clazy:exclude=function-args-by-ref
315  { Q_UNUSED( overviewList ); return QList<QgsRasterPyramid>(); }
316 
318  bool hasPyramids();
319 
324  virtual QString htmlMetadata() = 0;
325 
350  //virtual QMap<int, QVariant> identify( const QgsPointXY & point, QgsRaster::IdentifyFormat format, const QgsRectangle &extent = QgsRectangle(), int width = 0, int height = 0 );
351  virtual QgsRasterIdentifyResult identify( const QgsPointXY &point, QgsRaster::IdentifyFormat format, const QgsRectangle &boundingBox = QgsRectangle(), int width = 0, int height = 0, int dpi = 96 );
352 
361  virtual QString lastErrorTitle() = 0;
362 
372  virtual QString lastError() = 0;
373 
375  virtual QString lastErrorFormat();
376 
378  int dpi() const { return mDpi; }
379 
381  void setDpi( int dpi ) { mDpi = dpi; }
382 
384  QDateTime timestamp() const override { return mTimestamp; }
385 
387  QDateTime dataTimestamp() const override { return QDateTime(); }
388 
395  virtual bool isEditable() const { return false; }
396 
407  virtual bool setEditable( bool enabled ) { Q_UNUSED( enabled ); return false; }
408 
410  // TODO: add data type (may be defferent from band type)
411  virtual bool write( void *data, int band, int width, int height, int xOffset, int yOffset )
412  {
413  Q_UNUSED( data );
414  Q_UNUSED( band );
415  Q_UNUSED( width );
416  Q_UNUSED( height );
417  Q_UNUSED( xOffset );
418  Q_UNUSED( yOffset );
419  return false;
420  }
421 
438  bool writeBlock( QgsRasterBlock *block, int band, int xOffset = 0, int yOffset = 0 );
439 
441  static QgsRasterDataProvider *create( const QString &providerKey,
442  const QString &uri,
443  const QString &format, int nBands,
444  Qgis::DataType type,
445  int width, int height, double *geoTransform,
446  const QgsCoordinateReferenceSystem &crs,
447  const QStringList &createOptions = QStringList() );
448 
454  virtual bool setNoDataValue( int bandNo, double noDataValue ) { Q_UNUSED( bandNo ); Q_UNUSED( noDataValue ); return false; }
455 
457  virtual bool remove() { return false; }
458 
463  static QList<QPair<QString, QString> > pyramidResamplingMethods( const QString &providerKey );
464 
470  virtual QString validateCreationOptions( const QStringList &createOptions, const QString &format )
471  { Q_UNUSED( createOptions ); Q_UNUSED( format ); return QString(); }
472 
478  const QStringList &configOptions, const QString &fileFormat )
479  { Q_UNUSED( pyramidsFormat ); Q_UNUSED( configOptions ); Q_UNUSED( fileFormat ); return QString(); }
480 
481  static QString identifyFormatName( QgsRaster::IdentifyFormat format );
482  static QgsRaster::IdentifyFormat identifyFormatFromName( const QString &formatName );
483  static QString identifyFormatLabel( QgsRaster::IdentifyFormat format );
484  static Capability identifyFormatToCapability( QgsRaster::IdentifyFormat format );
485 
492 
499 
500  signals:
501 
506  void statusChanged( const QString & ) const;
507 
508  protected:
509 
514  virtual void readBlock( int bandNo, int xBlock, int yBlock, void *data ) SIP_SKIP
515  { Q_UNUSED( bandNo ); Q_UNUSED( xBlock ); Q_UNUSED( yBlock ); Q_UNUSED( data ); }
516 
521  virtual void readBlock( int bandNo, QgsRectangle const &viewExtent, int width, int height, void *data, QgsRasterBlockFeedback *feedback = nullptr ) SIP_SKIP
522  { Q_UNUSED( bandNo ); Q_UNUSED( viewExtent ); Q_UNUSED( width ); Q_UNUSED( height ); Q_UNUSED( data ); Q_UNUSED( feedback ); }
523 
525  bool userNoDataValuesContains( int bandNo, double value ) const;
526 
528  void copyBaseSettings( const QgsRasterDataProvider &other );
529 
531  static QStringList cStringList2Q_( char **stringList ) SIP_SKIP;
532 
536  int mDpi = -1;
537 
541  //bool hasNoDataValue ( int bandNo );
542 
544  QList<double> mSrcNoDataValue;
545 
547  QList<bool> mSrcHasNoDataValue;
548 
553  QList<bool> mUseSrcNoDataValue;
554 
558  QList< QgsRasterRangeList > mUserNoDataValue;
559 
561 
562 };
563 
564 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsRasterDataProvider::ProviderCapabilities )
565 
566 // clazy:excludeall=qstring-allocations
567 
568 #endif
virtual void readBlock(int bandNo, int xBlock, int yBlock, void *data)
Read block of data.
IdentifyFormat
Definition: qgsraster.h:57
A rectangle specified with double values.
Definition: qgsrectangle.h:39
bool setInput(QgsRasterInterface *input) override
Set input.
Black band of CMLY image.
Definition: qgsraster.h:50
Handles asynchronous download of images.
virtual QList< QgsColorRampShader::ColorRampItem > colorTable(int bandNo) const
virtual double bandOffset(int bandNo) const
Read band offset for raster value.
A class to represent a 2D point.
Definition: qgspointxy.h:43
QDateTime mTimestamp
Timestamp of data in the moment when the data were loaded by provider.
DataType
Raster data types.
Definition: qgis.h:91
virtual QgsImageFetcher * getLegendGraphicFetcher(const QgsMapSettings *mapSettings)
Get an image downloader for the raster legend.
Red band of RGBA image.
Definition: qgsraster.h:40
Capability
If you add to this, please also add to capabilitiesString()
virtual QImage getLegendGraphic(double scale=0, bool forceRefresh=false, const QgsRectangle *visibleExtent=nullptr)
Returns the legend rendered as pixmap.
virtual bool supportsLegendGraphic() const
Returns whether the provider supplies a legend graphic.
virtual bool setNoDataValue(int bandNo, double noDataValue)
Set no data value on created dataset.
Abstract base class for spatial data provider implementations.
virtual Qgis::DataType dataType(int bandNo) const =0
Returns data type for the band specified by number.
virtual QString buildPyramids(const QList< QgsRasterPyramid > &pyramidList, const QString &resamplingMethod="NEAREST", QgsRaster::RasterPyramidsFormat format=QgsRaster::PyramidsGTiff, const QStringList &configOptions=QStringList(), QgsRasterBlockFeedback *feedback=nullptr)
Create pyramid overviews.
Hue band of HLS image.
Definition: qgsraster.h:44
virtual double sourceNoDataValue(int bandNo) const
Value representing no data value.
virtual QString validatePyramidsConfigOptions(QgsRaster::RasterPyramidsFormat pyramidsFormat, const QStringList &configOptions, const QString &fileFormat)
Validates pyramid creation options for a specific dataset and destination format. ...
The QgsMapSettings class contains configuration for rendering of the map.
QDateTime timestamp() const override
Time stamp of data source in the moment when data/metadata were loaded by provider.
Raster identify results container.
ProviderCapability
Enumeration with capabilities that raster providers might implement.
Lightness band of HLS image.
Definition: qgsraster.h:46
int dpi() const
Returns the dpi of the output device.
virtual bool setEditable(bool enabled)
Turns on/off editing mode of the provider.
Paletted (see associated color table)
Definition: qgsraster.h:39
Alpha (0=transparent, 255=opaque)
Definition: qgsraster.h:43
Raster data container.
Green band of RGBA image.
Definition: qgsraster.h:41
virtual QList< QgsRasterPyramid > buildPyramidList(QList< int > overviewList=QList< int >())
Accessor for the raster layers pyramid list.
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.
#define SIP_SKIP
Definition: qgis_sip.h:119
Y Luminance.
Definition: qgsraster.h:51
static const int DEFAULT_MAXIMUM_TILE_HEIGHT
virtual bool reload()
Reload data (data could change)
QList< bool > mSrcHasNoDataValue
Source no data value exists.
virtual QString colorInterpretationName(int bandNo) 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...
#define SIP_FACTORY
Definition: qgis_sip.h:69
virtual void readBlock(int bandNo, QgsRectangle const &viewExtent, int width, int height, void *data, QgsRasterBlockFeedback *feedback=nullptr)
Read block of data using give extent and size.
Base class for processing filters like renderers, reprojector, resampler etc.
virtual bool isEditable() const
Checks whether the provider is in editing mode, i.e.
virtual bool sourceHasNoDataValue(int bandNo) const
Return true if source band has no data value.
Saturation band of HLS image.
Definition: qgsraster.h:45
QList< double > mSrcNoDataValue
Source no data value is available and is set to be used or internal no data is available.
virtual int stepHeight() const
Step height for raster iterations.
Blue band of RGBA image.
Definition: qgsraster.h:42
QString colorName(int colorInterpretation) const
QList< QgsRasterRange > QgsRasterRangeList
virtual QgsRasterInterface * clone() const =0
Clone itself, create deep copy.
This class represents a coordinate reference system (CRS).
Greyscale.
Definition: qgsraster.h:38
QStringList subLayers() const override
Returns the sublayers of this layer - useful for providers that manage their own layers, such as WMS.
virtual QgsRasterRangeList userNoDataValues(int bandNo) const
Get list of user no data value ranges.
virtual int stepWidth() const
Step width for raster iterations.
Magenta band of CMYK image.
Definition: qgsraster.h:48
virtual QString validateCreationOptions(const QStringList &createOptions, const QString &format)
Validates creation options for a specific dataset and destination format.
virtual QgsRectangle extent() const =0
Returns the extent of the layer.
virtual bool write(void *data, int band, int width, int height, int xOffset, int yOffset)
Writes into the provider datasource.
QDateTime dataTimestamp() const override
Current time stamp of data source.
virtual bool useSourceNoDataValue(int bandNo) const
Get source nodata value usage.
RasterPyramidsFormat
Definition: qgsraster.h:81
Feedback object tailored for raster block reading.
static const int DEFAULT_MAXIMUM_TILE_WIDTH
void setDpi(int dpi)
Sets the output device resolution.
QgsImageFetcher(QObject *parent=nullptr)
Constructor.
Cyan band of CMYK image.
Definition: qgsraster.h:47
virtual int colorInterpretation(int bandNo) const
Returns data type for the band specified by number.
Base class for raster data providers.
QList< QgsRasterRangeList > mUserNoDataValue
List of lists of user defined additional no data values for each band, indexed from 0...
QList< bool > mUseSrcNoDataValue
Use source nodata value.
Yellow band of CMYK image.
Definition: qgsraster.h:49
virtual double bandScale(int bandNo) const
Read band scale for raster value.