Class: QgsAlignRaster

Takes one or more raster layers and warps (resamples) them to a common grid.

The aligned rasters will have the same:

  • coordinate reference system

  • cell size and raster size

  • offset of the raster grid

Note

This is an abstract class, with methods which must be implemented by a subclass.

The following methods must be implemented: progress()

Enums

ResampleAlg

alias of GdalResampleAlgorithm

Methods

alignedRasterExtent

Returns the expected extent of the resulting aligned raster

alignedRasterSize

Returns the expected size of the resulting aligned raster

cellSize

Gets output cell size

checkInputParameters

Determine destination extent from the input rasters and calculate derived values

clipExtent

Gets clipping extent (region of interest).

createAndWarp

Internal function for processing of one raster (1.

destinationCrs

Gets the output CRS in WKT format

dump

write contents of the object to standard error stream - for debugging

errorMessage

Returns the error from a previous run() call.

gridOffset

progressHandler

Gets associated progress handler.

rasters

Gets list of rasters that will be aligned

run

Run the alignment process

setCellSize

Sets output cell size

setClipExtent

Configure clipping extent (region of interest).

setDestinationCrs

Sets the output CRS in WKT format

setGridOffset

setParametersFromRaster

Set destination CRS, cell size and grid offset from a raster file.

setProgressHandler

Assign a progress handler instance.

setRasters

Sets list of rasters that will be aligned

suggestedReferenceLayer

Returns the index of the layer which has smallest cell size (returns -1 on error)

Static Methods

suggestedWarpOutput

Determine suggested output of raster warp to a different CRS.

class qgis.analysis.QgsAlignRaster[source]

Bases: object

Item

alias of RasterItem

class ProgressHandler

Bases: object

Helper struct to be sub-classed for progress reporting

progress(self, complete: float) bool

Method to be overridden for progress reporting.

Parameters:

complete (float) – Overall progress of the alignment operation

Return type:

bool

Returns:

False if the execution should be canceled, True otherwise

class RasterInfo

Bases: object

Utility class for gathering information about rasters

bandCount(self) int

Returns the number of raster bands in the file

Return type:

int

cellSize(self) QSizeF

Returns the cell size in map units

Return type:

QSizeF

crs(self) str

Returns the CRS in WKT format

Return type:

str

dump(self)

Write contents of the object to standard error stream - for debugging

extent(self) QgsRectangle

Returns the extent of the raster

Return type:

QgsRectangle

gridOffset(self) QPointF

Returns the grid offset

Return type:

QPointF

identify(self, mx: float, my: float) float

Gets raster value at the given coordinates (from the first band)

Parameters:
  • mx (float)

  • my (float)

Return type:

float

isValid(self) bool

Check whether the given path is a valid raster

Return type:

bool

origin(self) QPointF

Returns the origin of the raster

Return type:

QPointF

rasterSize(self) QSize

Returns the size of the raster grid in pixels

Return type:

QSize

ResampleAlg

alias of GdalResampleAlgorithm

alignedRasterExtent(self) QgsRectangle[source]

Returns the expected extent of the resulting aligned raster

Note

first need to run checkInputParameters() which returns with success

Return type:

QgsRectangle

alignedRasterSize(self) QSize[source]

Returns the expected size of the resulting aligned raster

Note

first need to run checkInputParameters() which returns with success

Return type:

QSize

cellSize(self) QSizeF[source]

Gets output cell size

Return type:

QSizeF

checkInputParameters(self) bool[source]

Determine destination extent from the input rasters and calculate derived values

Return type:

bool

Returns:

True on success, sets error on error (see errorMessage())

clipExtent(self) QgsRectangle[source]

Gets clipping extent (region of interest). No extra clipping is done if the rectangle is null

Return type:

QgsRectangle

createAndWarp(self, raster: QgsAlignRasterData.RasterItem) bool[source]

Internal function for processing of one raster (1. create output, 2. do the alignment)

Parameters:

raster (QgsAlignRasterData.RasterItem)

Return type:

bool

destinationCrs(self) str[source]

Gets the output CRS in WKT format

Return type:

str

dump(self)[source]

write contents of the object to standard error stream - for debugging

errorMessage(self) str[source]

Returns the error from a previous run() call. Error message is empty if run() succeeded (returned True)

Return type:

str

gridOffset(self) QPointF[source]
Return type:

QPointF

progressHandler(self) QgsAlignRaster.ProgressHandler | None[source]

Gets associated progress handler. May be None (default)

Return type:

Optional[QgsAlignRaster.ProgressHandler]

rasters(self) List[QgsAlignRasterData.RasterItem][source]

Gets list of rasters that will be aligned

Return type:

List[QgsAlignRasterData.RasterItem]

run(self) bool[source]

Run the alignment process

Return type:

bool

Returns:

True on success, sets error on error (see errorMessage())

setCellSize(self, x: float, y: float)[source]

Sets output cell size

Parameters:
  • x (float)

  • y (float)

setCellSize(self, size: QSizeF)[source]

Sets output cell size

Parameters:

size (QSizeF)

setClipExtent(self, xmin: float, ymin: float, xmax: float, ymax: float)[source]

Configure clipping extent (region of interest). No extra clipping is done if the rectangle is null

Parameters:
  • xmin (float)

  • ymin (float)

  • xmax (float)

  • ymax (float)

setClipExtent(self, extent: QgsRectangle)[source]

Configure clipping extent (region of interest). No extra clipping is done if the rectangle is null

Parameters:

extent (QgsRectangle)

setDestinationCrs(self, crsWkt: str | None)[source]

Sets the output CRS in WKT format

Parameters:

crsWkt (Optional[str])

setGridOffset(self, offset: QPointF | QPoint)[source]
Parameters:

offset (Union[QPointF, QPoint])

setParametersFromRaster(self, rasterInfo: QgsAlignRaster.RasterInfo, customCRSWkt: str | None = '', customCellSize: QSizeF = QSizeF(), customGridOffset: QPointF | QPoint = QPointF(-1, -1)) bool[source]

Set destination CRS, cell size and grid offset from a raster file. The user may provide custom values for some of the parameters - in such case only the remaining parameters are calculated.

If default CRS is used, the parameters are set according to the raster file’s geo-transform. If a custom CRS is provided, suggested reprojection is calculated first (using GDAL) in order to determine suitable defaults for cell size and grid offset.

Return type:

bool

Returns:

True on success (may fail if it is not possible to reproject raster to given CRS)

Parameters:
  • rasterInfo (QgsAlignRaster.RasterInfo)

  • customCRSWkt (Optional[str] = '')

  • customCellSize (QSizeF = QSizeF())

  • customGridOffset (Union[QPointF, QPoint] = QPointF(-1, -1))

setParametersFromRaster(self, filename: str | None, customCRSWkt: str | None = '', customCellSize: QSizeF = QSizeF(), customGridOffset: QPointF | QPoint = QPointF(-1, -1)) bool[source]

Overridden variant for convenience, taking filename instead RasterInfo object. See the other variant for details.

Parameters:
  • filename (Optional[str])

  • customCRSWkt (Optional[str] = '')

  • customCellSize (QSizeF = QSizeF())

  • customGridOffset (Union[QPointF, QPoint] = QPointF(-1, -1))

Return type:

bool

setProgressHandler(self, progressHandler: QgsAlignRaster.ProgressHandler | None)[source]

Assign a progress handler instance. Does not take ownership. None can be passed.

Parameters:

progressHandler (Optional[QgsAlignRaster.ProgressHandler])

setRasters(self, list: Iterable[QgsAlignRasterData.RasterItem])[source]

Sets list of rasters that will be aligned

Parameters:

list (Iterable[QgsAlignRasterData.RasterItem])

suggestedReferenceLayer(self) int[source]

Returns the index of the layer which has smallest cell size (returns -1 on error)

Return type:

int

static suggestedWarpOutput(info: QgsAlignRaster.RasterInfo, destWkt: str | None, cellSize: QSizeF | None = None, gridOffset: QPointF | QPoint | None = None, rect: QgsRectangle | None = None) bool[source]

Determine suggested output of raster warp to a different CRS. Returns True on success

Parameters:
Return type:

bool