QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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 
67  virtual void start() = 0;
68 
69  signals:
70 
75  void finish( const QImage &legend );
77  void progress( qint64 received, qint64 total );
79  void error( const QString &msg );
80 };
81 
82 
87 class CORE_EXPORT QgsRasterDataProvider : public QgsDataProvider, public QgsRasterInterface
88 {
89  Q_OBJECT
90 
91  public:
92 
98  {
99  NoProviderCapabilities = 0,
100  ReadLayerMetadata = 1 << 1,
101  WriteLayerMetadata = 1 << 2,
102  };
103 
105  Q_DECLARE_FLAGS( ProviderCapabilities, ProviderCapability )
106 
108 
117  QgsRasterDataProvider( const QString &uri, const QgsDataProvider::ProviderOptions &providerOptions = QgsDataProvider::ProviderOptions() );
118 
119  QgsRasterInterface *clone() const override = 0;
120 
125  virtual QgsRasterDataProvider::ProviderCapabilities providerCapabilities() const;
126 
127  /* It makes no sense to set input on provider */
128  bool setInput( QgsRasterInterface *input ) override { Q_UNUSED( input ) return false; }
129 
130  QgsRectangle extent() const override = 0;
131 
133  Qgis::DataType dataType( int bandNo ) const override = 0;
134 
139  Qgis::DataType sourceDataType( int bandNo ) const override = 0;
140 
142  virtual int colorInterpretation( int bandNo ) const
143  {
144  Q_UNUSED( bandNo )
146  }
147 
148  QString colorName( int colorInterpretation ) const
149  {
150  // Modified copy from GDAL
151  switch ( colorInterpretation )
152  {
154  return QStringLiteral( "Undefined" );
155 
157  return QStringLiteral( "Gray" );
158 
160  return QStringLiteral( "Palette" );
161 
162  case QgsRaster::RedBand:
163  return QStringLiteral( "Red" );
164 
166  return QStringLiteral( "Green" );
167 
168  case QgsRaster::BlueBand:
169  return QStringLiteral( "Blue" );
170 
172  return QStringLiteral( "Alpha" );
173 
174  case QgsRaster::HueBand:
175  return QStringLiteral( "Hue" );
176 
178  return QStringLiteral( "Saturation" );
179 
181  return QStringLiteral( "Lightness" );
182 
183  case QgsRaster::CyanBand:
184  return QStringLiteral( "Cyan" );
185 
187  return QStringLiteral( "Magenta" );
188 
190  return QStringLiteral( "Yellow" );
191 
193  return QStringLiteral( "Black" );
194 
196  return QStringLiteral( "YCbCr_Y" );
197 
199  return QStringLiteral( "YCbCr_Cb" );
200 
202  return QStringLiteral( "YCbCr_Cr" );
203 
204  default:
205  return QStringLiteral( "Unknown" );
206  }
207  }
209  virtual bool reload() { return true; }
210 
211  virtual QString colorInterpretationName( int bandNo ) const
212  {
213  return colorName( colorInterpretation( bandNo ) );
214  }
215 
220  virtual double bandScale( int bandNo ) const { Q_UNUSED( bandNo ) return 1.0; }
221 
226  virtual double bandOffset( int bandNo ) const { Q_UNUSED( bandNo ) return 0.0; }
227 
228  // TODO: remove or make protected all readBlock working with void*
229 
231  QgsRasterBlock *block( int bandNo, const QgsRectangle &boundingBox, int width, int height, QgsRasterBlockFeedback *feedback = nullptr ) override;
232 
234  virtual bool sourceHasNoDataValue( int bandNo ) const { return mSrcHasNoDataValue.value( bandNo - 1 ); }
235 
237  virtual bool useSourceNoDataValue( int bandNo ) const { return mUseSrcNoDataValue.value( bandNo - 1 ); }
238 
240  virtual void setUseSourceNoDataValue( int bandNo, bool use );
241 
243  virtual double sourceNoDataValue( int bandNo ) const { return mSrcNoDataValue.value( bandNo - 1 ); }
244 
245  virtual void setUserNoDataValue( int bandNo, const QgsRasterRangeList &noData );
246 
248  virtual QgsRasterRangeList userNoDataValues( int bandNo ) const { return mUserNoDataValue.value( bandNo - 1 ); }
249 
250  virtual QList<QgsColorRampShader::ColorRampItem> colorTable( int bandNo ) const
251  { Q_UNUSED( bandNo ) return QList<QgsColorRampShader::ColorRampItem>(); }
252 
257  QStringList subLayers() const override
258  {
259  return QStringList();
260  }
261 
263  virtual bool supportsLegendGraphic() const { return false; }
264 
276  virtual QImage getLegendGraphic( double scale = 0, bool forceRefresh = false, const QgsRectangle *visibleExtent = nullptr ) SIP_SKIP
277  {
278  Q_UNUSED( scale )
279  Q_UNUSED( forceRefresh )
280  Q_UNUSED( visibleExtent )
281  return QImage();
282  }
283 
298  {
299  Q_UNUSED( mapSettings )
300  return nullptr;
301  }
302 
304  virtual QString buildPyramids( const QList<QgsRasterPyramid> &pyramidList,
305  const QString &resamplingMethod = "NEAREST",
307  const QStringList &configOptions = QStringList(),
308  QgsRasterBlockFeedback *feedback = nullptr )
309  {
310  Q_UNUSED( pyramidList )
311  Q_UNUSED( resamplingMethod )
312  Q_UNUSED( format )
313  Q_UNUSED( configOptions )
314  Q_UNUSED( feedback )
315  return QStringLiteral( "FAILED_NOT_SUPPORTED" );
316  }
317 
326  virtual QList<QgsRasterPyramid> buildPyramidList( QList<int> overviewList = QList<int>() ) // clazy:exclude=function-args-by-ref
327  { Q_UNUSED( overviewList ) return QList<QgsRasterPyramid>(); }
328 
330  bool hasPyramids();
331 
336  virtual QString htmlMetadata() = 0;
337 
363  virtual QgsRasterIdentifyResult identify( const QgsPointXY &point, QgsRaster::IdentifyFormat format, const QgsRectangle &boundingBox = QgsRectangle(), int width = 0, int height = 0, int dpi = 96 );
364 
379  virtual double sample( const QgsPointXY &point, int band,
380  bool *ok SIP_OUT = nullptr,
381  const QgsRectangle &boundingBox = QgsRectangle(), int width = 0, int height = 0, int dpi = 96 );
382 
391  virtual QString lastErrorTitle() = 0;
392 
402  virtual QString lastError() = 0;
403 
405  virtual QString lastErrorFormat();
406 
408  int dpi() const { return mDpi; }
409 
411  void setDpi( int dpi ) { mDpi = dpi; }
412 
414  QDateTime timestamp() const override { return mTimestamp; }
415 
417  QDateTime dataTimestamp() const override { return QDateTime(); }
418 
425  virtual bool isEditable() const { return false; }
426 
437  virtual bool setEditable( bool enabled ) { Q_UNUSED( enabled ) return false; }
438 
440  // TODO: add data type (may be different from band type)
441  virtual bool write( void *data, int band, int width, int height, int xOffset, int yOffset )
442  {
443  Q_UNUSED( data )
444  Q_UNUSED( band )
445  Q_UNUSED( width )
446  Q_UNUSED( height )
447  Q_UNUSED( xOffset )
448  Q_UNUSED( yOffset )
449  return false;
450  }
451 
468  bool writeBlock( QgsRasterBlock *block, int band, int xOffset = 0, int yOffset = 0 );
469 
471  static QgsRasterDataProvider *create( const QString &providerKey,
472  const QString &uri,
473  const QString &format, int nBands,
474  Qgis::DataType type,
475  int width, int height, double *geoTransform,
477  const QStringList &createOptions = QStringList() );
478 
484  virtual bool setNoDataValue( int bandNo, double noDataValue ) { Q_UNUSED( bandNo ) Q_UNUSED( noDataValue ); return false; }
485 
487  virtual bool remove() { return false; }
488 
493  static QList<QPair<QString, QString> > pyramidResamplingMethods( const QString &providerKey );
494 
500  virtual QString validateCreationOptions( const QStringList &createOptions, const QString &format )
501  { Q_UNUSED( createOptions ) Q_UNUSED( format ); return QString(); }
502 
508  const QStringList &configOptions, const QString &fileFormat )
509  { Q_UNUSED( pyramidsFormat ) Q_UNUSED( configOptions ); Q_UNUSED( fileFormat ); return QString(); }
510 
511  static QString identifyFormatName( QgsRaster::IdentifyFormat format );
512  static QgsRaster::IdentifyFormat identifyFormatFromName( const QString &formatName );
513  static QString identifyFormatLabel( QgsRaster::IdentifyFormat format );
514  static Capability identifyFormatToCapability( QgsRaster::IdentifyFormat format );
515 
522 
529 
538  virtual QList< double > nativeResolutions() const;
539 
540  signals:
541 
546  void statusChanged( const QString & ) const;
547 
548  protected:
549 
555  virtual bool readBlock( int bandNo, int xBlock, int yBlock, void *data ) SIP_SKIP
556  { Q_UNUSED( bandNo ) Q_UNUSED( xBlock ); Q_UNUSED( yBlock ); Q_UNUSED( data ); return false; }
557 
563  virtual bool readBlock( int bandNo, QgsRectangle const &viewExtent, int width, int height, void *data, QgsRasterBlockFeedback *feedback = nullptr ) SIP_SKIP
564  { Q_UNUSED( bandNo ) Q_UNUSED( viewExtent ); Q_UNUSED( width ); Q_UNUSED( height ); Q_UNUSED( data ); Q_UNUSED( feedback ); return false; }
565 
567  bool userNoDataValuesContains( int bandNo, double value ) const;
568 
570  void copyBaseSettings( const QgsRasterDataProvider &other );
571 
576  int mDpi = -1;
577 
582  //bool hasNoDataValue ( int bandNo );
583 
585  QList<double> mSrcNoDataValue;
586 
588  QList<bool> mSrcHasNoDataValue;
589 
595  QList<bool> mUseSrcNoDataValue;
596 
601  QList< QgsRasterRangeList > mUserNoDataValue;
602 
604 
605 };
606 
607 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsRasterDataProvider::ProviderCapabilities )
608 
609 // clazy:excludeall=qstring-allocations
610 
611 #endif
IdentifyFormat
Definition: qgsraster.h:57
A rectangle specified with double values.
Definition: qgsrectangle.h:41
bool setInput(QgsRasterInterface *input) override
Set input.
Black band of CMLY image.
Definition: qgsraster.h:50
virtual bool readBlock(int bandNo, QgsRectangle const &viewExtent, int width, int height, void *data, QgsRasterBlockFeedback *feedback=nullptr)
Reads a block of raster data into data, using the given extent and size.
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:79
virtual QgsImageFetcher * getLegendGraphicFetcher(const QgsMapSettings *mapSettings)
Returns a new 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.
const QgsCoordinateReferenceSystem & crs
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 >())
Returns 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
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
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
Returns 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 bool readBlock(int bandNo, int xBlock, int yBlock, void *data)
Reads a block of raster data into data.
virtual int stepHeight() const
Step height for raster iterations.
Blue band of RGBA image.
Definition: qgsraster.h:42
Setting options for creating vector data providers.
QString colorName(int colorInterpretation) const
QList< QgsRasterRange > QgsRasterRangeList
virtual QgsRasterInterface * clone() const =0
Clone itself, create deep copy.
#define SIP_OUT
Definition: qgis_sip.h:51
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
Returns a 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
Returns the source nodata value usage.
RasterPyramidsFormat
Definition: qgsraster.h:81
Feedback object tailored for raster block reading.
static const int DEFAULT_MAXIMUM_TILE_WIDTH
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.