Class: QgsRasterBlock

Raster data container.

Methods

applyNoDataValues

applyScaleOffset

Apply band scale and offset to raster block values

as_numpy

Returns the block data as a numpy array.

color

Read a single color

convert

Convert data to different type.

data

Gets access to raw data.

dataType

Returns data type

dataTypeSize

Data type size in bytes.

error

Returns the last error

fill

Fills the whole block with a constant value.

hasNoData

Returns True if the block may contain no data.

hasNoDataValue

True if the block has no data value.

height

Returns the height (number of rows) of the raster block.

image

Returns an image containing the block data, if the block's data type is color.

isEmpty

Returns True if block is empty, i.e. its size is 0 (zero rows or cols).

isNoData

Checks if value at position is no data

isValid

Returns True if the block is valid (correctly filled with data).

maximum

Returns the maximum value present in the raster block.

minimum

Returns the minimum value present in the raster block.

minimumMaximum

Returns the minimum and maximum value present in the raster block.

noDataValue

Returns no data value.

reset

Reset block

resetNoDataValue

Reset no data value: if there was a no data value previously set, it will be discarded.

setColor

Set color on position

setData

Rewrite raw pixel data.

setError

Sets the last error

setImage

Sets the block data via an image.

setIsData

Remove no data flag on pixel.

setIsNoData

Set no data on pixel

setIsNoDataExcept

Set the whole block to no data except specified rectangle

setNoDataValue

Sets cell value that will be considered as "no data".

setValid

Mark block as valid or invalid

setValue

Set value on position

toString

value

Read a single value if type of block is numeric.

width

Returns the width (number of columns) of the raster block.

Static Methods

printValue

Print double value with all necessary significant digits.

subRect

For extent and width, height find rectangle covered by subextent.

typeIsColor

Returns True if a data type is a color type.

typeIsComplex

Returns True if a data type is a complex number type.

typeIsNumeric

Returns True if a data type is numeric.

typeSize

Returns the size in bytes for the specified dataType.

typeWithNoDataValue

For given data type returns wider type and sets no data value

valueBytes

Gets byte array representing a value.

class qgis.core.QgsRasterBlock[source]

Bases: object

__init__()
__init__(dataType: Qgis.DataType, width: int, height: int)

Constructor which allocates data block in memory

Parameters:
  • dataType (Qgis.DataType) – raster data type

  • width (int) – width of data matrix

  • height (int) – height of data matrix

__init__(a0: QgsRasterBlock)
Parameters:

a0 (QgsRasterBlock)

applyNoDataValues(self, rangeList: Iterable[QgsRasterRange])[source]
Parameters:

rangeList (Iterable[QgsRasterRange])

applyScaleOffset(self, scale: float, offset: float)[source]

Apply band scale and offset to raster block values

Parameters:
  • scale (float)

  • offset (float)

as_numpy(use_masking: bool = True) ndarray | MaskedArray

Returns the block data as a numpy array.

If use_masking is True then the returned array will be a numpy masked array, masking the raster block’s nodata values.

Raises:

QgsNotSupportedException – if numpy is not available on the system

Added in version 3.40.

color(self, row: int, column: int) int[source]

Read a single color

Parameters:
  • row (int) – row index

  • column (int) – column index

Return type:

int

Returns:

color

color(self, index: int) int[source]

Read a single value

Parameters:

index (int) – data matrix index (long type in Python)

Return type:

int

Returns:

color

convert(self, destDataType: Qgis.DataType) bool[source]

Convert data to different type.

Parameters:

destDataType (Qgis.DataType) – dest data type

Return type:

bool

Returns:

True on success

data(self) QByteArray[source]

Gets access to raw data. The returned QByteArray instance is not a copy of the data: it only refers to the array owned by the QgsRasterBlock, therefore it is only valid while the QgsRasterBlock object still exists. Writing to the returned QByteArray will not affect the original data: a deep copy of the data will be made and only the local copy will be modified.

Note

in Python the method returns ordinary bytes object as the

Return type:

QByteArray

dataType(self) Qgis.DataType[source]

Returns data type

Return type:

Qgis.DataType

dataTypeSize(self) int[source]

Data type size in bytes.

Return type:

int

error(self) QgsError[source]

Returns the last error

Return type:

QgsError

fill(self, value: float)[source]

Fills the whole block with a constant value.

This method only applies to numeric raster blocks, not color blocks or complex number data types.

Raises:

ValueError – if the block is an empty, non-numeric or complex number type raster block.

Added in version 3.44.

Parameters:

value (float)

hasNoData(self) bool[source]

Returns True if the block may contain no data. It does not guarantee that it really contains any no data. It can be used to speed up processing. Not the difference between this method and hasNoDataValue().

Return type:

bool

Returns:

True if the block may contain no data

hasNoDataValue(self) bool[source]

True if the block has no data value.

Return type:

bool

Returns:

True if the block has no data value

See also

noDataValue()

See also

setNoDataValue()

height(self) int[source]

Returns the height (number of rows) of the raster block.

See also

width()

Return type:

int

image(self) QImage[source]

Returns an image containing the block data, if the block’s data type is color.

Return type:

QImage

isEmpty(self) bool[source]

Returns True if block is empty, i.e. its size is 0 (zero rows or cols). This method does not return True if size is not zero and all values are ‘no data’ (null).

Return type:

bool

isNoData(self, row: int, column: int) bool[source]

Checks if value at position is no data

Parameters:
  • row (int) – row index

  • column (int) – column index

Return type:

bool

Returns:

True if value is no data

See also

valueAndNoData()

isNoData(self, row: int, column: int) bool[source]

Check if value at position is no data

Parameters:
  • row (int) – row index

  • column (int) – column index

Return type:

bool

Returns:

True if value is no data

See also

valueAndNoData()

isNoData(self, index: int) bool[source]

Check if value at position is no data

Parameters:

index (int) – data matrix index (long type in Python)

Return type:

bool

Returns:

True if value is no data

See also

valueAndNoData()

isValid(self) bool[source]

Returns True if the block is valid (correctly filled with data). An empty block may still be valid (if zero size block was requested). If the block is not valid, error may be retrieved by error() method.

Return type:

bool

maximum(self)[source]

Returns the maximum value present in the raster block.

Note

If the maximum value is present multiple times in the raster block then the calculated row and column will refer to any instance only of this maximum.

Returns:

  • True if a maximum value was found, or False if it could not be found (eg due to non-numeric data types).

  • maximum: maximum value present

  • row: row containing maximum value pixel

  • column: column containing maximum value pixel

See also

minimum()

See also

minimumMaximum()

Added in version 3.42.

minimum(self)[source]

Returns the minimum value present in the raster block.

Note

If the minimum value is present multiple times in the raster block then the calculated row and column will refer to any instance of this minimum.

Returns:

  • True if a minimum value was found, or False if it could not be found (eg due to non-numeric data types).

  • minimum: minimum value present

  • row: row containing minimum value pixel

  • column: column containing minimum value pixel

See also

maximum()

See also

minimumMaximum()

Added in version 3.42.

minimumMaximum(self)[source]

Returns the minimum and maximum value present in the raster block.

Note

This method is more efficient than calling minimum() and maximum() separately.

Note

If the minimum or maximum value is present multiple times in the raster block then the calculated row and column will refer to any of instances of these values.

Returns:

  • True if a minimum and maximum value were found, or False if they could not be found (eg due to non-numeric data types).

  • minimum: minimum value present

  • minimumRow: row containing minimum value pixel

  • minimumColumn: column containing minimum value pixel

  • maximum: maximum value present

  • maximumRow: row containing maximum value pixel

  • maximumColumn: column containing maximum value pixel

See also

minimum()

See also

maximum()

Added in version 3.42.

noDataValue(self) float[source]

Returns no data value. If the block does not have a no data value the returned value is undefined.

Return type:

float

Returns:

No data value

See also

hasNoDataValue()

See also

setNoDataValue()

static printValue(value: float, localized: bool = False) str[source]

Print double value with all necessary significant digits. It is ensured that conversion back to double gives the same number.

Parameters:
  • value (float) – the value to be printed

  • localized (bool = False) – if True, use localized number format

Return type:

str

Returns:

string representing the value

reset(self, dataType: Qgis.DataType, width: int, height: int) bool[source]

Reset block

Parameters:
  • dataType (Qgis.DataType) – raster data type

  • width (int) – width of data matrix

  • height (int) – height of data matrix

Return type:

bool

Returns:

True on success

resetNoDataValue(self)[source]

Reset no data value: if there was a no data value previously set, it will be discarded.

See also

noDataValue()

See also

hasNoDataValue()

See also

setNoDataValue()

setColor(self, row: int, column: int, color: int) bool[source]

Set color on position

Parameters:
  • row (int) – row index

  • column (int) – column index

  • color (int) – the color to be set, QRgb value

Return type:

bool

Returns:

True on success

setColor(self, index: int, color: int) bool[source]

Set color on index (indexed line by line)

Parameters:
  • index (int) – data matrix index (long type in Python)

  • color (int) – the color to be set, QRgb value

Return type:

bool

Returns:

True on success

setData(self, data: QByteArray | bytes | bytearray, offset: int = 0)[source]

Rewrite raw pixel data. If the data array is shorter than the internal array within the raster block object, pixels at the end will stay untouched. If the data array is longer than the internal array, only the initial data from the input array will be used. Optionally it is possible to set non-zero offset (in bytes) if the input data should overwrite data somewhere in the middle of the internal buffer.

Parameters:
  • data (Union[QByteArray, bytes, bytearray])

  • offset (int = 0)

setError(self, error: QgsError)[source]

Sets the last error

Parameters:

error (QgsError)

setImage(self, image: QImage | None) bool[source]

Sets the block data via an image.

Return type:

bool

Returns:

True on success

Parameters:

image (Optional[QImage])

setIsData(self, row: int, column: int)[source]

Remove no data flag on pixel. If the raster block does not have an explicit no data value set then an internal map of no data pixels is maintained for the block. In this case it is possible to reset a pixel to flag it as having valid data using this method. This method has no effect for raster blocks with an explicit no data value set.

Parameters:
  • row (int) – row index

  • column (int) – column index

setIsData(self, index: int)[source]

Remove no data flag on pixel. If the raster block does not have an explicit no data value set then an internal map of no data pixels is maintained for the block. In this case it is possible to reset a pixel to flag it as having valid data using this method. This method has no effect for raster blocks with an explicit no data value set.

Parameters:

index (int) – data matrix index (long type in Python)

setIsNoData(self, row: int, column: int) bool[source]

Set no data on pixel

Parameters:
  • row (int) – row index

  • column (int) – column index

Return type:

bool

Returns:

True on success

setIsNoData(self, index: int) bool[source]

Set no data on pixel

Parameters:

index (int) – data matrix index (long type in Python)

Return type:

bool

Returns:

True on success

setIsNoData(self) bool[source]

Set the whole block to no data

Return type:

bool

Returns:

True on success

setIsNoDataExcept(self, exceptRect: QRect) bool[source]

Set the whole block to no data except specified rectangle

Return type:

bool

Returns:

True on success

Parameters:

exceptRect (QRect)

setNoDataValue(self, noDataValue: float)[source]

Sets cell value that will be considered as “no data”.

See also

noDataValue()

See also

hasNoDataValue()

Parameters:

noDataValue (float)

setValid(self, valid: bool)[source]

Mark block as valid or invalid

Parameters:

valid (bool)

setValue(self, row: int, column: int, value: float) bool[source]

Set value on position

Parameters:
  • row (int) – row index

  • column (int) – column index

  • value (float) – the value to be set

Return type:

bool

Returns:

True on success

setValue(self, index: int, value: float) bool[source]

Set value on index (indexed line by line)

Parameters:
  • index (int) – data matrix index (long type in Python)

  • value (float) – the value to be set

Return type:

bool

Returns:

True on success

static subRect(extent: QgsRectangle, width: int, height: int, subExtent: QgsRectangle) QRect[source]

For extent and width, height find rectangle covered by subextent. The output rect has x oriented from left to right and y from top to bottom (upper-left to lower-right orientation).

Parameters:
  • extent (QgsRectangle) – extent, usually the larger

  • width (int) – numbers of columns in theExtent

  • height (int) – numbers of rows in theExtent

  • subExtent (QgsRectangle) – extent, usually smaller than theExtent

Return type:

QRect

Returns:

the rectangle covered by sub extent

toString(self) str[source]
Return type:

str

static typeIsColor(type: Qgis.DataType) bool[source]

Returns True if a data type is a color type.

See also

typeIsNumeric()

See also

typeIsComplex()

Parameters:

type (Qgis.DataType)

Return type:

bool

static typeIsComplex(type: Qgis.DataType) bool[source]

Returns True if a data type is a complex number type.

See also

typeIsNumeric()

See also

typeIsColor()

Added in version 3.44.

Parameters:

type (Qgis.DataType)

Return type:

bool

static typeIsNumeric(type: Qgis.DataType) bool[source]

Returns True if a data type is numeric.

See also

typeIsComplex()

See also

typeIsColor()

Parameters:

type (Qgis.DataType)

Return type:

bool

static typeSize(dataType: Qgis.DataType) int[source]

Returns the size in bytes for the specified dataType.

Parameters:

dataType (Qgis.DataType)

Return type:

int

static typeWithNoDataValue(dataType: Qgis.DataType)[source]

For given data type returns wider type and sets no data value

Parameters:

dataType (Qgis.DataType) -> (Qgis.DataType)

value(self, row: int, column: int) float[source]

Read a single value if type of block is numeric. If type is color, returned value is undefined.

Parameters:
  • row (int) – row index

  • column (int) – column index

Return type:

float

Returns:

value

See also

valueAndNoData()

value(self, index: int) float[source]

Reads a single value if type of block is numeric. If type is color, returned value is undefined.

Parameters:

index (int) – data matrix index (long type in Python)

Return type:

float

Returns:

value

See also

valueAndNoData()

static valueBytes(dataType: Qgis.DataType, value: float) QByteArray[source]

Gets byte array representing a value.

Parameters:
Return type:

QByteArray

Returns:

byte array representing the value

width(self) int[source]

Returns the width (number of columns) of the raster block.

See also

height()

Return type:

int