QgsRasterBlock class

Raster data container.

Public static functions

static auto printValue(double value) -> QString
Print double value with all necessary significant digits.
static auto printValue(float value) -> QString
Print float value with all necessary significant digits.
static auto readValue(void* data, Qgis::DataType type, qgssize index) -> double
static auto subRect(const QgsRectangle& extent, int width, int height, const QgsRectangle& subExtent) -> QRect
For extent and width, height find rectangle covered by subextent.
static auto typeIsColor(Qgis::DataType type) -> bool
Returns true if data type is color.
static auto typeIsNumeric(Qgis::DataType type) -> bool
Returns true if data type is numeric.
static auto typeWithNoDataValue(Qgis::DataType dataType, double* noDataValue) -> Qgis::DataType
For given data type returns wider type and sets no data value.
static auto valueBytes(Qgis::DataType dataType, double value) -> QByteArray
Gets byte array representing a value.
static void writeValue(void* data, Qgis::DataType type, qgssize index, double value)

Constructors, destructors, conversion operators

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

Public functions

void applyScaleOffset(double scale, double offset)
Apply band scale and offset to raster block values.
auto bits(int row, int column) -> char*
Returns a pointer to block data.
auto bits(qgssize index) -> char*
Returns a pointer to block data.
auto bits() -> char*
Returns a pointer to block data.
auto byteData() const -> const quint8*
Gives direct access to the raster block data.
auto color(int row, int column) const -> QRgb
Read a single color.
auto color(qgssize index) const -> QRgb
Read a single value.
auto colorData() -> QRgb*
Gives direct read/write access to the raster RGB data.
auto convert(Qgis::DataType destDataType) -> bool
Convert data to different type.
auto data() const -> QByteArray
Gets access to raw data.
auto dataType() const -> Qgis::DataType
Returns data type.
auto error() const -> QgsError
Returns the last error.
auto hasNoData() const -> bool
Returns true if the block may contain no data.
auto hasNoDataValue() const -> bool
True if the block has no data value.
auto height() const -> int
Returns the height (number of rows) of the raster block.
auto image() const -> QImage
Returns an image containing the block data, if the block's data type is color.
auto isEmpty() const -> bool
Returns true if block is empty, i.e.
auto isNoData(int row, int column) const -> bool
Check if value at position is no data.
auto isNoData(qgssize row, qgssize column) const -> bool
Check if value at position is no data.
auto isNoData(qgssize index) const -> bool
Check if value at position is no data.
auto isValid() const -> bool
Returns true if the block is valid (correctly filled with data).
auto noDataValue() const -> double
Returns no data value.
auto reset(Qgis::DataType dataType, int width, int height) -> bool
Reset block.
void resetNoDataValue()
Reset no data value: if there was a no data value previously set, it will be discarded.
auto setColor(int row, int column, QRgb color) -> bool
Set color on position.
auto setColor(qgssize index, QRgb color) -> bool
Set color on index (indexed line by line)
void setData(const QByteArray& data, int offset = 0)
Rewrite raw pixel data.
void setError(const QgsError& error)
Sets the last error.
auto setImage(const QImage* image) -> bool
Sets the block data via an image.
void setIsData(int row, int column)
Remove no data flag on pixel.
void setIsData(qgssize index)
Remove no data flag on pixel.
auto setIsNoData(int row, int column) -> bool
Set no data on pixel.
auto setIsNoData(qgssize index) -> bool
Set no data on pixel.
auto setIsNoData() -> bool
Set the whole block to no data.
auto setIsNoDataExcept(QRect exceptRect) -> bool
Set the whole block to no data except specified rectangle.
void setNoDataValue(double noDataValue)
Sets cell value that will be considered as "no data".
void setValid(bool valid)
Mark block as valid or invalid.
auto setValue(int row, int column, double value) -> bool
Set value on position.
auto setValue(qgssize index, double value) -> bool
Set value on index (indexed line by line)
auto value(int row, int column) const -> double
Read a single value if type of block is numeric.
auto value(qgssize index) const -> double
Read a single value if type of block is numeric.
auto width() const -> int
Returns the width (number of columns) of the raster block.

Function documentation

static QString QgsRasterBlock::printValue(double value)

Print double value with all necessary significant digits.

Parameters
value the value to be printed
Returns string representing the value

It is ensured that conversion back to double gives the same number.

static QString QgsRasterBlock::printValue(float value)

Print float value with all necessary significant digits.

Parameters
value the value to be printed
Returns string representing the value

It is ensured that conversion back to float gives the same number.

static double QgsRasterBlock::readValue(void* data, Qgis::DataType type, qgssize index)

static QRect QgsRasterBlock::subRect(const QgsRectangle& extent, int width, int height, const QgsRectangle& subExtent)

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

Parameters
extent extent, usually the larger
width numbers of columns in theExtent
height numbers of rows in theExtent
subExtent extent, usually smaller than theExtent
Returns the rectangle covered by sub extent

The output rect has x oriented from left to right and y from top to bottom (upper-left to lower-right orientation).

static QByteArray QgsRasterBlock::valueBytes(Qgis::DataType dataType, double value)

Gets byte array representing a value.

Parameters
dataType data type
value value
Returns byte array representing the value

static void QgsRasterBlock::writeValue(void* data, Qgis::DataType type, qgssize index, double value)

QgsRasterBlock::QgsRasterBlock(Qgis::DataType dataType, int width, int height)

Constructor which allocates data block in memory.

Parameters
dataType raster data type
width width of data matrix
height height of data matrix

void QgsRasterBlock::applyScaleOffset(double scale, double offset)

Apply band scale and offset to raster block values.

char* QgsRasterBlock::bits(int row, int column)

Returns a pointer to block data.

Parameters
row row index
column column index

char* QgsRasterBlock::bits(qgssize index)

Returns a pointer to block data.

Parameters
index data matrix index (long type in Python)

char* QgsRasterBlock::bits()

Returns a pointer to block data.

const quint8* QgsRasterBlock::byteData() const

Gives direct access to the raster block data.

The data type of the block must be Qgis::Byte otherwise it returns null pointer. Useful for most efficient read access.

QRgb QgsRasterBlock::color(int row, int column) const

Read a single color.

Parameters
row row index
column column index
Returns color

QRgb QgsRasterBlock::color(qgssize index) const

Read a single value.

Parameters
index data matrix index (long type in Python)
Returns color

QRgb* QgsRasterBlock::colorData()

Gives direct read/write access to the raster RGB data.

The data type of the block must be Qgis::ARGB32 or Qgis::ARGB32_Premultiplied otherwise it returns null pointer. Useful for most efficient read/write access to RGB blocks.

bool QgsRasterBlock::convert(Qgis::DataType destDataType)

Convert data to different type.

Parameters
destDataType dest data type
Returns true on success

QByteArray QgsRasterBlock::data() const

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.

bool QgsRasterBlock::hasNoData() const

Returns true if the block may contain no data.

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

bool QgsRasterBlock::hasNoDataValue() const

True if the block has no data value.

Returns true if the block has no data value

int QgsRasterBlock::height() const

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

bool QgsRasterBlock::isEmpty() const

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

bool QgsRasterBlock::isNoData(int row, int column) const

Check if value at position is no data.

Parameters
row row index
column column index
Returns true if value is no data

bool QgsRasterBlock::isNoData(qgssize row, qgssize column) const

Check if value at position is no data.

Parameters
row row index
column column index
Returns true if value is no data

bool QgsRasterBlock::isNoData(qgssize index) const

Check if value at position is no data.

Parameters
index data matrix index (long type in Python)
Returns true if value is no data

bool QgsRasterBlock::isValid() const

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.

double QgsRasterBlock::noDataValue() const

Returns no data value.

Returns No data value

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

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

Reset block.

Parameters
dataType raster data type
width width of data matrix
height height of data matrix
Returns true on success

void QgsRasterBlock::resetNoDataValue()

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

bool QgsRasterBlock::setColor(int row, int column, QRgb color)

Set color on position.

Parameters
row row index
column column index
color the color to be set, QRgb value
Returns true on success

bool QgsRasterBlock::setColor(qgssize index, QRgb color)

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
Returns true on success

void QgsRasterBlock::setData(const QByteArray& data, int offset = 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.

bool QgsRasterBlock::setImage(const QImage* image)

Sets the block data via an image.

Returns true on success

void QgsRasterBlock::setIsData(int row, int column)

Remove no data flag on pixel.

Parameters
row row index
column column index

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.

void QgsRasterBlock::setIsData(qgssize index)

Remove no data flag on pixel.

Parameters
index data matrix index (long type in Python)

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.

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

Set no data on pixel.

Parameters
row row index
column column index
Returns true on success

bool QgsRasterBlock::setIsNoData(qgssize index)

Set no data on pixel.

Parameters
index data matrix index (long type in Python)
Returns true on success

bool QgsRasterBlock::setIsNoData()

Set the whole block to no data.

Returns true on success

bool QgsRasterBlock::setIsNoDataExcept(QRect exceptRect)

Set the whole block to no data except specified rectangle.

Returns true on success

void QgsRasterBlock::setNoDataValue(double noDataValue)

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

bool QgsRasterBlock::setValue(int row, int column, double value)

Set value on position.

Parameters
row row index
column column index
value the value to be set
Returns true on success

bool QgsRasterBlock::setValue(qgssize index, double value)

Set value on index (indexed line by line)

Parameters
index data matrix index (long type in Python)
value the value to be set
Returns true on success

double QgsRasterBlock::value(int row, int column) const

Read a single value if type of block is numeric.

Parameters
row row index
column column index
Returns value

If type is color, returned value is undefined.

double QgsRasterBlock::value(qgssize index) const

Read a single value if type of block is numeric.

Parameters
index data matrix index (long type in Python)
Returns value

If type is color, returned value is undefined.

int QgsRasterBlock::width() const

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