QGIS API Documentation  3.20.0-Odense (decaadbb31)
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 "qgsraster.h"
37 #include "qgsfields.h"
38 #include "qgsrasterinterface.h"
39 #include "qgsrasterpyramid.h"
40 #include "qgsrasterrange.h"
41 #include "qgsrectangle.h"
42 #include "qgsrasteriterator.h"
44 
45 class QImage;
46 class QByteArray;
47 
48 class QgsPointXY;
50 class QgsMapSettings;
51 
57 class CORE_EXPORT QgsImageFetcher : public QObject
58 {
59  Q_OBJECT
60  public:
62  QgsImageFetcher( QObject *parent = nullptr ) : QObject( parent ) {}
63 
68  virtual void start() = 0;
69 
70  signals:
71 
76  void finish( const QImage &legend );
78  void progress( qint64 received, qint64 total );
80  void error( const QString &msg );
81 };
82 
83 
88 class CORE_EXPORT QgsRasterDataProvider : public QgsDataProvider, public QgsRasterInterface
89 {
90  Q_OBJECT
91 
92  public:
93 
99  {
100  NoProviderCapabilities = 0,
101  ReadLayerMetadata = 1 << 1,
102  WriteLayerMetadata = 1 << 2,
103  ProviderHintBenefitsFromResampling = 1 << 3,
104  ProviderHintCanPerformProviderResampling = 1 << 4,
105  ReloadData = 1 << 5,
106  DpiDependentData = 1 << 6,
107  };
108 
110  Q_DECLARE_FLAGS( ProviderCapabilities, ProviderCapability )
111 
113 
122  QgsRasterDataProvider( const QString &uri,
124  QgsDataProvider::ReadFlags flags = QgsDataProvider::ReadFlags() );
125 
126  QgsRasterDataProvider *clone() const override = 0;
127 
132  virtual QgsRasterDataProvider::ProviderCapabilities providerCapabilities() const;
133 
134  /* It makes no sense to set input on provider */
135  bool setInput( QgsRasterInterface *input ) override { Q_UNUSED( input ) return false; }
136 
137  QgsRectangle extent() const override = 0;
138 
140  Qgis::DataType dataType( int bandNo ) const override = 0;
141 
147  virtual QgsFields fields() const { return QgsFields(); };
148 
153  Qgis::DataType sourceDataType( int bandNo ) const override = 0;
154 
156  virtual int colorInterpretation( int bandNo ) const;
157 
158  QString colorName( int colorInterpretation ) const
159  {
160  // Modified copy from GDAL
161  switch ( colorInterpretation )
162  {
164  return QStringLiteral( "Undefined" );
165 
167  return QStringLiteral( "Gray" );
168 
170  return QStringLiteral( "Palette" );
171 
172  case QgsRaster::RedBand:
173  return QStringLiteral( "Red" );
174 
176  return QStringLiteral( "Green" );
177 
178  case QgsRaster::BlueBand:
179  return QStringLiteral( "Blue" );
180 
182  return QStringLiteral( "Alpha" );
183 
184  case QgsRaster::HueBand:
185  return QStringLiteral( "Hue" );
186 
188  return QStringLiteral( "Saturation" );
189 
191  return QStringLiteral( "Lightness" );
192 
193  case QgsRaster::CyanBand:
194  return QStringLiteral( "Cyan" );
195 
197  return QStringLiteral( "Magenta" );
198 
200  return QStringLiteral( "Yellow" );
201 
203  return QStringLiteral( "Black" );
204 
206  return QStringLiteral( "YCbCr_Y" );
207 
209  return QStringLiteral( "YCbCr_Cb" );
210 
212  return QStringLiteral( "YCbCr_Cr" );
213 
214  default:
215  return QStringLiteral( "Unknown" );
216  }
217  }
219  virtual bool reload() { return true; }
220 
221  QString colorInterpretationName( int bandNo ) const override;
222 
227  virtual double bandScale( int bandNo ) const { Q_UNUSED( bandNo ) return 1.0; }
228 
233  virtual double bandOffset( int bandNo ) const { Q_UNUSED( bandNo ) return 0.0; }
234 
235  // TODO: remove or make protected all readBlock working with void*
236 
238  QgsRasterBlock *block( int bandNo, const QgsRectangle &boundingBox, int width, int height, QgsRasterBlockFeedback *feedback = nullptr ) override SIP_FACTORY;
239 
241  virtual bool sourceHasNoDataValue( int bandNo ) const { return mSrcHasNoDataValue.value( bandNo - 1 ); }
242 
244  virtual bool useSourceNoDataValue( int bandNo ) const { return mUseSrcNoDataValue.value( bandNo - 1 ); }
245 
247  virtual void setUseSourceNoDataValue( int bandNo, bool use );
248 
250  virtual double sourceNoDataValue( int bandNo ) const { return mSrcNoDataValue.value( bandNo - 1 ); }
251 
252  virtual void setUserNoDataValue( int bandNo, const QgsRasterRangeList &noData );
253 
255  virtual QgsRasterRangeList userNoDataValues( int bandNo ) const { return mUserNoDataValue.value( bandNo - 1 ); }
256 
257  virtual QList<QgsColorRampShader::ColorRampItem> colorTable( int bandNo ) const
258  { Q_UNUSED( bandNo ) return QList<QgsColorRampShader::ColorRampItem>(); }
259 
264  QStringList subLayers() const override
265  {
266  return QStringList();
267  }
268 
269  QgsRasterDataProviderTemporalCapabilities *temporalCapabilities() override;
270  const QgsRasterDataProviderTemporalCapabilities *temporalCapabilities() const override SIP_SKIP;
271 
273  virtual bool supportsLegendGraphic() const { return false; }
274 
286  virtual QImage getLegendGraphic( double scale = 0, bool forceRefresh = false, const QgsRectangle *visibleExtent = nullptr ) SIP_SKIP
287  {
288  Q_UNUSED( scale )
289  Q_UNUSED( forceRefresh )
290  Q_UNUSED( visibleExtent )
291  return QImage();
292  }
293 
308  {
309  Q_UNUSED( mapSettings )
310  return nullptr;
311  }
312 
329  virtual QString buildPyramids( const QList<QgsRasterPyramid> &pyramidList,
330  const QString &resamplingMethod = "NEAREST",
332  const QStringList &configOptions = QStringList(),
333  QgsRasterBlockFeedback *feedback = nullptr )
334  {
335  Q_UNUSED( pyramidList )
336  Q_UNUSED( resamplingMethod )
337  Q_UNUSED( format )
338  Q_UNUSED( configOptions )
339  Q_UNUSED( feedback )
340  return QStringLiteral( "FAILED_NOT_SUPPORTED" );
341  }
342 
361  virtual QList<QgsRasterPyramid> buildPyramidList( const QList<int> &overviewList = QList<int>() )
362  { Q_UNUSED( overviewList ) return QList<QgsRasterPyramid>(); }
363 
373  bool hasPyramids();
374 
379  virtual QString htmlMetadata() = 0;
380 
405  virtual QgsRasterIdentifyResult identify( const QgsPointXY &point, QgsRaster::IdentifyFormat format, const QgsRectangle &boundingBox = QgsRectangle(), int width = 0, int height = 0, int dpi = 96 );
406 
421  virtual double sample( const QgsPointXY &point, int band,
422  bool *ok SIP_OUT = nullptr,
423  const QgsRectangle &boundingBox = QgsRectangle(), int width = 0, int height = 0, int dpi = 96 );
424 
433  virtual QString lastErrorTitle() = 0;
434 
444  virtual QString lastError() = 0;
445 
447  virtual QString lastErrorFormat();
448 
450  int dpi() const { return mDpi; }
451 
453  void setDpi( int dpi ) { mDpi = dpi; }
454 
456  QDateTime timestamp() const override { return mTimestamp; }
457 
459  QDateTime dataTimestamp() const override { return QDateTime(); }
460 
467  virtual bool isEditable() const { return false; }
468 
479  virtual bool setEditable( bool enabled ) { Q_UNUSED( enabled ) return false; }
480 
481  // TODO: add data type (may be different from band type)
482 
484  virtual bool write( void *data, int band, int width, int height, int xOffset, int yOffset )
485  {
486  Q_UNUSED( data )
487  Q_UNUSED( band )
488  Q_UNUSED( width )
489  Q_UNUSED( height )
490  Q_UNUSED( xOffset )
491  Q_UNUSED( yOffset )
492  return false;
493  }
494 
511  bool writeBlock( QgsRasterBlock *block, int band, int xOffset = 0, int yOffset = 0 );
512 
514  static QgsRasterDataProvider *create( const QString &providerKey,
515  const QString &uri,
516  const QString &format, int nBands,
517  Qgis::DataType type,
518  int width, int height, double *geoTransform,
520  const QStringList &createOptions = QStringList() );
521 
527  virtual bool setNoDataValue( int bandNo, double noDataValue ) { Q_UNUSED( bandNo ) Q_UNUSED( noDataValue ); return false; }
528 
530  virtual bool remove() { return false; }
531 
536  static QList<QPair<QString, QString> > pyramidResamplingMethods( const QString &providerKey );
537 
543  virtual QString validateCreationOptions( const QStringList &createOptions, const QString &format )
544  { Q_UNUSED( createOptions ) Q_UNUSED( format ); return QString(); }
545 
551  const QStringList &configOptions, const QString &fileFormat )
552  { Q_UNUSED( pyramidsFormat ) Q_UNUSED( configOptions ); Q_UNUSED( fileFormat ); return QString(); }
553 
554  static QString identifyFormatName( QgsRaster::IdentifyFormat format );
555  static QgsRaster::IdentifyFormat identifyFormatFromName( const QString &formatName );
556  static QString identifyFormatLabel( QgsRaster::IdentifyFormat format );
557  static Capability identifyFormatToCapability( QgsRaster::IdentifyFormat format );
558 
565 
572 
581  virtual QList< double > nativeResolutions() const;
582 
589  virtual bool ignoreExtents() const;
590 
596  {
599  };
600 
613  virtual QgsPoint transformCoordinates( const QgsPoint &point, TransformType type );
614 
615 
622  virtual bool enableProviderResampling( bool enable ) { Q_UNUSED( enable ); return false; }
623 
636  bool isProviderResamplingEnabled() const { return mProviderResamplingEnabled; }
637 
642  enum class ResamplingMethod
643  {
644  Nearest,
645  Bilinear,
646  Cubic,
647  CubicSpline,
648  Lanczos,
649  Average,
650  Mode,
651  Gauss
652  };
653 
660  virtual bool setZoomedInResamplingMethod( ResamplingMethod method ) { Q_UNUSED( method ); return false; }
661 
666  ResamplingMethod zoomedInResamplingMethod() const { return mZoomedInResamplingMethod; }
667 
674  virtual bool setZoomedOutResamplingMethod( ResamplingMethod method ) { Q_UNUSED( method ); return false; }
675 
680  ResamplingMethod zoomedOutResamplingMethod() const { return mZoomedOutResamplingMethod; }
681 
688  virtual bool setMaxOversampling( double factor ) { Q_UNUSED( factor ); return false; }
689 
694  double maxOversampling() const { return mMaxOversampling; }
695 
696  void readXml( const QDomElement &filterElem ) override;
697 
698  void writeXml( QDomDocument &doc, QDomElement &parentElem ) const override;
699 
700  signals:
701 
706  void statusChanged( const QString & ) const;
707 
708  protected:
709 
715  virtual bool readBlock( int bandNo, int xBlock, int yBlock, void *data ) SIP_SKIP
716  { Q_UNUSED( bandNo ) Q_UNUSED( xBlock ); Q_UNUSED( yBlock ); Q_UNUSED( data ); return false; }
717 
723  virtual bool readBlock( int bandNo, QgsRectangle const &viewExtent, int width, int height, void *data, QgsRasterBlockFeedback *feedback = nullptr ) SIP_SKIP
724  { Q_UNUSED( bandNo ) Q_UNUSED( viewExtent ); Q_UNUSED( width ); Q_UNUSED( height ); Q_UNUSED( data ); Q_UNUSED( feedback ); return false; }
725 
727  bool userNoDataValuesContains( int bandNo, double value ) const;
728 
730  void copyBaseSettings( const QgsRasterDataProvider &other );
731 
736  int mDpi = -1;
737 
742  //bool hasNoDataValue ( int bandNo );
743 
745  QList<double> mSrcNoDataValue;
746 
748  QList<bool> mSrcHasNoDataValue;
749 
755  QList<bool> mUseSrcNoDataValue;
756 
761  QList< QgsRasterRangeList > mUserNoDataValue;
762 
764 
766  bool mProviderResamplingEnabled = false;
767 
769  ResamplingMethod mZoomedInResamplingMethod = ResamplingMethod::Nearest;
770 
772  ResamplingMethod mZoomedOutResamplingMethod = ResamplingMethod::Nearest;
773 
775  double mMaxOversampling = 2.0;
776 
777  private:
778 
782  std::unique_ptr< QgsRasterDataProviderTemporalCapabilities > mTemporalCapabilities;
783 
784 };
785 
786 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsRasterDataProvider::ProviderCapabilities )
787 
788 // clazy:excludeall=qstring-allocations
789 
790 #endif
DataType
Raster data types.
Definition: qgis.h:119
This class represents a coordinate reference system (CRS).
Abstract base class for spatial data provider implementations.
Container of fields for a vector layer.
Definition: qgsfields.h:45
Handles asynchronous download of images.
QgsImageFetcher(QObject *parent=nullptr)
Constructor.
void progress(qint64 received, qint64 total)
Emitted to report progress.
void error(const QString &msg)
Emitted when an error occurs.
void finish(const QImage &legend)
Emitted when the download completes.
virtual void start()=0
Starts the image download.
The QgsMapSettings class contains configuration for rendering of the map.
A class to represent a 2D point.
Definition: qgspointxy.h:59
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:49
Feedback object tailored for raster block reading.
Raster data container.
Implementation of data provider temporal properties for QgsRasterDataProviders.
Base class for raster data providers.
virtual QgsFields fields() const
Returns the fields of the raster layer for data providers that expose them, the default implementatio...
virtual QString buildPyramids(const QList< QgsRasterPyramid > &pyramidList, const QString &resamplingMethod="NEAREST", QgsRaster::RasterPyramidsFormat format=QgsRaster::PyramidsGTiff, const QStringList &configOptions=QStringList(), QgsRasterBlockFeedback *feedback=nullptr)
Creates pyramid overviews.
QList< bool > mUseSrcNoDataValue
Use source nodata value.
TransformType
Types of transformation in transformCoordinates() function.
@ TransformLayerToImage
Transforms layer (georeferenced) coordinates to image coordinates.
@ TransformImageToLayer
Transforms image coordinates to layer (georeferenced) coordinates.
virtual QList< QgsColorRampShader::ColorRampItem > colorTable(int bandNo) const
virtual bool setNoDataValue(int bandNo, double noDataValue)
Set no data value on created dataset.
virtual double bandOffset(int bandNo) const
Read band offset for raster value.
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.
virtual bool useSourceNoDataValue(int bandNo) const
Returns the source nodata value usage.
virtual QString validateCreationOptions(const QStringList &createOptions, const QString &format)
Validates creation options for a specific dataset and destination format.
virtual QString lastError()=0
Returns the verbose error text for the last error in this provider.
virtual QList< QgsRasterPyramid > buildPyramidList(const QList< int > &overviewList=QList< int >())
Returns the raster layers pyramid list.
Qgis::DataType sourceDataType(int bandNo) const override=0
Returns source data type for the band specified by number, source data type may be shorter than dataT...
bool isProviderResamplingEnabled() const
Returns whether provider-level resampling is enabled.
virtual bool remove()
Remove dataset.
virtual double sourceNoDataValue(int bandNo) const
Value representing no data value.
QString colorName(int colorInterpretation) const
virtual QImage getLegendGraphic(double scale=0, bool forceRefresh=false, const QgsRectangle *visibleExtent=nullptr)
Returns the legend rendered as pixmap.
virtual bool reload()
Reload data (data could change)
QList< QgsRasterRangeList > mUserNoDataValue
List of lists of user defined additional no data values for each band, indexed from 0.
ResamplingMethod zoomedOutResamplingMethod() const
Returns resampling method for zoomed-out operations.
virtual bool isEditable() const
Checks whether the provider is in editing mode, i.e.
QgsRasterDataProvider * clone() const override=0
Clone itself, create deep copy.
virtual bool enableProviderResampling(bool enable)
Enable or disable provider-level resampling.
void setDpi(int dpi)
Sets the output device resolution.
QDateTime timestamp() const override
Time stamp of data source in the moment when data/metadata were loaded by provider.
QgsRectangle extent() const override=0
Returns the extent of the layer.
virtual bool setMaxOversampling(double factor)
Sets maximum oversampling factor for zoomed-out operations.
Qgis::DataType dataType(int bandNo) const override=0
Returns data type for the band specified by number.
double maxOversampling() const
Returns maximum oversampling factor for zoomed-out operations.
QStringList subLayers() const override
Returns the sublayers of this layer - useful for providers that manage their own layers,...
virtual double bandScale(int bandNo) const
Read band scale for raster value.
ProviderCapability
Enumeration with capabilities that raster providers might implement.
int dpi() const
Returns the dpi of the output device.
virtual QString validatePyramidsConfigOptions(QgsRaster::RasterPyramidsFormat pyramidsFormat, const QStringList &configOptions, const QString &fileFormat)
Validates pyramid creation options for a specific dataset and destination format.
QDateTime dataTimestamp() const override
Current time stamp of data source.
virtual QString htmlMetadata()=0
Returns metadata in a format suitable for feeding directly into a subset of the GUI raster properties...
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.
bool setInput(QgsRasterInterface *input) override
Set input.
QList< bool > mSrcHasNoDataValue
Source no data value exists.
void statusChanged(const QString &) const
Emit a message to be displayed on status bar, usually used by network providers (WMS,...
virtual QgsRasterRangeList userNoDataValues(int bandNo) const
Returns a list of user no data value ranges.
virtual bool setZoomedInResamplingMethod(ResamplingMethod method)
Set resampling method to apply for zoomed-in operations.
virtual int stepHeight() const
Step height for raster iterations.
virtual bool setEditable(bool enabled)
Turns on/off editing mode of the provider.
virtual int stepWidth() const
Step width for raster iterations.
ResamplingMethod zoomedInResamplingMethod() const
Returns resampling method for zoomed-in operations.
ResamplingMethod
Resampling method for provider-level resampling.
virtual QString lastErrorTitle()=0
Returns the caption error text for the last error in this provider.
virtual bool write(void *data, int band, int width, int height, int xOffset, int yOffset)
Writes into the provider datasource.
virtual bool setZoomedOutResamplingMethod(ResamplingMethod method)
Set resampling method to apply for zoomed-out operations.
virtual QgsImageFetcher * getLegendGraphicFetcher(const QgsMapSettings *mapSettings)
Returns a new image downloader for the raster legend.
Raster identify results container.
Base class for processing filters like renderers, reprojector, resampler etc.
static const int DEFAULT_MAXIMUM_TILE_WIDTH
Default maximum tile width.
static const int DEFAULT_MAXIMUM_TILE_HEIGHT
Default maximum tile height.
IdentifyFormat
Definition: qgsraster.h:58
RasterPyramidsFormat
Definition: qgsraster.h:82
@ PyramidsGTiff
Definition: qgsraster.h:83
@ RedBand
Red band of RGBA image.
Definition: qgsraster.h:40
@ UndefinedColorInterpretation
Definition: qgsraster.h:37
@ YCbCr_YBand
Y Luminance.
Definition: qgsraster.h:51
@ YellowBand
Yellow band of CMYK image.
Definition: qgsraster.h:49
@ YCbCr_CbBand
Cb Chroma.
Definition: qgsraster.h:52
@ BlueBand
Blue band of RGBA image.
Definition: qgsraster.h:42
@ PaletteIndex
Paletted (see associated color table)
Definition: qgsraster.h:39
@ HueBand
Hue band of HLS image.
Definition: qgsraster.h:44
@ LightnessBand
Lightness band of HLS image.
Definition: qgsraster.h:46
@ AlphaBand
Alpha (0=transparent, 255=opaque)
Definition: qgsraster.h:43
@ BlackBand
Black band of CMLY image.
Definition: qgsraster.h:50
@ GreenBand
Green band of RGBA image.
Definition: qgsraster.h:41
@ CyanBand
Cyan band of CMYK image.
Definition: qgsraster.h:47
@ SaturationBand
Saturation band of HLS image.
Definition: qgsraster.h:45
@ GrayIndex
Greyscale.
Definition: qgsraster.h:38
@ MagentaBand
Magenta band of CMYK image.
Definition: qgsraster.h:48
@ YCbCr_CrBand
Cr Chroma.
Definition: qgsraster.h:53
A rectangle specified with double values.
Definition: qgsrectangle.h:42
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_OUT
Definition: qgis_sip.h:58
#define SIP_FACTORY
Definition: qgis_sip.h:76
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsField::ConfigurationFlags) CORE_EXPORT QDataStream &operator<<(QDataStream &out
Writes the field to stream out. QGIS version compatibility is not guaranteed.
QList< QgsRasterRange > QgsRasterRangeList
const QgsCoordinateReferenceSystem & crs
Setting options for creating vector data providers.