Class: QgsRasterLayer¶
- class qgis.core.QgsRasterLayer¶
Bases:
QgsMapLayer
Represents a raster layer.
A
QgsRasterLayer
is instantiated by specifying the name of a data provider, such as “gdal” or “wms”, and a url defining the specific data set to connect to. The raster layer constructor in turn instantiates aQgsRasterDataProvider
subclass corresponding to the provider type, and passes it the url. The data provider connects to the data source.Sample usage of the
QgsRasterLayer
class:my_raster_layer = QgsRasterLayer("/path/to/file.tif", "my layer")
QgsRasterLayer() Constructor. Provider is not set.
QgsRasterLayer(uri: str, baseName: str = ‘’, providerType: str = ‘’, options: QgsRasterLayer.LayerOptions = QgsRasterLayer.LayerOptions()) This is the constructor for the RasterLayer class.
The main tasks carried out by the constructor are:
Load the rasters default style (.qml) file if it exists
Populate the RasterStatsVector with initial values for each band.
Calculate the layer extents
Determine whether the layer is gray, paletted or multiband.
Assign sensible defaults for the red, green, blue and gray bands.
Methods
- param visitor
Add error message
Returns the number of bands in this layer.
Returns the name of a band given its number.
Returns the raster's brightness/contrast filter.
Returns a new instance equivalent to this one.
Returns the source data provider.
- param rendererContext
Returns the source data provider.
- param source
This is an overloaded version of the
draw()
function that is called by bothdraw()
and thumbnailAsPixmap- param source
Checks whether a new set of dependencies will introduce a cycle this method is now deprecated and always return
False
, because circular dependencies are now correctly managed.Returns the height of the (unclipped) raster.
- rtype
str
Returns the raster's hue/saturation filter.
If the ignoreExtent flag is set, the layer will also render outside the bounding box reported by the data provider.
Invalidates the WGS84 extent.
- rtype
bool
This helper checks to see whether the file name appears to be a valid raster file name.
Returns time stamp for given file name
Returns a list with classification items (Text and color).
- rtype
float
Returns a 100x100 pixmap of the color palette.
Returns the raster pipe.
Draws a preview of the rasterlayer into a QImage
[ data provider interface ] Which provider is being used for this Raster Layer?
Returns the raster layer type (which is a read only property).
Returns the number of raster units per each raster pixel in X axis.
Returns the number of raster units per each raster pixel in Y axis.
Read style data common to all layer types
Read custom properties from project file.
- param node
Read style manager's configuration (if any).
- param node
- param layer_node
Returns the raster's renderer.
Returns the raster's resample filter.
Returns which stage of the pipe should apply resampling.
Set contrast enhancement algorithm
Set the data provider.
Sets the default contrast enhancement
Sets error message
Sets the extent
- param layers
- param opacity
Sets the
providerType
(provider key)Sets the raster's
renderer
.Select which stage of the pipe should apply resampling.
- param name
Sets the string (typically sql) used to define a subset of the layer
Sets the coordinate transform context to
transformContext
Sets whether layer is valid or not
- param message
- rtype
List[str]
Returns the string (typically sql) used to define a subset of the layer.
- rtype
QgsMapLayerTemporalProperties
- rtype
QDateTime
Returns the width of the (unclipped) raster.
Write style data common to all layer types
Write custom properties to project file.
Writes the symbology of the layer into the document provided in SLD 1.0.0 format
- param node
Write style manager's configuration (if exists).
- param a0
- param layer_node
Signals
Emitted when the layer's subset string has changed.
Attributes
- ColorLayer = 3¶
- ColorRampShader = 3¶
- class ColorShadingAlgorithm¶
Bases:
int
- FreakOutShader = 2¶
- GrayOrUndefined = 0¶
- class LayerOptions(loadDefaultStyle: bool = True, transformContext: QgsCoordinateTransformContext = QgsCoordinateTransformContext())¶
Bases:
sip.wrapper
Constructor for LayerOptions.
QgsRasterLayer.LayerOptions(QgsRasterLayer.LayerOptions)
- loadDefaultStyle¶
- skipCrsValidation¶
- transformContext¶
- class LayerType¶
Bases:
int
- MULTIPLE_BAND_MULTI_BYTE_ENHANCEMENT_ALGORITHM = 1¶
- MULTIPLE_BAND_MULTI_BYTE_MIN_MAX_LIMITS = 3¶
- MULTIPLE_BAND_SINGLE_BYTE_ENHANCEMENT_ALGORITHM = 0¶
- MULTIPLE_BAND_SINGLE_BYTE_MIN_MAX_LIMITS = 1¶
- Multiband = 2¶
- Palette = 1¶
- PseudoColorShader = 1¶
- SAMPLE_SIZE = 250000.0¶
- SINGLE_BAND_ENHANCEMENT_ALGORITHM = 1¶
- SINGLE_BAND_MIN_MAX_LIMITS = 1¶
- UndefinedShader = 0¶
- UserDefinedShader = 4¶
- accept(self, visitor: QgsStyleEntityVisitorInterface) bool ¶
- Parameters
visitor (QgsStyleEntityVisitorInterface) –
- Return type
bool
- appendError(self, error: QgsErrorMessage)¶
Add error message
- bandCount(self) int ¶
Returns the number of bands in this layer.
- Return type
int
- bandName(self, bandNoInt: int) str ¶
Returns the name of a band given its number.
- Parameters
bandNoInt (int) –
- Return type
str
- brightnessFilter(self) QgsBrightnessContrastFilter ¶
Returns the raster’s brightness/contrast filter.
See also
See also
- Return type
- childEvent(self, QChildEvent)¶
- clone(self) QgsRasterLayer ¶
Returns a new instance equivalent to this one. A new provider is created for the same data source and renderer is cloned too.
- Return type
- Returns
a new layer instance
New in version 3.0.
- connectNotify(self, QMetaMethod)¶
- constDataProvider(self) QgsRasterDataProvider ¶
Returns the source data provider.
This will be
None
if the layer is invalid.- Return type
- createMapRenderer(self, rendererContext: QgsRenderContext) QgsMapLayerRenderer ¶
- Parameters
rendererContext (QgsRenderContext) –
- Return type
- customEvent(self, QEvent)¶
- dataProvider(self) QgsRasterDataProvider ¶
Returns the source data provider.
This will be
None
if the layer is invalid.- Return type
- decodedSource(self, source: str, provider: str, context: QgsReadWriteContext) str ¶
- Parameters
source (str) –
provider (str) –
context (QgsReadWriteContext) –
- Return type
str
- disconnectNotify(self, QMetaMethod)¶
- draw(self, theQPainter: QPainter, myRasterViewPort: QgsRasterViewPort, qgsMapToPixel: QgsMapToPixel = None)¶
This is an overloaded version of the
draw()
function that is called by bothdraw()
and thumbnailAsPixmap- Parameters
theQPainter (QPainter) –
myRasterViewPort (QgsRasterViewPort) –
qgsMapToPixel (
QgsMapToPixel
= None) –
- encodedSource(self, source: str, context: QgsReadWriteContext) str ¶
- Parameters
source (str) –
context (QgsReadWriteContext) –
- Return type
str
- hasDependencyCycle(self, Iterable[QgsMapLayerDependency]) bool ¶
Checks whether a new set of dependencies will introduce a cycle this method is now deprecated and always return
False
, because circular dependencies are now correctly managed.Deprecated since version QGIS: 3.10
- htmlMetadata(self) str ¶
- Return type
str
- hueSaturationFilter(self) QgsHueSaturationFilter ¶
Returns the raster’s hue/saturation filter.
See also
See also
- Return type
- ignoreExtents(self) bool ¶
If the ignoreExtent flag is set, the layer will also render outside the bounding box reported by the data provider. To be used for example for WMS layers with labels or symbology that happens to be drawn outside the data extent.
New in version 3.10.
- Return type
bool
- invalidateWgs84Extent(self)¶
Invalidates the WGS84 extent. If FlagTrustLayerMetadata is enabled, the extent is not invalidated because we want to trust metadata whatever happens.
New in version 3.20.
- isSignalConnected(self, QMetaMethod) bool ¶
- isSpatial(self) bool ¶
- Return type
bool
- isValidRasterFileName(fileNameQString: str, retError: str) bool ¶
This helper checks to see whether the file name appears to be a valid raster file name. If the file name looks like it could be valid, but some sort of error occurs in processing the file, the error is returned in
retError
.isValidRasterFileName(fileNameQString: str) -> bool
- Parameters
fileNameQString (str) –
retError (str) –
- Return type
bool
- lastModified(name: str) QDateTime ¶
Returns time stamp for given file name
- Parameters
name (str) –
- Return type
QDateTime
- legendSymbologyItems(self) List[Tuple[str, QColor]] ¶
Returns a list with classification items (Text and color).
Deprecated since version use:
QgsRasterRenderer.createLegendNodes()
instead.- Return type
List[Tuple[str, QColor]]
- opacity(self) float ¶
- Return type
float
- paletteAsPixmap(self, bandNumber: int = 1) QPixmap ¶
Returns a 100x100 pixmap of the color palette. If the layer has no palette a white pixmap will be returned
- Parameters
bandNumber (int = 1) – the number of the band to use for generating a pixmap of the associated palette
- Return type
QPixmap
- pipe(self) QgsRasterPipe ¶
Returns the raster pipe.
- Return type
- previewAsImage(self, size: QSize, bgColor: Union[QColor, Qt.GlobalColor, QGradient] = Qt.white, format: QImage.Format = QImage.Format_ARGB32_Premultiplied) QImage ¶
Draws a preview of the rasterlayer into a QImage
New in version 2.4.
- Parameters
size (QSize) –
bgColor (Union[QColor) –
format (QImage.Format = QImage.Format_ARGB32_Premultiplied) –
- Return type
QImage
- providerType(self) str ¶
[ data provider interface ] Which provider is being used for this Raster Layer?
- Return type
str
- rasterType(self) QgsRasterLayer.LayerType ¶
Returns the raster layer type (which is a read only property).
- Return type
- rasterUnitsPerPixelX(self) float ¶
Returns the number of raster units per each raster pixel in X axis.
In a world file, this is normally the first row (without the sign). (E.g. the value reported by the GDAL geotransform[1]).
See also
- Return type
float
- rasterUnitsPerPixelY(self) float ¶
Returns the number of raster units per each raster pixel in Y axis.
In a world file, this is normally the first row (without the sign).
See also
- Return type
float
- readCommonStyle(self, layerElement: QDomElement, context: QgsReadWriteContext, categories: Union[QgsMapLayer.StyleCategories, QgsMapLayer.StyleCategory] = QgsMapLayer.AllStyleCategories)¶
Read style data common to all layer types
New in version 3.0.
- readCustomProperties(self, layerNode: QDomNode, keyStartsWith: str = '')¶
Read custom properties from project file.
- Parameters
layerNode – note to read from
keyStartsWith – reads only properties starting with the specified string (or all if the string is empty)
- readStyle(self, node: QDomNode, errorMessage: str, context: QgsReadWriteContext, categories: Union[QgsMapLayer.StyleCategories, QgsMapLayer.StyleCategory] = QgsMapLayer.AllStyleCategories) bool ¶
- Parameters
node (QDomNode) –
errorMessage (str) –
context (QgsReadWriteContext) –
categories (Union[QgsMapLayer.StyleCategories) –
- Return type
bool
- readStyleManager(self, layerNode: QDomNode)¶
Read style manager’s configuration (if any). To be called by subclasses.
- readSymbology(self, node: QDomNode, errorMessage: str, context: QgsReadWriteContext, categories: Union[QgsMapLayer.StyleCategories, QgsMapLayer.StyleCategory] = QgsMapLayer.AllStyleCategories) bool ¶
- Parameters
node (QDomNode) –
errorMessage (str) –
context (QgsReadWriteContext) –
categories (Union[QgsMapLayer.StyleCategories) –
- Return type
bool
- readXml(self, layer_node: QDomNode, context: QgsReadWriteContext) bool ¶
- Parameters
layer_node (QDomNode) –
context (QgsReadWriteContext) –
- Return type
bool
- receivers(self, PYQT_SIGNAL) int ¶
- reload(self)¶
- renderer(self) QgsRasterRenderer ¶
Returns the raster’s renderer.
See also
- Return type
- resampleFilter(self) QgsRasterResampleFilter ¶
Returns the raster’s resample filter.
See also
See also
- Return type
- resamplingStage(self) Qgis.RasterResamplingStage ¶
Returns which stage of the pipe should apply resampling.
See also
New in version 3.16.
- Return type
- sender(self) QObject ¶
- senderSignalIndex(self) int ¶
- setContrastEnhancement(self, algorithm: QgsContrastEnhancement.ContrastEnhancementAlgorithm, limits: QgsRasterMinMaxOrigin.Limits = QgsRasterMinMaxOrigin.MinMax, extent: QgsRectangle = QgsRectangle(), sampleSize: int = QgsRasterLayer.SAMPLE_SIZE, generateLookupTableFlag: bool = True)¶
Set contrast enhancement algorithm
- Parameters
algorithm (QgsContrastEnhancement.ContrastEnhancementAlgorithm) – Contrast enhancement algorithm
limits (QgsRasterMinMaxOrigin.Limits = QgsRasterMinMaxOrigin.MinMax) – Limits
extent (
QgsRectangle
= QgsRectangle()) – Extent used to calculate limits, if empty, use full layer extentsampleSize (int = QgsRasterLayer.SAMPLE_SIZE) – Size of data sample to calculate limits, if 0, use full resolution
generateLookupTableFlag (bool = True) – Generate lookup table.
- setDataProvider(self, provider: str)¶
Set the data provider.
Deprecated since version Use: the version with ProviderOptions instead.
setDataProvider(self, provider: str, options: QgsDataProvider.ProviderOptions, flags: Union[QgsDataProvider.ReadFlags, QgsDataProvider.ReadFlag] = QgsDataProvider.ReadFlags()) Set the data provider.
- Parameters
provider (str) – provider key string, must match a valid
QgsRasterDataProvider
key. E.g. “gdal”, “wms”, etc.options – provider options
flags – provider flags since QGIS 3.16
New in version 3.2.
- setDefaultContrastEnhancement(self)¶
Sets the default contrast enhancement
- setExtent(self, rect: QgsRectangle)¶
Sets the extent
- setLayerOrder(self, layers: Iterable[str])¶
- Parameters
layers (Iterable[str]) –
- setOpacity(self, opacity: float)¶
- Parameters
opacity (float) –
- setProviderType(self, providerType: str)¶
Sets the
providerType
(provider key)
- setRenderer(self, renderer: QgsRasterRenderer)¶
Sets the raster’s
renderer
. Takes ownership of the renderer object.See also
- Parameters
renderer (QgsRasterRenderer) –
- setResamplingStage(self, stage: Qgis.RasterResamplingStage)¶
Select which stage of the pipe should apply resampling.
See also
New in version 3.16.
- Parameters
stage (Qgis.RasterResamplingStage) –
- setSubLayerVisibility(self, name: str, vis: bool)¶
- Parameters
name (str) –
vis (bool) –
- setSubsetString(self, subset: str) bool ¶
Sets the string (typically sql) used to define a subset of the layer
- Parameters
subset (str) – The subset string. This may be the where clause of a sql statement or other definition string specific to the underlying dataprovider and data store.
- Return type
bool
- Returns
True
, when setting the subset string was successful,False
otherwise
New in version 3.12.
- setTransformContext(self, transformContext: QgsCoordinateTransformContext)¶
Sets the coordinate transform context to
transformContext
New in version 3.8.
- Parameters
transformContext (QgsCoordinateTransformContext) –
- setValid(self, valid: bool)¶
Sets whether layer is valid or not
- showStatusMessage(self, message: str)¶
- Parameters
message (str) –
- subLayers(self) List[str] ¶
- Return type
List[str]
- subsetString(self) str ¶
Returns the string (typically sql) used to define a subset of the layer.
- Return type
str
- Returns
The subset string or null QString if not implemented by the provider
New in version 3.12.
- subsetStringChanged¶
Emitted when the layer’s subset string has changed.
New in version 3.12: [signal]
- temporalProperties(self) QgsMapLayerTemporalProperties ¶
- Return type
- timerEvent(self, QTimerEvent)¶
- timestamp(self) QDateTime ¶
- Return type
QDateTime
- writeCommonStyle(self, layerElement: QDomElement, document: QDomDocument, context: QgsReadWriteContext, categories: Union[QgsMapLayer.StyleCategories, QgsMapLayer.StyleCategory] = QgsMapLayer.AllStyleCategories)¶
Write style data common to all layer types
New in version 3.0.
- writeCustomProperties(self, layerNode: QDomNode, doc: QDomDocument)¶
Write custom properties to project file.
- writeSld(self, node: QDomNode, doc: QDomDocument, errorMessage: str, props: Dict[str, Any] = {}) bool ¶
Writes the symbology of the layer into the document provided in SLD 1.0.0 format
- Parameters
node (QDomNode) – the node that will have the style element added to it.
doc (QDomDocument) – the document that will have the QDomNode added.
errorMessage (str) – reference to string that will be updated with any error messages
props (Dict[str) – a open ended set of properties that can drive/inform the SLD encoding
- Return type
bool
- Returns
True
in case of success
New in version 3.6.
- writeStyle(self, node: QDomNode, doc: QDomDocument, errorMessage: str, context: QgsReadWriteContext, categories: Union[QgsMapLayer.StyleCategories, QgsMapLayer.StyleCategory] = QgsMapLayer.AllStyleCategories) bool ¶
- Parameters
node (QDomNode) –
doc (QDomDocument) –
errorMessage (str) –
context (QgsReadWriteContext) –
categories (Union[QgsMapLayer.StyleCategories) –
- Return type
bool
- writeStyleManager(self, layerNode: QDomNode, doc: QDomDocument)¶
Write style manager’s configuration (if exists). To be called by subclasses.
- writeSymbology(self, a0: QDomNode, doc: QDomDocument, errorMessage: str, context: QgsReadWriteContext, categories: Union[QgsMapLayer.StyleCategories, QgsMapLayer.StyleCategory] = QgsMapLayer.AllStyleCategories) bool ¶
- Parameters
a0 (QDomNode) –
doc (QDomDocument) –
errorMessage (str) –
context (QgsReadWriteContext) –
categories (Union[QgsMapLayer.StyleCategories) –
- Return type
bool
- writeXml(self, layer_node: QDomNode, doc: QDomDocument, context: QgsReadWriteContext) bool ¶
- Parameters
layer_node (QDomNode) –
doc (QDomDocument) –
context (QgsReadWriteContext) –
- Return type
bool