QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
qgsrasterlayer.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsrasterlayer.h - description
3  -------------------
4  begin : Fri Jun 28 2002
5  copyright : (C) 2004 by T.Sutton, Gary E.Sherman, Steve Halasz
6  email : [email protected]
7 ***************************************************************************/
8 /*
9  * Peter J. Ersts - contributed to the refactoring and maintenance of this class
10  * B. Morley - added functions to convert this class to a data provider interface
11  * Frank Warmerdam - contributed bug fixes and migrated class to use all GDAL_C_API calls
12  */
13 /***************************************************************************
14  * *
15  * This program is free software; you can redistribute it and/or modify *
16  * it under the terms of the GNU General Public License as published by *
17  * the Free Software Foundation; either version 2 of the License, or *
18  * (at your option) any later version. *
19  * *
20  ***************************************************************************/
21 #ifndef QGSRASTERLAYER_H
22 #define QGSRASTERLAYER_H
23 
24 #include "qgis_core.h"
25 #include "qgis_sip.h"
26 #include <QColor>
27 #include <QDateTime>
28 #include <QList>
29 #include <QMap>
30 #include <QPair>
31 #include <QVector>
32 
33 #include "qgis_sip.h"
34 #include "qgsmaplayer.h"
35 #include "qgsraster.h"
36 #include "qgsrasterdataprovider.h"
37 #include "qgsrasterpipe.h"
38 #include "qgsrasterviewport.h"
39 #include "qgsrasterminmaxorigin.h"
40 #include "qgscontrastenhancement.h"
41 
42 class QgsMapToPixel;
43 class QgsRasterRenderer;
44 class QgsRectangle;
46 
47 class QImage;
48 class QPixmap;
49 class QSlider;
50 
51 typedef QList < QPair< QString, QColor > > QgsLegendColorList;
52 
70 class CORE_EXPORT QgsRasterLayer : public QgsMapLayer
71 {
72  Q_OBJECT
73  public:
74 
76  static const double SAMPLE_SIZE;
77 
80 
83 
86 
89 
92 
95 
98 
104  {
105 
109  explicit LayerOptions( bool loadDefaultStyle = true,
111  : loadDefaultStyle( loadDefaultStyle )
112  , transformContext( transformContext )
113  {}
114 
116  bool loadDefaultStyle = true;
117 
123 
137  bool skipCrsValidation = false;
138 
139  };
140 
153  explicit QgsRasterLayer( const QString &uri,
154  const QString &baseName = QString(),
155  const QString &providerType = "gdal",
157 
158  ~QgsRasterLayer() override;
159 
166  QgsRasterLayer *clone() const override SIP_FACTORY;
167 
170  {
175  UserDefinedShader
176  };
177 
180  {
184  ColorLayer
185  };
186 
193  static bool isValidRasterFileName( const QString &fileNameQString, QString &retError );
194  // TODO QGIS 4.0 - rename fileNameQString to fileName
195 
196  static bool isValidRasterFileName( const QString &fileNameQString );
197 
199  static QDateTime lastModified( const QString &name );
200 
205  Q_DECL_DEPRECATED void setDataProvider( const QString &provider ) SIP_DEPRECATED;
206 
214  void setDataProvider( const QString &provider, const QgsDataProvider::ProviderOptions &options, QgsDataProvider::ReadFlags flags = QgsDataProvider::ReadFlags() );
215 
228  void setDataSource( const QString &dataSource, const QString &baseName, const QString &provider, const QgsDataProvider::ProviderOptions &options, bool loadDefaultStyleFlag = false ) override;
229 
233  LayerType rasterType() { return mRasterType; }
234 
239  void setRenderer( QgsRasterRenderer *renderer SIP_TRANSFER );
240 
246  QgsRasterRenderer *renderer() const { return mPipe.renderer(); }
247 
254  QgsRasterResampleFilter *resampleFilter() const { return mPipe.resampleFilter(); }
255 
262  QgsBrightnessContrastFilter *brightnessFilter() const { return mPipe.brightnessFilter(); }
263 
270  QgsHueSaturationFilter *hueSaturationFilter() const { return mPipe.hueSaturationFilter(); }
271 
279  void setResamplingStage( QgsRasterPipe::ResamplingStage stage );
280 
288  QgsRasterPipe::ResamplingStage resamplingStage() const { return mPipe.resamplingStage(); }
289 
293  QgsRasterPipe *pipe() { return &mPipe; }
294 
299  int width() const;
300 
305  int height() const;
306 
310  int bandCount() const;
311 
315  QString bandName( int bandNoInt ) const;
316 
323 
329  const QgsRasterDataProvider *dataProvider() const SIP_PYNAME( constDataProvider ) override;
330 
331  void reload() override;
332  QgsMapLayerRenderer *createMapRenderer( QgsRenderContext &rendererContext ) override SIP_FACTORY;
333 
335  void draw( QPainter *theQPainter,
336  QgsRasterViewPort *myRasterViewPort,
337  const QgsMapToPixel *qgsMapToPixel = nullptr );
338 
340  QgsLegendColorList legendSymbologyItems() const;
341 
342  bool isSpatial() const override { return true; }
343 
344  QString htmlMetadata() const override;
345 
350  QPixmap paletteAsPixmap( int bandNumber = 1 );
351 
353  QString providerType() const;
354 
363  double rasterUnitsPerPixelX() const;
364 
372  double rasterUnitsPerPixelY() const;
373 
384  const QgsRectangle &extent = QgsRectangle(),
385  int sampleSize = QgsRasterLayer::SAMPLE_SIZE,
386  bool generateLookupTableFlag = true );
387 
392  void refreshContrastEnhancement( const QgsRectangle &extent ) SIP_SKIP;
393 
398  void refreshRendererIfNeeded( QgsRasterRenderer *rasterRenderer, const QgsRectangle &extent ) SIP_SKIP;
399 
405  virtual QString subsetString() const;
406 
415  virtual bool setSubsetString( const QString &subset );
416 
421  bool defaultContrastEnhancementSettings(
423  QgsRasterMinMaxOrigin::Limits &myLimits ) const SIP_SKIP;
424 
426  void setDefaultContrastEnhancement();
427 
428  QStringList subLayers() const override;
429 
434  QImage previewAsImage( QSize size, const QColor &bgColor = Qt::white,
435  QImage::Format format = QImage::Format_ARGB32_Premultiplied );
436 
437  void setLayerOrder( const QStringList &layers ) override;
438  void setSubLayerVisibility( const QString &name, bool vis ) override;
439  QDateTime timestamp() const override;
440  bool accept( QgsStyleEntityVisitorInterface *visitor ) const override;
441 
451  bool writeSld( QDomNode &node, QDomDocument &doc, QString &errorMessage, const QgsStringMap &props = QgsStringMap() ) const;
452 
461  bool ignoreExtents() const;
462 
464 
465  public slots:
466  void showStatusMessage( const QString &message );
467 
473  virtual void setTransformContext( const QgsCoordinateTransformContext &transformContext ) override;
474 
475  signals:
476 
482 
483 
484  protected:
485  bool readSymbology( const QDomNode &node, QString &errorMessage, QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories = QgsMapLayer::AllStyleCategories ) override;
486  bool readStyle( const QDomNode &node, QString &errorMessage, QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories = QgsMapLayer::AllStyleCategories ) override;
487  bool readXml( const QDomNode &layer_node, QgsReadWriteContext &context ) override;
488  bool writeSymbology( QDomNode &, QDomDocument &doc, QString &errorMessage,
489  const QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories = QgsMapLayer::AllStyleCategories ) const override;
490  bool writeStyle( QDomNode &node, QDomDocument &doc, QString &errorMessage,
491  const QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories = QgsMapLayer::AllStyleCategories ) const override;
492  bool writeXml( QDomNode &layer_node, QDomDocument &doc, const QgsReadWriteContext &context ) const override;
493  QString encodedSource( const QString &source, const QgsReadWriteContext &context ) const override;
494  QString decodedSource( const QString &source, const QString &provider, const QgsReadWriteContext &context ) const override;
495 
496  private:
498  void init();
499 
501  void closeDataProvider();
502 
504  bool update();
505 
507  void setRendererForDrawingStyle( QgsRaster::DrawingStyle drawingStyle );
508 
511  const QgsRectangle &extent,
512  int sampleSize,
513  bool generateLookupTableFlag,
514  QgsRasterRenderer *rasterRenderer );
515 
517  void refreshRenderer( QgsRasterRenderer *rasterRenderer, const QgsRectangle &extent );
518 
519  void computeMinMax( int band,
520  const QgsRasterMinMaxOrigin &mmo,
522  const QgsRectangle &extent,
523  int sampleSize,
524  double &min, double &max );
525 
527  const QString QSTRING_NOT_SET;
528  const QString TRSTRING_NOT_SET;
529 
531  QgsRasterDataProvider *mDataProvider = nullptr;
532 
534  QgsRasterLayerTemporalProperties *mTemporalProperties = nullptr;
535 
537  QDateTime mLastModified;
538 
539  QgsRasterViewPort mLastViewPort;
540 
541  LayerType mRasterType;
542 
543  QgsRasterPipe mPipe;
544 
546  QgsRectangle mLastRectangleUsedByRefreshContrastEnhancementIfNeeded;
547 
548  QDomDocument mOriginalStyleDocument;
549  QDomElement mOriginalStyleElement;
550 };
551 
552 // clazy:excludeall=qstring-allocations
553 
554 #endif
SIP_PYNAME
#define SIP_PYNAME(name)
Definition: qgis_sip.h:81
qgsrasterminmaxorigin.h
QgsCoordinateTransformContext
Contains information about the context in which a coordinate transform is executed.
Definition: qgscoordinatetransformcontext.h:58
QgsMapLayer::writeXml
virtual bool writeXml(QDomNode &layer_node, QDomDocument &document, const QgsReadWriteContext &context) const
Called by writeLayerXML(), used by children to write state specific to them to project files.
Definition: qgsmaplayer.cpp:598
QgsDataProvider::ProviderOptions
Setting options for creating vector data providers.
Definition: qgsdataprovider.h:105
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:35
qgsrasterpipe.h
QgsRasterLayer::UndefinedShader
@ UndefinedShader
Definition: qgsrasterlayer.h:171
QgsRasterLayer::resamplingStage
QgsRasterPipe::ResamplingStage resamplingStage() const
Returns which stage of the pipe should apply resampling.
Definition: qgsrasterlayer.h:288
qgsrasterviewport.h
QgsMapLayer::clone
virtual QgsMapLayer * clone() const =0
Returns a new instance equivalent to this one except for the id which is still unique.
QgsRasterMinMaxOrigin::Limits
Limits
This enumerator describes the limits used to compute min/max values.
Definition: qgsrasterminmaxorigin.h:59
algorithm
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into allowing algorithms to be written in pure substantial changes are required in order to port existing x Processing algorithms for QGIS x The most significant changes are outlined not GeoAlgorithm For algorithms which operate on features one by consider subclassing the QgsProcessingFeatureBasedAlgorithm class This class allows much of the boilerplate code for looping over features from a vector layer to be bypassed and instead requires implementation of a processFeature method Ensure that your algorithm(or algorithm 's parent class) implements the new pure virtual createInstance(self) call
qgscontrastenhancement.h
QgsMapLayer::subLayers
virtual QStringList subLayers() const
Returns the sublayers of this layer.
Definition: qgsmaplayer.cpp:748
QgsRasterLayer::GrayOrUndefined
@ GrayOrUndefined
Definition: qgsrasterlayer.h:181
QgsRasterLayer::SAMPLE_SIZE
static const double SAMPLE_SIZE
Default sample size (number of pixels) for estimated statistics/histogram calculation.
Definition: qgsrasterlayer.h:76
QgsRaster::DrawingStyle
DrawingStyle
This enumerator describes the different kinds of drawing we can do.
Definition: qgsraster.h:90
QgsRasterPipe
Base class for processing modules.
Definition: qgsrasterpipe.h:47
QgsRenderContext
Contains information about the context of a rendering operation.
Definition: qgsrendercontext.h:58
QgsStyleEntityVisitorInterface
An interface for classes which can visit style entity (e.g.
Definition: qgsstyleentityvisitor.h:34
QgsRasterLayer::subsetStringChanged
void subsetStringChanged()
Emitted when the layer's subset string has changed.
QgsRasterLayer::Multiband
@ Multiband
Definition: qgsrasterlayer.h:183
QgsRasterLayer::MULTIPLE_BAND_SINGLE_BYTE_ENHANCEMENT_ALGORITHM
static const QgsContrastEnhancement::ContrastEnhancementAlgorithm MULTIPLE_BAND_SINGLE_BYTE_ENHANCEMENT_ALGORITHM
Default enhancement algorithm for multiple band raster of type Byte.
Definition: qgsrasterlayer.h:82
QgsRasterViewPort
This class provides details of the viewable area that a raster will be rendered into.
Definition: qgsrasterviewport.h:35
QgsMapLayer::htmlMetadata
virtual QString htmlMetadata() const
Obtain a formatted HTML string containing assorted metadata for this layer.
Definition: qgsmaplayer.cpp:1843
QgsMapLayer::accept
virtual bool accept(QgsStyleEntityVisitorInterface *visitor) const
Accepts the specified symbology visitor, causing it to visit all symbols associated with the layer.
Definition: qgsmaplayer.cpp:1894
QgsRasterLayer::LayerOptions::LayerOptions
LayerOptions(bool loadDefaultStyle=true, const QgsCoordinateTransformContext &transformContext=QgsCoordinateTransformContext())
Constructor for LayerOptions.
Definition: qgsrasterlayer.h:109
QgsRasterLayer::SINGLE_BAND_ENHANCEMENT_ALGORITHM
static const QgsContrastEnhancement::ContrastEnhancementAlgorithm SINGLE_BAND_ENHANCEMENT_ALGORITHM
Default enhancement algorithm for single band raster.
Definition: qgsrasterlayer.h:79
QgsRasterLayer::MULTIPLE_BAND_MULTI_BYTE_MIN_MAX_LIMITS
static const QgsRasterMinMaxOrigin::Limits MULTIPLE_BAND_MULTI_BYTE_MIN_MAX_LIMITS
Default enhancement limits for multiple band raster of type different from Byte.
Definition: qgsrasterlayer.h:94
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:42
QgsRasterLayerTemporalProperties
Implementation of map layer temporal properties for raster layers.
Definition: qgsrasterlayertemporalproperties.h:36
QgsMapLayerRenderer
Base class for utility classes that encapsulate information necessary for rendering of map layers.
Definition: qgsmaplayerrenderer.h:51
SIP_FACTORY
#define SIP_FACTORY
Definition: qgis_sip.h:76
QgsMapLayer::providerType
QString providerType() const
Returns the provider type (provider key) for this layer.
Definition: qgsmaplayer.cpp:1617
QgsRasterMinMaxOrigin
This class describes the origin of min/max values.
Definition: qgsrasterminmaxorigin.h:34
QgsRasterLayer::Palette
@ Palette
Definition: qgsrasterlayer.h:182
SIP_DEPRECATED
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
QgsRasterLayer::ColorRampShader
@ ColorRampShader
Definition: qgsrasterlayer.h:174
QgsMapLayer::setSubLayerVisibility
virtual void setSubLayerVisibility(const QString &name, bool visible)
Set the visibility of the given sublayer name.
Definition: qgsmaplayer.cpp:759
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
qgsraster.h
QgsMapLayer::encodedSource
virtual QString encodedSource(const QString &source, const QgsReadWriteContext &context) const
Called by writeLayerXML(), used by derived classes to encode provider's specific data source to proje...
Definition: qgsmaplayer.cpp:608
QgsContrastEnhancement::ContrastEnhancementAlgorithm
ContrastEnhancementAlgorithm
This enumerator describes the types of contrast enhancement algorithms that can be used.
Definition: qgscontrastenhancement.h:49
QgsRasterMinMaxOrigin::MinMax
@ MinMax
Real min-max values.
Definition: qgsrasterminmaxorigin.h:61
QgsRasterRenderer
Raster renderer pipe that applies colors to a raster.
Definition: qgsrasterrenderer.h:39
qgsmaplayer.h
qgis_sip.h
SIP_TRANSFER
#define SIP_TRANSFER
Definition: qgis_sip.h:36
QgsMapLayer::temporalProperties
virtual QgsMapLayerTemporalProperties * temporalProperties()
Returns the layer's temporal properties.
Definition: qgsmaplayer.h:1203
QgsMapLayerTemporalProperties
Base class for storage of map layer temporal properties.
Definition: qgsmaplayertemporalproperties.h:43
QgsHueSaturationFilter
Color and saturation filter pipe for rasters.
Definition: qgshuesaturationfilter.h:32
QgsRasterLayer::FreakOutShader
@ FreakOutShader
Definition: qgsrasterlayer.h:173
QgsRasterLayer::LayerType
LayerType
This enumerator describes the type of raster layer.
Definition: qgsrasterlayer.h:180
QgsRasterLayer::ColorShadingAlgorithm
ColorShadingAlgorithm
This enumerator describes the types of shading that can be used.
Definition: qgsrasterlayer.h:170
QgsMapLayer::decodedSource
virtual QString decodedSource(const QString &source, const QString &dataProvider, const QgsReadWriteContext &context) const
Called by readLayerXML(), used by derived classes to decode provider's specific data source from proj...
Definition: qgsmaplayer.cpp:614
QgsRasterLayer
Represents a raster layer.
Definition: qgsrasterlayer.h:71
QgsMapLayer::setTransformContext
virtual void setTransformContext(const QgsCoordinateTransformContext &transformContext)=0
Sets the coordinate transform context to transformContext.
QgsRasterLayer::resampleFilter
QgsRasterResampleFilter * resampleFilter() const
Returns the raster's resample filter.
Definition: qgsrasterlayer.h:254
QgsMapLayer::readSymbology
virtual bool readSymbology(const QDomNode &node, QString &errorMessage, QgsReadWriteContext &context, StyleCategories categories=AllStyleCategories)=0
Read the symbology for the current layer from the DOM node supplied.
QgsMapLayer::setDataSource
virtual void setDataSource(const QString &dataSource, const QString &baseName, const QString &provider, const QgsDataProvider::ProviderOptions &options, bool loadDefaultStyleFlag=false)
Updates the data source of the layer.
Definition: qgsmaplayer.cpp:1607
QgsRasterLayer::LayerOptions
Setting options for loading raster layers.
Definition: qgsrasterlayer.h:104
QgsStringMap
QMap< QString, QString > QgsStringMap
Definition: qgis.h:758
QgsMapLayer::writeSymbology
virtual bool writeSymbology(QDomNode &node, QDomDocument &doc, QString &errorMessage, const QgsReadWriteContext &context, StyleCategories categories=AllStyleCategories) const =0
Write the style for the layer into the docment provided.
QgsBrightnessContrastFilter
Brightness/contrast and gamma correction filter pipe for rasters.
Definition: qgsbrightnesscontrastfilter.h:32
QgsMapToPixel
Perform transforms between map coordinates and device coordinates.
Definition: qgsmaptopixel.h:38
QgsRasterLayer::pipe
QgsRasterPipe * pipe()
Returns the raster pipe.
Definition: qgsrasterlayer.h:293
QgsRasterLayer::MULTIPLE_BAND_MULTI_BYTE_ENHANCEMENT_ALGORITHM
static const QgsContrastEnhancement::ContrastEnhancementAlgorithm MULTIPLE_BAND_MULTI_BYTE_ENHANCEMENT_ALGORITHM
Default enhancement algorithm for multiple band raster of type different from Byte.
Definition: qgsrasterlayer.h:85
QgsMapLayer
Base class for all map layer types.
Definition: qgsmaplayer.h:83
QgsRasterResampleFilter
Resample filter pipe for rasters.
Definition: qgsrasterresamplefilter.h:33
QgsRasterLayer::SINGLE_BAND_MIN_MAX_LIMITS
static const QgsRasterMinMaxOrigin::Limits SINGLE_BAND_MIN_MAX_LIMITS
Default enhancement limits for single band raster.
Definition: qgsrasterlayer.h:88
QgsMapLayer::readXml
virtual bool readXml(const QDomNode &layer_node, QgsReadWriteContext &context)
Called by readLayerXML(), used by children to read state specific to them from project files.
Definition: qgsmaplayer.cpp:381
QgsRasterLayer::hueSaturationFilter
QgsHueSaturationFilter * hueSaturationFilter() const
Returns the raster's hue/saturation filter.
Definition: qgsrasterlayer.h:270
QgsLegendColorList
QList< QPair< QString, QColor > > QgsLegendColorList
Definition: qgsrasterlayer.h:49
QgsRasterLayer::brightnessFilter
QgsBrightnessContrastFilter * brightnessFilter() const
Returns the raster's brightness/contrast filter.
Definition: qgsrasterlayer.h:262
QgsRasterLayer::MULTIPLE_BAND_SINGLE_BYTE_MIN_MAX_LIMITS
static const QgsRasterMinMaxOrigin::Limits MULTIPLE_BAND_SINGLE_BYTE_MIN_MAX_LIMITS
Default enhancement limits for multiple band raster of type Byte.
Definition: qgsrasterlayer.h:91
QgsMapLayer::readStyle
virtual bool readStyle(const QDomNode &node, QString &errorMessage, QgsReadWriteContext &context, StyleCategories categories=AllStyleCategories)
Read the style for the current layer from the DOM node supplied.
Definition: qgsmaplayer.cpp:1587
QgsRasterLayer::renderer
QgsRasterRenderer * renderer() const
Returns the raster's renderer.
Definition: qgsrasterlayer.h:246
QgsMapLayer::AllStyleCategories
@ AllStyleCategories
Definition: qgsmaplayer.h:178
QgsRasterLayer::rasterType
LayerType rasterType()
Returns the raster layer type (which is a read only property).
Definition: qgsrasterlayer.h:233
QgsRasterPipe::ResamplingStage
ResamplingStage
Stage at which resampling occurs.
Definition: qgsrasterpipe.h:139
QgsRasterDataProvider
Base class for raster data providers.
Definition: qgsrasterdataprovider.h:89
QgsMapLayer::dataProvider
virtual QgsDataProvider * dataProvider()
Returns the layer's data provider, it may be nullptr.
Definition: qgsmaplayer.cpp:169
QgsMapLayer::writeStyle
virtual bool writeStyle(QDomNode &node, QDomDocument &doc, QString &errorMessage, const QgsReadWriteContext &context, StyleCategories categories=AllStyleCategories) const
Write just the symbology information for the layer into the document.
Definition: qgsmaplayer.cpp:1596
QgsRasterLayer::PseudoColorShader
@ PseudoColorShader
Definition: qgsrasterlayer.h:172
QgsMapLayer::timestamp
virtual QDateTime timestamp() const
Time stamp of data source in the moment when data/metadata were loaded by provider.
Definition: qgsmaplayer.cpp:1848
QgsMapLayer::setLayerOrder
virtual void setLayerOrder(const QStringList &layers)
Reorders the previously selected sublayers of this layer from bottom to top.
Definition: qgsmaplayer.cpp:753
qgsrasterdataprovider.h