QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
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 
160 #ifdef SIP_RUN
161  SIP_PYOBJECT __repr__();
162  % MethodCode
163  QString str = QStringLiteral( "<QgsRasterLayer: '%1' (%2)>" ).arg( sipCpp->name(), sipCpp->dataProvider() ? sipCpp->dataProvider()->name() : QStringLiteral( "Invalid" ) );
164  sipRes = PyUnicode_FromString( str.toUtf8().constData() );
165  % End
166 #endif
167 
174  QgsRasterLayer *clone() const override SIP_FACTORY;
175 
178  {
183  UserDefinedShader
184  };
185 
188  {
192  ColorLayer
193  };
194 
201  static bool isValidRasterFileName( const QString &fileNameQString, QString &retError );
202  // TODO QGIS 4.0 - rename fileNameQString to fileName
203 
204  static bool isValidRasterFileName( const QString &fileNameQString );
205 
207  static QDateTime lastModified( const QString &name );
208 
213  Q_DECL_DEPRECATED void setDataProvider( const QString &provider ) SIP_DEPRECATED;
214 
222  void setDataProvider( const QString &provider, const QgsDataProvider::ProviderOptions &options, QgsDataProvider::ReadFlags flags = QgsDataProvider::ReadFlags() );
223 
236  void setDataSource( const QString &dataSource, const QString &baseName, const QString &provider, const QgsDataProvider::ProviderOptions &options, bool loadDefaultStyleFlag = false ) override;
237 
241  LayerType rasterType() { return mRasterType; }
242 
247  void setRenderer( QgsRasterRenderer *renderer SIP_TRANSFER );
248 
254  QgsRasterRenderer *renderer() const { return mPipe.renderer(); }
255 
262  QgsRasterResampleFilter *resampleFilter() const { return mPipe.resampleFilter(); }
263 
270  QgsBrightnessContrastFilter *brightnessFilter() const { return mPipe.brightnessFilter(); }
271 
278  QgsHueSaturationFilter *hueSaturationFilter() const { return mPipe.hueSaturationFilter(); }
279 
287  void setResamplingStage( QgsRasterPipe::ResamplingStage stage );
288 
296  QgsRasterPipe::ResamplingStage resamplingStage() const { return mPipe.resamplingStage(); }
297 
301  QgsRasterPipe *pipe() { return &mPipe; }
302 
307  int width() const;
308 
313  int height() const;
314 
318  int bandCount() const;
319 
323  QString bandName( int bandNoInt ) const;
324 
331 
337  const QgsRasterDataProvider *dataProvider() const SIP_PYNAME( constDataProvider ) override;
338 
339  void reload() override;
340  QgsMapLayerRenderer *createMapRenderer( QgsRenderContext &rendererContext ) override SIP_FACTORY;
341 
343  void draw( QPainter *theQPainter,
344  QgsRasterViewPort *myRasterViewPort,
345  const QgsMapToPixel *qgsMapToPixel = nullptr );
346 
352  Q_DECL_DEPRECATED QgsLegendColorList legendSymbologyItems() const SIP_DEPRECATED;
353 
354  bool isSpatial() const override { return true; }
355 
356  QString htmlMetadata() const override;
357 
362  QPixmap paletteAsPixmap( int bandNumber = 1 );
363 
365  QString providerType() const;
366 
375  double rasterUnitsPerPixelX() const;
376 
384  double rasterUnitsPerPixelY() const;
385 
386  void setOpacity( double opacity ) FINAL;
387  double opacity() const FINAL;
388 
397  void setContrastEnhancement( QgsContrastEnhancement::ContrastEnhancementAlgorithm algorithm,
398  QgsRasterMinMaxOrigin::Limits limits = QgsRasterMinMaxOrigin::MinMax,
399  const QgsRectangle &extent = QgsRectangle(),
400  int sampleSize = QgsRasterLayer::SAMPLE_SIZE,
401  bool generateLookupTableFlag = true );
402 
407  void refreshContrastEnhancement( const QgsRectangle &extent ) SIP_SKIP;
408 
413  void refreshRendererIfNeeded( QgsRasterRenderer *rasterRenderer, const QgsRectangle &extent ) SIP_SKIP;
414 
420  virtual QString subsetString() const;
421 
430  virtual bool setSubsetString( const QString &subset );
431 
436  bool defaultContrastEnhancementSettings(
437  QgsContrastEnhancement::ContrastEnhancementAlgorithm &myAlgorithm,
438  QgsRasterMinMaxOrigin::Limits &myLimits ) const SIP_SKIP;
439 
441  void setDefaultContrastEnhancement();
442 
443  QStringList subLayers() const override;
444 
449  QImage previewAsImage( QSize size, const QColor &bgColor = Qt::white,
450  QImage::Format format = QImage::Format_ARGB32_Premultiplied );
451 
452  void setLayerOrder( const QStringList &layers ) override;
453  void setSubLayerVisibility( const QString &name, bool vis ) override;
454  QDateTime timestamp() const override;
455  bool accept( QgsStyleEntityVisitorInterface *visitor ) const override;
456 
466  bool writeSld( QDomNode &node, QDomDocument &doc, QString &errorMessage, const QVariantMap &props = QVariantMap() ) const;
467 
476  bool ignoreExtents() const;
477 
478  QgsMapLayerTemporalProperties *temporalProperties() override;
479 
480  public slots:
481  void showStatusMessage( const QString &message );
482 
488  virtual void setTransformContext( const QgsCoordinateTransformContext &transformContext ) override;
489 
490  signals:
491 
496  void subsetStringChanged();
497 
498 
499  protected:
500  bool readSymbology( const QDomNode &node, QString &errorMessage, QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories = QgsMapLayer::AllStyleCategories ) override;
501  bool readStyle( const QDomNode &node, QString &errorMessage, QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories = QgsMapLayer::AllStyleCategories ) override;
502  bool readXml( const QDomNode &layer_node, QgsReadWriteContext &context ) override;
503  bool writeSymbology( QDomNode &, QDomDocument &doc, QString &errorMessage,
504  const QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories = QgsMapLayer::AllStyleCategories ) const override;
505  bool writeStyle( QDomNode &node, QDomDocument &doc, QString &errorMessage,
506  const QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories = QgsMapLayer::AllStyleCategories ) const override;
507  bool writeXml( QDomNode &layer_node, QDomDocument &doc, const QgsReadWriteContext &context ) const override;
508  QString encodedSource( const QString &source, const QgsReadWriteContext &context ) const override;
509  QString decodedSource( const QString &source, const QString &provider, const QgsReadWriteContext &context ) const override;
510 
511  private:
513  void init();
514 
516  void closeDataProvider();
517 
519  bool update();
520 
522  void setRendererForDrawingStyle( QgsRaster::DrawingStyle drawingStyle );
523 
524  void setContrastEnhancement( QgsContrastEnhancement::ContrastEnhancementAlgorithm algorithm,
525  QgsRasterMinMaxOrigin::Limits limits,
526  const QgsRectangle &extent,
527  int sampleSize,
528  bool generateLookupTableFlag,
529  QgsRasterRenderer *rasterRenderer );
530 
532  void refreshRenderer( QgsRasterRenderer *rasterRenderer, const QgsRectangle &extent );
533 
534  void computeMinMax( int band,
535  const QgsRasterMinMaxOrigin &mmo,
536  QgsRasterMinMaxOrigin::Limits limits,
537  const QgsRectangle &extent,
538  int sampleSize,
539  double &min, double &max );
540 
542  const QString QSTRING_NOT_SET;
543  const QString TRSTRING_NOT_SET;
544 
546  QgsRasterDataProvider *mDataProvider = nullptr;
547 
549  QgsRasterLayerTemporalProperties *mTemporalProperties = nullptr;
550 
552  QDateTime mLastModified;
553 
554  QgsRasterViewPort mLastViewPort;
555 
556  LayerType mRasterType;
557 
558  QgsRasterPipe mPipe;
559 
561  QgsRectangle mLastRectangleUsedByRefreshContrastEnhancementIfNeeded;
562 
563  QDomDocument mOriginalStyleDocument;
564  QDomElement mOriginalStyleElement;
565 };
566 
567 // clazy:excludeall=qstring-allocations
568 
569 #endif
Brightness/contrast and gamma correction filter pipe for rasters.
Manipulates raster or point cloud pixel values so that they enhanceContrast or clip into a specified ...
ContrastEnhancementAlgorithm
This enumerator describes the types of contrast enhancement algorithms that can be used.
Contains information about the context in which a coordinate transform is executed.
Color and saturation filter pipe for rasters.
Base class for utility classes that encapsulate information necessary for rendering of map layers.
Base class for storage of map layer temporal properties.
Base class for all map layer types.
Definition: qgsmaplayer.h:85
QString providerType() const
Returns the provider type (provider key) for this layer.
virtual void setOpacity(double opacity)
Sets the opacity for the layer, where opacity is a value between 0 (totally transparent) and 1....
virtual QgsMapLayer * clone() const =0
Returns a new instance equivalent to this one except for the id which is still unique.
virtual QString htmlMetadata() const
Obtain a formatted HTML string containing assorted metadata for this layer.
double opacity
Definition: qgsmaplayer.h:94
virtual Q_INVOKABLE QgsDataProvider * dataProvider()
Returns the layer's data provider, it may be nullptr.
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.
Perform transforms between map coordinates and device coordinates.
Definition: qgsmaptopixel.h:39
Base class for raster data providers.
Implementation of map layer temporal properties for raster layers.
Represents a raster layer.
QgsRasterPipe::ResamplingStage resamplingStage() const
Returns which stage of the pipe should apply resampling.
LayerType
This enumerator describes the type of raster layer.
ColorShadingAlgorithm
This enumerator describes the types of shading that can be used.
QgsRasterPipe * pipe()
Returns the raster pipe.
static const QgsContrastEnhancement::ContrastEnhancementAlgorithm MULTIPLE_BAND_SINGLE_BYTE_ENHANCEMENT_ALGORITHM
Default enhancement algorithm for multiple band raster of type Byte.
QgsHueSaturationFilter * hueSaturationFilter() const
Returns the raster's hue/saturation filter.
static const QgsRasterMinMaxOrigin::Limits SINGLE_BAND_MIN_MAX_LIMITS
Default enhancement limits for single band raster.
static const QgsRasterMinMaxOrigin::Limits MULTIPLE_BAND_MULTI_BYTE_MIN_MAX_LIMITS
Default enhancement limits for multiple band raster of type different from Byte.
LayerType rasterType()
Returns the raster layer type (which is a read only property).
static const double SAMPLE_SIZE
Default sample size (number of pixels) for estimated statistics/histogram calculation.
static const QgsRasterMinMaxOrigin::Limits MULTIPLE_BAND_SINGLE_BYTE_MIN_MAX_LIMITS
Default enhancement limits for multiple band raster of type Byte.
static const QgsContrastEnhancement::ContrastEnhancementAlgorithm MULTIPLE_BAND_MULTI_BYTE_ENHANCEMENT_ALGORITHM
Default enhancement algorithm for multiple band raster of type different from Byte.
QgsRasterResampleFilter * resampleFilter() const
Returns the raster's resample filter.
static const QgsContrastEnhancement::ContrastEnhancementAlgorithm SINGLE_BAND_ENHANCEMENT_ALGORITHM
Default enhancement algorithm for single band raster.
QgsBrightnessContrastFilter * brightnessFilter() const
Returns the raster's brightness/contrast filter.
QgsRasterRenderer * renderer() const
Returns the raster's renderer.
This class describes the origin of min/max values.
Limits
This enumerator describes the limits used to compute min/max values.
Base class for processing modules.
Definition: qgsrasterpipe.h:47
ResamplingStage
Stage at which resampling occurs.
Raster renderer pipe that applies colors to a raster.
Resample filter pipe for rasters.
Raster namespace.
Definition: qgsraster.h:32
The class is used as a container of context for various read/write operations on other objects.
A rectangle specified with double values.
Definition: qgsrectangle.h:42
Contains information about the context of a rendering operation.
An interface for classes which can visit style entity (e.g.
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
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
#define SIP_SKIP
Definition: qgis_sip.h:126
#define FINAL
Definition: qgis_sip.h:228
#define SIP_PYNAME(name)
Definition: qgis_sip.h:81
#define SIP_TRANSFER
Definition: qgis_sip.h:36
#define SIP_FACTORY
Definition: qgis_sip.h:76
constexpr int SAMPLE_SIZE
QList< QPair< QString, QColor > > QgsLegendColorList
Setting options for creating vector data providers.
Setting options for loading raster layers.
LayerOptions(bool loadDefaultStyle=true, const QgsCoordinateTransformContext &transformContext=QgsCoordinateTransformContext())
Constructor for LayerOptions.
This class provides details of the viewable area that a raster will be rendered into.