Class: QgsRasterBlock

class qgis.core.QgsRasterBlock

Bases: sip.wrapper

Raster data container.

QgsRasterBlock()

QgsRasterBlock(dataType: Qgis.DataType, width: int, height: int) Constructor which allocates data block in memory

Parameters:
  • dataType – raster data type

  • width – width of data matrix

  • height – height of data matrix

QgsRasterBlock(QgsRasterBlock)

Methods

applyNoDataValues

param rangeList:

applyScaleOffset

Apply band scale and offset to raster block values

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

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).

noDataValue

Returns no data value.

printValue

Print double value with all necessary significant digits.

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

subRect

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

toString

rtype:

str

typeIsColor

Returns True if data type is color

typeIsNumeric

Returns True if 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

value

Read a single value if type of block is numeric.

valueBytes

Gets byte array representing a value.

width

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

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

rangeList (Iterable[QgsRasterRange]) –

applyScaleOffset(self, scale: float, offset: float)

Apply band scale and offset to raster block values

New in version 2.3.

Parameters:
  • scale (float) –

  • offset (float) –

color(self, row: int, column: int) int

Read a single color

Parameters:
  • row (int) – row index

  • column (int) – column index

Returns:

color

color(self, index: int) -> int Read a single value

Parameters:

index – data matrix index (long type in Python)

Return type:

int

Returns:

color

convert(self, destDataType: Qgis.DataType) bool

Convert data to different type.

Parameters:

destDataType (Qgis.DataType) – dest data type

Return type:

bool

Returns:

True on success

data(self) QByteArray

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

New in version 3.0.

Return type:

QByteArray

dataType(self) Qgis.DataType

Returns data type

Return type:

Qgis.DataType

dataTypeSize(self) int

Data type size in bytes.

Return type:

int

error(self) QgsError

Returns the last error

Return type:

QgsError

hasNoData(self) bool

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

True if the block has no data value.

Return type:

bool

Returns:

True if the block has no data value

See also

noDataValue()

height(self) int

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

See also

width()

New in version 2.10.

Return type:

int

image(self) QImage

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

Return type:

QImage

isEmpty(self) bool

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

Checks if value at position is no data

Parameters:
  • row (int) – row index

  • column (int) – column index

Returns:

True if value is no data

See also

valueAndNoData()

isNoData(self, row: int, column: int) -> bool Check if value at position is no data

Parameters:
  • row – row index

  • column – column index

Returns:

True if value is no data

See also

valueAndNoData()

isNoData(self, index: int) -> bool Check if value at position is no data

Parameters:

index – data matrix index (long type in Python)

Return type:

bool

Returns:

True if value is no data

See also

valueAndNoData()

isValid(self) bool

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

noDataValue(self) float

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()

printValue(value: float) str

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

Return type:

str

Returns:

string representing the value

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

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)

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

See also

noDataValue()

New in version 3.0.

setColor(self, row: int, column: int, color: int) bool

Set color on position

Parameters:
  • row (int) – row index

  • column (int) – column index

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

Returns:

True on success

setColor(self, index: int, color: int) -> bool Set color on index (indexed line by line)

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

  • color – the color to be set, QRgb value

Return type:

bool

Returns:

True on success

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

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.

New in version 3.0.

Parameters:
  • data (Union[QByteArray) –

  • offset (int = 0) –

setError(self, error: QgsError)

Sets the last error

Parameters:

error (QgsError) –

setImage(self, image: QImage) bool

Sets the block data via an image.

Return type:

bool

Returns:

True on success

Parameters:

image (QImage) –

setIsData(self, row: int, column: int)

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

New in version 2.10.

setIsData(self, index: int) 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 – data matrix index (long type in Python)

New in version 2.10.

setIsNoData(self, row: int, column: int) bool

Set no data on pixel

Parameters:
  • row (int) – row index

  • column (int) – column index

Returns:

True on success

setIsNoData(self, index: int) -> bool Set no data on pixel

Parameters:

index – data matrix index (long type in Python)

Returns:

True on success

setIsNoData(self) -> bool Set the whole block to no data

Return type:

bool

Returns:

True on success

setIsNoDataExcept(self, exceptRect: QRect) bool

Set the whole block to no data except specified rectangle

Return type:

bool

Returns:

True on success

Parameters:

exceptRect (QRect) –

setNoDataValue(self, noDataValue: float)

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

See also

noDataValue()

New in version 3.0.

Parameters:

noDataValue (float) –

setValid(self, valid: bool)

Mark block as valid or invalid

Parameters:

valid (bool) –

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

Set value on position

Parameters:
  • row (int) – row index

  • column (int) – column index

  • value (float) – the value to be set

Returns:

True on success

setValue(self, index: int, value: float) -> bool Set value on index (indexed line by line)

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

  • value – the value to be set

Return type:

bool

Returns:

True on success

subRect(extent: QgsRectangle, width: int, height: int, subExtent: QgsRectangle) QRect

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
Return type:

str

typeIsColor(type: Qgis.DataType) bool

Returns True if data type is color

Parameters:

type (Qgis.DataType) –

Return type:

bool

typeIsNumeric(type: Qgis.DataType) bool

Returns True if data type is numeric

Parameters:

type (Qgis.DataType) –

Return type:

bool

typeSize(dataType: Qgis.DataType) int

Returns the size in bytes for the specified dataType.

Parameters:

dataType (Qgis.DataType) –

Return type:

int

typeWithNoDataValue(dataType: Qgis.DataType) Tuple[Qgis.DataType, float]

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

Parameters:

dataType (Qgis.DataType) –

Return type:

Tuple[Qgis.DataType, float]

value(self, row: int, column: int) float

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

Returns:

value

See also

valueAndNoData()

value(self, index: int) -> float Reads a single value if type of block is numeric. If type is color, returned value is undefined.

Parameters:

index – data matrix index (long type in Python)

Return type:

float

Returns:

value

See also

valueAndNoData()

valueBytes(dataType: Qgis.DataType, value: float) QByteArray

Gets byte array representing a value.

Parameters:
Return type:

QByteArray

Returns:

byte array representing the value

width(self) int

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

See also

height()

New in version 2.10.

Return type:

int