Class: QgsRasterBlock¶
Raster data container.
Methods
Apply band scale and offset to raster block values |
|
Returns the block data as a numpy array. |
|
Read a single color |
|
Convert data to different type. |
|
Gets access to raw data. |
|
Returns data type |
|
Data type size in bytes. |
|
Returns the last error |
|
Fills the whole block with a constant value. |
|
Returns |
|
|
|
Returns the height (number of rows) of the raster block. |
|
Returns an image containing the block data, if the block's data type is color. |
|
Returns |
|
Checks if value at position is no data |
|
Returns |
|
Returns the maximum value present in the raster block. |
|
Returns the minimum value present in the raster block. |
|
Returns the minimum and maximum value present in the raster block. |
|
Returns no data value. |
|
Reset block |
|
Reset no data value: if there was a no data value previously set, it will be discarded. |
|
Set color on position |
|
Rewrite raw pixel data. |
|
Sets the last error |
|
Sets the block data via an image. |
|
Remove no data flag on pixel. |
|
Set no data on pixel |
|
Set the whole block to no data except specified rectangle |
|
Sets cell value that will be considered as "no data". |
|
Mark block as valid or invalid |
|
Set value on position |
|
Read a single value if type of block is numeric. |
|
Returns the width (number of columns) of the raster block. |
Static Methods
Print double value with all necessary significant digits. |
|
For extent and width, height find rectangle covered by subextent. |
|
Returns |
|
Returns |
|
Returns |
|
Returns the size in bytes for the specified dataType. |
|
For given data type returns wider type and sets no data value |
|
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 theQgsRasterBlock
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:
- 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 andhasNoDataValue()
.- 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
See also
See also
- height(self) int [source]¶
Returns the height (number of rows) of the raster block.
See also
- 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 returnTrue
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 byerror()
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, orFalse
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
See also
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, orFalse
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
See also
Added in version 3.42.
- minimumMaximum(self)[source]¶
Returns the minimum and maximum value present in the raster block.
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, orFalse
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
See also
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
See also
See also
- 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
See also
See also
- 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)
- 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
See also
See also
- Parameters:
noDataValue (float)
- 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
- static typeIsColor(type: Qgis.DataType) bool [source]¶
Returns
True
if a data type is a color type.See also
See also
- 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
See also
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
See also
- 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:
dataType (Qgis.DataType) – data type
value (float) – value
- Return type:
QByteArray
- Returns:
byte array representing the value