QGIS API Documentation  3.17.0-Master (a8c3b02cdd)
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  };
107 
109  Q_DECLARE_FLAGS( ProviderCapabilities, ProviderCapability )
110 
112 
121  QgsRasterDataProvider( const QString &uri,
123  QgsDataProvider::ReadFlags flags = QgsDataProvider::ReadFlags() );
124 
125  QgsRasterDataProvider *clone() const override = 0;
126 
131  virtual QgsRasterDataProvider::ProviderCapabilities providerCapabilities() const;
132 
133  /* It makes no sense to set input on provider */
134  bool setInput( QgsRasterInterface *input ) override { Q_UNUSED( input ) return false; }
135 
136  QgsRectangle extent() const override = 0;
137 
139  Qgis::DataType dataType( int bandNo ) const override = 0;
140 
146  virtual QgsFields fields() const { return QgsFields(); };
147 
152  Qgis::DataType sourceDataType( int bandNo ) const override = 0;
153 
155  virtual int colorInterpretation( int bandNo ) const;
156 
157  QString colorName( int colorInterpretation ) const
158  {
159  // Modified copy from GDAL
160  switch ( colorInterpretation )
161  {
163  return QStringLiteral( "Undefined" );
164 
166  return QStringLiteral( "Gray" );
167 
169  return QStringLiteral( "Palette" );
170 
171  case QgsRaster::RedBand:
172  return QStringLiteral( "Red" );
173 
175  return QStringLiteral( "Green" );
176 
177  case QgsRaster::BlueBand:
178  return QStringLiteral( "Blue" );
179 
181  return QStringLiteral( "Alpha" );
182 
183  case QgsRaster::HueBand:
184  return QStringLiteral( "Hue" );
185 
187  return QStringLiteral( "Saturation" );
188 
190  return QStringLiteral( "Lightness" );
191 
192  case QgsRaster::CyanBand:
193  return QStringLiteral( "Cyan" );
194 
196  return QStringLiteral( "Magenta" );
197 
199  return QStringLiteral( "Yellow" );
200 
202  return QStringLiteral( "Black" );
203 
205  return QStringLiteral( "YCbCr_Y" );
206 
208  return QStringLiteral( "YCbCr_Cb" );
209 
211  return QStringLiteral( "YCbCr_Cr" );
212 
213  default:
214  return QStringLiteral( "Unknown" );
215  }
216  }
218  virtual bool reload() { return true; }
219 
220  QString colorInterpretationName( int bandNo ) const override;
221 
226  virtual double bandScale( int bandNo ) const { Q_UNUSED( bandNo ) return 1.0; }
227 
232  virtual double bandOffset( int bandNo ) const { Q_UNUSED( bandNo ) return 0.0; }
233 
234  // TODO: remove or make protected all readBlock working with void*
235 
237  QgsRasterBlock *block( int bandNo, const QgsRectangle &boundingBox, int width, int height, QgsRasterBlockFeedback *feedback = nullptr ) override SIP_FACTORY;
238 
240  virtual bool sourceHasNoDataValue( int bandNo ) const { return mSrcHasNoDataValue.value( bandNo - 1 ); }
241 
243  virtual bool useSourceNoDataValue( int bandNo ) const { return mUseSrcNoDataValue.value( bandNo - 1 ); }
244 
246  virtual void setUseSourceNoDataValue( int bandNo, bool use );
247 
249  virtual double sourceNoDataValue( int bandNo ) const { return mSrcNoDataValue.value( bandNo - 1 ); }
250 
251  virtual void setUserNoDataValue( int bandNo, const QgsRasterRangeList &noData );
252 
254  virtual QgsRasterRangeList userNoDataValues( int bandNo ) const { return mUserNoDataValue.value( bandNo - 1 ); }
255 
256  virtual QList<QgsColorRampShader::ColorRampItem> colorTable( int bandNo ) const
257  { Q_UNUSED( bandNo ) return QList<QgsColorRampShader::ColorRampItem>(); }
258 
263  QStringList subLayers() const override
264  {
265  return QStringList();
266  }
267 
270 
272  virtual bool supportsLegendGraphic() const { return false; }
273 
285  virtual QImage getLegendGraphic( double scale = 0, bool forceRefresh = false, const QgsRectangle *visibleExtent = nullptr ) SIP_SKIP
286  {
287  Q_UNUSED( scale )
288  Q_UNUSED( forceRefresh )
289  Q_UNUSED( visibleExtent )
290  return QImage();
291  }
292 
306  virtual QgsImageFetcher *getLegendGraphicFetcher( const QgsMapSettings *mapSettings ) SIP_FACTORY
307  {
308  Q_UNUSED( mapSettings )
309  return nullptr;
310  }
311 
313  virtual QString buildPyramids( const QList<QgsRasterPyramid> &pyramidList,
314  const QString &resamplingMethod = "NEAREST",
316  const QStringList &configOptions = QStringList(),
317  QgsRasterBlockFeedback *feedback = nullptr )
318  {
319  Q_UNUSED( pyramidList )
320  Q_UNUSED( resamplingMethod )
321  Q_UNUSED( format )
322  Q_UNUSED( configOptions )
323  Q_UNUSED( feedback )
324  return QStringLiteral( "FAILED_NOT_SUPPORTED" );
325  }
326 
335  virtual QList<QgsRasterPyramid> buildPyramidList( QList<int> overviewList = QList<int>() ) // clazy:exclude=function-args-by-ref
336  { Q_UNUSED( overviewList ) return QList<QgsRasterPyramid>(); }
337 
339  bool hasPyramids();
340 
345  virtual QString htmlMetadata() = 0;
346 
371  virtual QgsRasterIdentifyResult identify( const QgsPointXY &point, QgsRaster::IdentifyFormat format, const QgsRectangle &boundingBox = QgsRectangle(), int width = 0, int height = 0, int dpi = 96 );
372 
387  virtual double sample( const QgsPointXY &point, int band,
388  bool *ok SIP_OUT = nullptr,
389  const QgsRectangle &boundingBox = QgsRectangle(), int width = 0, int height = 0, int dpi = 96 );
390 
399  virtual QString lastErrorTitle() = 0;
400 
410  virtual QString lastError() = 0;
411 
413  virtual QString lastErrorFormat();
414 
416  int dpi() const { return mDpi; }
417 
419  void setDpi( int dpi ) { mDpi = dpi; }
420 
422  QDateTime timestamp() const override { return mTimestamp; }
423 
425  QDateTime dataTimestamp() const override { return QDateTime(); }
426 
433  virtual bool isEditable() const { return false; }
434 
445  virtual bool setEditable( bool enabled ) { Q_UNUSED( enabled ) return false; }
446 
447  // TODO: add data type (may be different from band type)
448 
450  virtual bool write( void *data, int band, int width, int height, int xOffset, int yOffset )
451  {
452  Q_UNUSED( data )
453  Q_UNUSED( band )
454  Q_UNUSED( width )
455  Q_UNUSED( height )
456  Q_UNUSED( xOffset )
457  Q_UNUSED( yOffset )
458  return false;
459  }
460 
477  bool writeBlock( QgsRasterBlock *block, int band, int xOffset = 0, int yOffset = 0 );
478 
480  static QgsRasterDataProvider *create( const QString &providerKey,
481  const QString &uri,
482  const QString &format, int nBands,
483  Qgis::DataType type,
484  int width, int height, double *geoTransform,
486  const QStringList &createOptions = QStringList() );
487 
493  virtual bool setNoDataValue( int bandNo, double noDataValue ) { Q_UNUSED( bandNo ) Q_UNUSED( noDataValue ); return false; }
494 
496  virtual bool remove() { return false; }
497 
502  static QList<QPair<QString, QString> > pyramidResamplingMethods( const QString &providerKey );
503 
509  virtual QString validateCreationOptions( const QStringList &createOptions, const QString &format )
510  { Q_UNUSED( createOptions ) Q_UNUSED( format ); return QString(); }
511 
517  const QStringList &configOptions, const QString &fileFormat )
518  { Q_UNUSED( pyramidsFormat ) Q_UNUSED( configOptions ); Q_UNUSED( fileFormat ); return QString(); }
519 
520  static QString identifyFormatName( QgsRaster::IdentifyFormat format );
521  static QgsRaster::IdentifyFormat identifyFormatFromName( const QString &formatName );
522  static QString identifyFormatLabel( QgsRaster::IdentifyFormat format );
523  static Capability identifyFormatToCapability( QgsRaster::IdentifyFormat format );
524 
531 
538 
547  virtual QList< double > nativeResolutions() const;
548 
555  virtual bool ignoreExtents() const;
556 
562  {
565  };
566 
579  virtual QgsPoint transformCoordinates( const QgsPoint &point, TransformType type );
580 
581 
588  virtual bool enableProviderResampling( bool enable ) { Q_UNUSED( enable ); return false; }
589 
602  bool isProviderResamplingEnabled() const { return mProviderResamplingEnabled; }
603 
608  enum class ResamplingMethod
609  {
610  Nearest,
611  Bilinear,
612  Cubic,
613  };
614 
621  virtual bool setZoomedInResamplingMethod( ResamplingMethod method ) { Q_UNUSED( method ); return false; }
622 
627  ResamplingMethod zoomedInResamplingMethod() const { return mZoomedInResamplingMethod; }
628 
635  virtual bool setZoomedOutResamplingMethod( ResamplingMethod method ) { Q_UNUSED( method ); return false; }
636 
641  ResamplingMethod zoomedOutResamplingMethod() const { return mZoomedOutResamplingMethod; }
642 
649  virtual bool setMaxOversampling( double factor ) { Q_UNUSED( factor ); return false; }
650 
655  double maxOversampling() const { return mMaxOversampling; }
656 
657  void readXml( const QDomElement &filterElem ) override;
658 
659  void writeXml( QDomDocument &doc, QDomElement &parentElem ) const override;
660 
661  signals:
662 
667  void statusChanged( const QString & ) const;
668 
669  protected:
670 
676  virtual bool readBlock( int bandNo, int xBlock, int yBlock, void *data ) SIP_SKIP
677  { Q_UNUSED( bandNo ) Q_UNUSED( xBlock ); Q_UNUSED( yBlock ); Q_UNUSED( data ); return false; }
678 
684  virtual bool readBlock( int bandNo, QgsRectangle const &viewExtent, int width, int height, void *data, QgsRasterBlockFeedback *feedback = nullptr ) SIP_SKIP
685  { Q_UNUSED( bandNo ) Q_UNUSED( viewExtent ); Q_UNUSED( width ); Q_UNUSED( height ); Q_UNUSED( data ); Q_UNUSED( feedback ); return false; }
686 
688  bool userNoDataValuesContains( int bandNo, double value ) const;
689 
691  void copyBaseSettings( const QgsRasterDataProvider &other );
692 
697  int mDpi = -1;
698 
703  //bool hasNoDataValue ( int bandNo );
704 
706  QList<double> mSrcNoDataValue;
707 
709  QList<bool> mSrcHasNoDataValue;
710 
716  QList<bool> mUseSrcNoDataValue;
717 
722  QList< QgsRasterRangeList > mUserNoDataValue;
723 
725 
727  bool mProviderResamplingEnabled = false;
728 
730  ResamplingMethod mZoomedInResamplingMethod = ResamplingMethod::Nearest;
731 
733  ResamplingMethod mZoomedOutResamplingMethod = ResamplingMethod::Nearest;
734 
736  double mMaxOversampling = 2.0;
737 
738  private:
739 
743  std::unique_ptr< QgsRasterDataProviderTemporalCapabilities > mTemporalCapabilities;
744 
745 };
746 
747 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsRasterDataProvider::ProviderCapabilities )
748 
749 // clazy:excludeall=qstring-allocations
750 
751 #endif
IdentifyFormat
Definition: qgsraster.h:57
A rectangle specified with double values.
Definition: qgsrectangle.h:41
bool setInput(QgsRasterInterface *input) override
Set input.
virtual bool setZoomedInResamplingMethod(ResamplingMethod method)
Set resampling method to apply for zoomed-in operations.
bool isProviderResamplingEnabled() const
Returns whether provider-level resampling is enabled.
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.
virtual QgsFields fields() const
Returns the fields of the raster layer for data providers that expose them, the default implementatio...
A class to represent a 2D point.
Definition: qgspointxy.h:43
ResamplingMethod zoomedOutResamplingMethod() const
Returns resampling method for zoomed-out operations.
QDateTime mTimestamp
Timestamp of data in the moment when the data were loaded by provider.
DataType
Raster data types.
Definition: qgis.h:101
virtual QgsImageFetcher * getLegendGraphicFetcher(const QgsMapSettings *mapSettings)
Returns a new image downloader for the raster legend.
Red band of RGBA image.
Definition: qgsraster.h:40
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.
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.
Container of fields for a vector layer.
Definition: qgsfields.h:44
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.
Transforms layer (georeferenced) coordinates to image coordinates.
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
virtual bool setZoomedOutResamplingMethod(ResamplingMethod method)
Set resampling method to apply for zoomed-out operations.
virtual bool enableProviderResampling(bool enable)
Enable or disable provider-level resampling.
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:126
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.
TransformType
Types of transformation in transformCoordinates() function.
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:76
double maxOversampling() const
Returns maximum oversampling factor for zoomed-out operations.
virtual QString colorInterpretationName(int bandNumber) const
Returns the name of the color interpretation for the specified bandNumber.
Base class for processing filters like renderers, reprojector, resampler etc.
virtual bool isEditable() const
Checks whether the provider is in editing mode, i.e.
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:37
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.
ResamplingMethod
Resampling method for provider-level resampling.
Implementation of data provider temporal properties for QgsRasterDataProviders.
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.
virtual QgsDataProviderTemporalCapabilities * temporalCapabilities()
Returns the provider&#39;s temporal capabilities.
QString colorName(int colorInterpretation) const
QList< QgsRasterRange > QgsRasterRangeList
ResamplingMethod zoomedInResamplingMethod() const
Returns resampling method for zoomed-in operations.
virtual QgsRasterInterface * clone() const =0
Clone itself, create deep copy.
#define SIP_OUT
Definition: qgis_sip.h:58
virtual bool setMaxOversampling(double factor)
Sets maximum oversampling factor for zoomed-out operations.
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.
Transforms image coordinates to layer (georeferenced) coordinates.
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
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.