QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 "qgis.h"
35 #include "qgsmaplayer.h"
36 #include "qgsraster.h"
37 #include "qgsrasterdataprovider.h"
38 #include "qgsrasterviewport.h"
39 #include "qgsrasterminmaxorigin.h"
40 #include "qgscontrastenhancement.h"
42 
43 class QgsMapToPixel;
44 class QgsRasterRenderer;
45 class QgsRectangle;
47 class QgsRasterPipe;
52 
53 class QImage;
54 class QPixmap;
55 class QSlider;
56 
57 typedef QList < QPair< QString, QColor > > QgsLegendColorList;
58 
76 class CORE_EXPORT QgsRasterLayer : public QgsMapLayer, public QgsAbstractProfileSource
77 {
78  Q_OBJECT
79  public:
80 
82  static const double SAMPLE_SIZE;
83 
86 
89 
92 
95 
98 
101 
103  QgsRasterLayer();
104 
110  {
111 
115  explicit LayerOptions( bool loadDefaultStyle = true,
117  : loadDefaultStyle( loadDefaultStyle )
118  , transformContext( transformContext )
119  {}
120 
122  bool loadDefaultStyle = true;
123 
129 
143  bool skipCrsValidation = false;
144 
145  };
146 
159  explicit QgsRasterLayer( const QString &uri,
160  const QString &baseName = QString(),
161  const QString &providerType = "gdal",
163 
164  ~QgsRasterLayer() override;
165 
166 #ifdef SIP_RUN
167  SIP_PYOBJECT __repr__();
168  % MethodCode
169  QString str = QStringLiteral( "<QgsRasterLayer: '%1' (%2)>" ).arg( sipCpp->name(), sipCpp->dataProvider() ? sipCpp->dataProvider()->name() : QStringLiteral( "Invalid" ) );
170  sipRes = PyUnicode_FromString( str.toUtf8().constData() );
171  % End
172 #endif
173 
180  QgsRasterLayer *clone() const override SIP_FACTORY;
181 
182  QgsAbstractProfileGenerator *createProfileGenerator( const QgsProfileRequest &request ) override SIP_FACTORY;
183 
186  {
191  UserDefinedShader
192  };
193 
196  {
200  ColorLayer
201  };
202 
209  static bool isValidRasterFileName( const QString &fileNameQString, QString &retError );
210  // TODO QGIS 4.0 - rename fileNameQString to fileName
211 
212  static bool isValidRasterFileName( const QString &fileNameQString );
213 
215  static QDateTime lastModified( const QString &name );
216 
221  Q_DECL_DEPRECATED void setDataProvider( const QString &provider ) SIP_DEPRECATED;
222 
230  void setDataProvider( const QString &provider, const QgsDataProvider::ProviderOptions &options, QgsDataProvider::ReadFlags flags = QgsDataProvider::ReadFlags() );
231 
235  LayerType rasterType() { return mRasterType; }
236 
241  void setRenderer( QgsRasterRenderer *renderer SIP_TRANSFER );
242 
248  QgsRasterRenderer *renderer() const;
249 
256  QgsRasterResampleFilter *resampleFilter() const;
257 
264  QgsBrightnessContrastFilter *brightnessFilter() const;
265 
272  QgsHueSaturationFilter *hueSaturationFilter() const;
273 
281  void setResamplingStage( Qgis::RasterResamplingStage stage );
282 
290  Qgis::RasterResamplingStage resamplingStage() const;
291 
295  QgsRasterPipe *pipe() { return mPipe.get(); }
296 
301  int width() const;
302 
307  int height() const;
308 
312  int bandCount() const;
313 
317  QString bandName( int bandNoInt ) const;
318 
325 
331  const QgsRasterDataProvider *dataProvider() const SIP_PYNAME( constDataProvider ) override;
332 
333  void reload() override;
334  QgsMapLayerRenderer *createMapRenderer( QgsRenderContext &rendererContext ) override SIP_FACTORY;
335 
337  void draw( QPainter *theQPainter,
338  QgsRasterViewPort *myRasterViewPort,
339  const QgsMapToPixel *qgsMapToPixel = nullptr );
340 
346  Q_DECL_DEPRECATED QgsLegendColorList legendSymbologyItems() const SIP_DEPRECATED;
347 
348  bool isSpatial() const override { return true; }
349 
350  QString htmlMetadata() const override;
351  Qgis::MapLayerProperties properties() const override;
352 
357  QPixmap paletteAsPixmap( int bandNumber = 1 );
358 
360  QString providerType() const;
361 
370  double rasterUnitsPerPixelX() const;
371 
379  double rasterUnitsPerPixelY() const;
380 
381  void setOpacity( double opacity ) FINAL;
382  double opacity() const FINAL;
383 
392  void setContrastEnhancement( QgsContrastEnhancement::ContrastEnhancementAlgorithm algorithm,
393  QgsRasterMinMaxOrigin::Limits limits = QgsRasterMinMaxOrigin::MinMax,
394  const QgsRectangle &extent = QgsRectangle(),
395  int sampleSize = QgsRasterLayer::SAMPLE_SIZE,
396  bool generateLookupTableFlag = true );
397 
402  void refreshContrastEnhancement( const QgsRectangle &extent ) SIP_SKIP;
403 
408  void refreshRendererIfNeeded( QgsRasterRenderer *rasterRenderer, const QgsRectangle &extent ) SIP_SKIP;
409 
415  virtual QString subsetString() const;
416 
425  virtual bool setSubsetString( const QString &subset );
426 
431  bool defaultContrastEnhancementSettings(
432  QgsContrastEnhancement::ContrastEnhancementAlgorithm &myAlgorithm,
433  QgsRasterMinMaxOrigin::Limits &myLimits ) const SIP_SKIP;
434 
436  void setDefaultContrastEnhancement();
437 
438  QStringList subLayers() const override;
439 
444  QImage previewAsImage( QSize size, const QColor &bgColor = Qt::white,
445  QImage::Format format = QImage::Format_ARGB32_Premultiplied );
446 
447  void setLayerOrder( const QStringList &layers ) override;
448  void setSubLayerVisibility( const QString &name, bool vis ) override;
449  QDateTime timestamp() const override;
450  bool accept( QgsStyleEntityVisitorInterface *visitor ) const override;
451 
461  bool writeSld( QDomNode &node, QDomDocument &doc, QString &errorMessage, const QVariantMap &props = QVariantMap() ) const;
462 
471  bool ignoreExtents() const;
472 
473  QgsMapLayerTemporalProperties *temporalProperties() override;
474  QgsMapLayerElevationProperties *elevationProperties() override;
475 
476  public slots:
477  void showStatusMessage( const QString &message );
478 
484  virtual void setTransformContext( const QgsCoordinateTransformContext &transformContext ) override;
485 
486  signals:
487 
492  void subsetStringChanged();
493 
494 
495  protected:
496  bool readSymbology( const QDomNode &node, QString &errorMessage, QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories = QgsMapLayer::AllStyleCategories ) override;
497  bool readStyle( const QDomNode &node, QString &errorMessage, QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories = QgsMapLayer::AllStyleCategories ) override;
498  bool readXml( const QDomNode &layer_node, QgsReadWriteContext &context ) override;
499  bool writeSymbology( QDomNode &, QDomDocument &doc, QString &errorMessage,
500  const QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories = QgsMapLayer::AllStyleCategories ) const override;
501  bool writeStyle( QDomNode &node, QDomDocument &doc, QString &errorMessage,
502  const QgsReadWriteContext &context, QgsMapLayer::StyleCategories categories = QgsMapLayer::AllStyleCategories ) const override;
503  bool writeXml( QDomNode &layer_node, QDomDocument &doc, const QgsReadWriteContext &context ) const override;
504  QString encodedSource( const QString &source, const QgsReadWriteContext &context ) const override;
505  QString decodedSource( const QString &source, const QString &provider, const QgsReadWriteContext &context ) const override;
506 
507  private:
509  void init();
510 
512  void closeDataProvider();
513 
515  bool update();
516 
518  void setRendererForDrawingStyle( QgsRaster::DrawingStyle drawingStyle );
519 
520  void setContrastEnhancement( QgsContrastEnhancement::ContrastEnhancementAlgorithm algorithm,
521  QgsRasterMinMaxOrigin::Limits limits,
522  const QgsRectangle &extent,
523  int sampleSize,
524  bool generateLookupTableFlag,
525  QgsRasterRenderer *rasterRenderer );
526 
528  void refreshRenderer( QgsRasterRenderer *rasterRenderer, const QgsRectangle &extent );
529 
530  void computeMinMax( int band,
531  const QgsRasterMinMaxOrigin &mmo,
532  QgsRasterMinMaxOrigin::Limits limits,
533  const QgsRectangle &extent,
534  int sampleSize,
535  double &min, double &max );
536 
548  void setDataSourcePrivate( const QString &dataSource, const QString &baseName, const QString &provider, const QgsDataProvider::ProviderOptions &options, QgsDataProvider::ReadFlags flags ) override;
549 
551  const QString QSTRING_NOT_SET;
552  const QString TRSTRING_NOT_SET;
553 
555  QgsRasterDataProvider *mDataProvider = nullptr;
556 
558  QgsRasterLayerTemporalProperties *mTemporalProperties = nullptr;
559 
560  QgsRasterLayerElevationProperties *mElevationProperties = nullptr;
561 
563  QDateTime mLastModified;
564 
565  QgsRasterViewPort mLastViewPort;
566 
567  LayerType mRasterType = GrayOrUndefined;
568 
569  std::unique_ptr< QgsRasterPipe > mPipe;
570 
572  QgsRectangle mLastRectangleUsedByRefreshContrastEnhancementIfNeeded;
573 
574  QDomDocument mOriginalStyleDocument;
575  QDomElement mOriginalStyleElement;
576 };
577 
578 // clazy:excludeall=qstring-allocations
579 
580 #endif
SAMPLE_SIZE
constexpr int SAMPLE_SIZE
Definition: qgsrasterhistogramwidget.cpp:55
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:57
QgsDataProvider::ProviderOptions
Setting options for creating vector data providers.
Definition: qgsdataprovider.h:107
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:34
QgsDataProvider
Abstract base class for spatial data provider implementations.
Definition: qgsdataprovider.h:40
QgsRasterLayer::UndefinedShader
@ UndefinedShader
Definition: qgsrasterlayer.h:187
QgsMapLayer::opacity
double opacity
Definition: qgsmaplayer.h:82
QgsMapLayerElevationProperties
Base class for storage of map layer elevation properties.
Definition: qgsmaplayerelevationproperties.h:41
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:71
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::properties
virtual Qgis::MapLayerProperties properties() const
Returns the map layer properties of this layer.
Definition: qgsmaplayer.cpp:164
QgsRasterLayer::GrayOrUndefined
@ GrayOrUndefined
Definition: qgsrasterlayer.h:197
QgsRasterLayer::SAMPLE_SIZE
static const double SAMPLE_SIZE
Default sample size (number of pixels) for estimated statistics/histogram calculation.
Definition: qgsrasterlayer.h:82
QgsRasterPipe
Contains a pipeline of raster interfaces for sequential raster processing.
Definition: qgsrasterpipe.h:49
QgsAbstractProfileGenerator
Abstract base class for objects which generate elevation profiles.
Definition: qgsabstractprofilegenerator.h:392
Qgis::RasterResamplingStage
RasterResamplingStage
Stage at which raster resampling occurs.
Definition: qgis.h:720
qgis.h
QgsRenderContext
Contains information about the context of a rendering operation.
Definition: qgsrendercontext.h:59
QgsStyleEntityVisitorInterface
An interface for classes which can visit style entity (e.g. symbol) nodes (using the visitor pattern)...
Definition: qgsstyleentityvisitor.h:33
QgsRasterLayer::Multiband
@ Multiband
Definition: qgsrasterlayer.h:199
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:88
QgsRasterViewPort
Definition: qgsrasterviewport.h:34
qgsabstractprofilesource.h
FINAL
#define FINAL
Definition: qgis_sip.h:237
QgsMapLayer::htmlMetadata
virtual QString htmlMetadata() const
Obtain a formatted HTML string containing assorted metadata for this layer.
Definition: qgsmaplayer.cpp:2135
QgsRasterLayer::LayerOptions::LayerOptions
LayerOptions(bool loadDefaultStyle=true, const QgsCoordinateTransformContext &transformContext=QgsCoordinateTransformContext())
Constructor for LayerOptions.
Definition: qgsrasterlayer.h:115
QgsRasterLayer::SINGLE_BAND_ENHANCEMENT_ALGORITHM
static const QgsContrastEnhancement::ContrastEnhancementAlgorithm SINGLE_BAND_ENHANCEMENT_ALGORITHM
Default enhancement algorithm for single band raster.
Definition: qgsrasterlayer.h:85
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:100
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:41
QgsRasterLayerTemporalProperties
Implementation of map layer temporal properties for raster layers.
Definition: qgsrasterlayertemporalproperties.h:35
QgsMapLayerRenderer
Base class for utility classes that encapsulate information necessary for rendering of map layers.
Definition: qgsmaplayerrenderer.h:54
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:1864
QgsRasterMinMaxOrigin
This class describes the origin of min/max values. It does not store by itself the min/max values.
Definition: qgsrasterminmaxorigin.h:33
QgsRasterLayer::Palette
@ Palette
Definition: qgsrasterlayer.h:198
geos::unique_ptr
std::unique_ptr< GEOSGeometry, GeosDeleter > unique_ptr
Scoped GEOS pointer.
Definition: qgsgeos.h:79
SIP_DEPRECATED
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
QgsRasterLayer::ColorRampShader
@ ColorRampShader
Definition: qgsrasterlayer.h:190
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
qgsraster.h
QgsContrastEnhancement::ContrastEnhancementAlgorithm
ContrastEnhancementAlgorithm
This enumerator describes the types of contrast enhancement algorithms that can be used.
Definition: qgscontrastenhancement.h:48
QgsMapLayer::dataProvider
virtual Q_INVOKABLE QgsDataProvider * dataProvider()
Returns the layer's data provider, it may be nullptr.
Definition: qgsmaplayer.cpp:190
QgsRasterRenderer
Raster renderer pipe that applies colors to a raster.
Definition: qgsrasterrenderer.h:40
QgsRasterLayerElevationProperties
Raster layer specific subclass of QgsMapLayerElevationProperties.
Definition: qgsrasterlayerelevationproperties.h:34
qgsmaplayer.h
qgis_sip.h
QgsMapLayer::setOpacity
virtual void setOpacity(double opacity)
Sets the opacity for the layer, where opacity is a value between 0 (totally transparent) and 1....
Definition: qgsmaplayer.cpp:325
SIP_TRANSFER
#define SIP_TRANSFER
Definition: qgis_sip.h:36
QgsProfileRequest
Encapsulates properties and constraints relating to fetching elevation profiles from different source...
Definition: qgsprofilerequest.h:37
QgsMapLayerTemporalProperties
Base class for storage of map layer temporal properties.
Definition: qgsmaplayertemporalproperties.h:42
QgsHueSaturationFilter
Color and saturation filter pipe for rasters.
Definition: qgshuesaturationfilter.h:31
QgsRasterLayer::FreakOutShader
@ FreakOutShader
Definition: qgsrasterlayer.h:189
QgsRasterLayer::LayerType
LayerType
This enumerator describes the type of raster layer.
Definition: qgsrasterlayer.h:195
QgsRasterLayer::ColorShadingAlgorithm
ColorShadingAlgorithm
This enumerator describes the types of shading that can be used.
Definition: qgsrasterlayer.h:185
QgsRasterLayer
Represents a raster layer.
Definition: qgsrasterlayer.h:76
QgsContrastEnhancement
Manipulates raster or point cloud pixel values so that they enhanceContrast or clip into a specified ...
Definition: qgscontrastenhancement.h:42
QgsRasterLayer::LayerOptions
Setting options for loading raster layers.
Definition: qgsrasterlayer.h:109
QgsBrightnessContrastFilter
Brightness/contrast and gamma correction filter pipe for rasters.
Definition: qgsbrightnesscontrastfilter.h:31
QgsMapToPixel
Perform transforms between map coordinates and device coordinates.
Definition: qgsmaptopixel.h:38
str
#define str(x)
Definition: qgis.cpp:37
QgsRasterLayer::pipe
QgsRasterPipe * pipe()
Returns the raster pipe.
Definition: qgsrasterlayer.h:295
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:91
QgsMapLayer
Base class for all map layer types. This is the base class for all map layer types (vector,...
Definition: qgsmaplayer.h:72
QgsRasterResampleFilter
Resample filter pipe for rasters.
Definition: qgsrasterresamplefilter.h:32
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:94
QgsLegendColorList
QList< QPair< QString, QColor > > QgsLegendColorList
Definition: qgsrasterlayer.h:55
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:97
QgsRaster
Raster namespace.
Definition: qgsraster.h:31
QgsRasterLayer::rasterType
LayerType rasterType()
Returns the raster layer type (which is a read only property).
Definition: qgsrasterlayer.h:235
QgsRasterDataProvider
Base class for raster data providers.
Definition: qgsrasterdataprovider.h:88
QgsAbstractProfileSource
Interface for classes which can generate elevation profiles.
Definition: qgsabstractprofilesource.h:33
QgsRasterLayer::PseudoColorShader
@ PseudoColorShader
Definition: qgsrasterlayer.h:188
qgsrasterdataprovider.h