QgsAlignRaster class

QgsAlignRaster takes one or more raster layers and warps (resamples) them so they have the same:

  • coordinate reference system
  • cell size and raster size
  • offset of the raster grid

Public types

struct Item
Definition of one raster layer for alignment.
struct ProgressHandler
Helper struct to be sub-classed for progress reporting.
struct RasterInfo
Utility class for gathering information about rasters.
enum ResampleAlg { RA_NearestNeighbour = 0, RA_Bilinear = 1, RA_Cubic = 2, RA_CubicSpline = 3, RA_Lanczos = 4, RA_Average = 5, RA_Mode = 6, RA_Max = 8, RA_Min = 9, RA_Median = 10, RA_Q1 = 11, RA_Q3 = 12 }
Resampling algorithm to be used (equivalent to GDAL's enum GDALResampleAlg)

Public functions

auto alignedRasterExtent() const -> QgsRectangle
Returns the expected extent of the resulting aligned raster.
auto alignedRasterSize() const -> QSize
Returns the expected size of the resulting aligned raster.
auto cellSize() const -> QSizeF
Gets output cell size.
auto checkInputParameters() -> bool
Determine destination extent from the input rasters and calculate derived values.
auto clipExtent() const -> QgsRectangle
Gets clipping extent (region of interest).
auto destinationCrs() const -> QString
Gets the output CRS in WKT format.
void dump() const
write contents of the object to standard error stream - for debugging
auto errorMessage() const -> QString
Returns the error from a previous run() call.
auto progressHandler() const -> ProgressHandler*
Gets associated progress handler. May be nullptr (default)
auto rasters() const -> List
Gets list of rasters that will be aligned.
auto run() -> bool
Run the alignment process.
void setCellSize(double x, double y)
Sets output cell size.
void setCellSize(QSizeF size)
Sets output cell size.
void setClipExtent(double xmin, double ymin, double xmax, double ymax)
Configure clipping extent (region of interest).
void setClipExtent(const QgsRectangle& extent)
Configure clipping extent (region of interest).
void setDestinationCrs(const QString& crsWkt)
Sets the output CRS in WKT format.
auto setParametersFromRaster(const RasterInfo& rasterInfo, const QString& customCRSWkt = QString(), QSizeF customCellSize = QSizeF(), QPointF customGridOffset = QPointF(-1, -1)) -> bool
Set destination CRS, cell size and grid offset from a raster file.
auto setParametersFromRaster(const QString& filename, const QString& customCRSWkt = QString(), QSizeF customCellSize = QSizeF(), QPointF customGridOffset = QPointF(-1, -1)) -> bool
Overridden variant for convenience, taking filename instead RasterInfo object.
void setProgressHandler(ProgressHandler* progressHandler)
Assign a progress handler instance. Does not take ownership. nullptr can be passed.
void setRasters(const List& list)
Sets list of rasters that will be aligned.
auto suggestedReferenceLayer() const -> int
Returns the index of the layer which has smallest cell size (returns -1 on error)

Protected static functions

static auto suggestedWarpOutput(const RasterInfo& info, const QString& destWkt, QSizeF* cellSize = nullptr, QPointF* gridOffset = nullptr, QgsRectangle* rect = nullptr) -> bool
Determine suggested output of raster warp to a different CRS. Returns true on success.

Protected functions

auto createAndWarp(const Item& raster) -> bool
Internal function for processing of one raster (1. create output, 2. do the alignment)

Protected variables

double mCellSizeX
Destination cell size.
double mClipExtent
Optional clip extent: sets "requested area" which be extended to fit the raster grid.
QString mCrsWkt
Destination CRS - stored in well-known text (WKT) format.
QString mErrorMessage
Last error message from run()
double mGeoTransform
Computed geo-transform.
double mGridOffsetX
Destination grid offset - expected to be in interval <0,cellsize)
ProgressHandler* mProgressHandler
Object that facilitates reporting of progress / cancelation.
List mRasters
List of rasters to be aligned (with their output files and other options)
int mXSize
Computed raster grid width.
int mYSize
Computed raster grid height.

Enum documentation

enum QgsAlignRaster::ResampleAlg

Resampling algorithm to be used (equivalent to GDAL's enum GDALResampleAlg)

Enumerators
RA_NearestNeighbour

Nearest neighbour (select on one input pixel)

RA_Bilinear

Bilinear (2x2 kernel)

RA_Cubic

Cubic Convolution Approximation (4x4 kernel)

RA_CubicSpline

Cubic B-Spline Approximation (4x4 kernel)

RA_Lanczos

Lanczos windowed sinc interpolation (6x6 kernel)

RA_Average

Average (computes the average of all non-NODATA contributing pixels)

RA_Mode

Mode (selects the value which appears most often of all the sampled points)

RA_Max

Maximum (selects the maximum of all non-NODATA contributing pixels)

RA_Min

Minimum (selects the minimum of all non-NODATA contributing pixels)

RA_Median

Median (selects the median of all non-NODATA contributing pixels)

RA_Q1

First quartile (selects the first quartile of all non-NODATA contributing pixels)

RA_Q3

Third quartile (selects the third quartile of all non-NODATA contributing pixels)

Function documentation

QgsRectangle QgsAlignRaster::alignedRasterExtent() const

Returns the expected extent of the resulting aligned raster.

QSize QgsAlignRaster::alignedRasterSize() const

Returns the expected size of the resulting aligned raster.

bool QgsAlignRaster::checkInputParameters()

Determine destination extent from the input rasters and calculate derived values.

Returns true on success, sets error on error (see errorMessage())

QgsRectangle QgsAlignRaster::clipExtent() const

Gets clipping extent (region of interest).

No extra clipping is done if the rectangle is null

QString QgsAlignRaster::errorMessage() const

Returns the error from a previous run() call.

Error message is empty if run() succeeded (returned true)

bool QgsAlignRaster::run()

Run the alignment process.

Returns true on success, sets error on error (see errorMessage())

void QgsAlignRaster::setClipExtent(double xmin, double ymin, double xmax, double ymax)

Configure clipping extent (region of interest).

No extra clipping is done if the rectangle is null

void QgsAlignRaster::setClipExtent(const QgsRectangle& extent)

Configure clipping extent (region of interest).

No extra clipping is done if the rectangle is null

bool QgsAlignRaster::setParametersFromRaster(const RasterInfo& rasterInfo, const QString& customCRSWkt = QString(), QSizeF customCellSize = QSizeF(), QPointF customGridOffset = QPointF(-1, -1))

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

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

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.

bool QgsAlignRaster::setParametersFromRaster(const QString& filename, const QString& customCRSWkt = QString(), QSizeF customCellSize = QSizeF(), QPointF customGridOffset = QPointF(-1, -1))

Overridden variant for convenience, taking filename instead RasterInfo object.

See the other variant for details.

Variable documentation

double QgsAlignRaster::mClipExtent protected

Optional clip extent: sets "requested area" which be extended to fit the raster grid.

Clipping not done if all coords are zeroes.