Class: QgsRasterFileWriter

class qgis.core.QgsRasterFileWriter

Bases: sip.wrapper

The raster file writer which allows you to save a raster to a new file.

The writer defaults to creating GeoTIFF outputs using GDAL. Alternative formats and data providers can be used by calling setOutputFormat() and setOutputProviderKey().

QgsRasterFileWriter(outputUrl: Optional[str]) Constructor for QgsRasterFileWriter, writing to the specified output URL/filename.

QgsRasterFileWriter(a0: QgsRasterFileWriter)

Enums

Mode

alias of RasterExportType

WriterError

alias of RasterFileWriterResult

Methods

buildPyramidsFlag

Returns the pyramid building option.

createMultiBandRaster

Create a raster file with given number of bands without initializing the pixel data.

createOneBandRaster

Create a raster file with one band without initializing the pixel data.

createOptions

Returns the list of data source creation options which will be used when creating the output raster file.

driverForExtension

Returns the GDAL driver name for a specified file extension.

extensionsForFormat

Returns a list of known file extensions for the given GDAL driver format.

filterForDriver

Creates a filter for an GDAL driver key

maxTileHeight

Returns the maximum tile height (in pixels) for tiled outputs.

maxTileWidth

Returns the maximum tile width (in pixels) for tiled outputs.

outputFormat

Returns the output format.

outputProviderKey

Returns the name of the data provider for the raster output.

outputUrl

Returns the output URL (filename) for the raster.

pyramidsConfigOptions

Returns the list of configuration options used when creating the pyramids for the output raster file.

pyramidsFormat

Returns the raster pyramid format.

pyramidsList

Returns the list of pyramids which will be created for the output file.

pyramidsResampling

rtype:

str

setBuildPyramidsFlag

Sets the pyramid building option.

setCreateOptions

Sets a list of data source creation options to use when creating the output raster file.

setMaxTileHeight

Sets the maximum tile height (in pixels) for tiled outputs.

setMaxTileWidth

Sets the maximum tile width (in pixels) for tiled outputs.

setOutputFormat

Sets the output format.

setOutputProviderKey

Sets the name of the data provider for the raster output.

setPyramidsConfigOptions

Sets a list of configuration options to use when creating the pyramids for the output raster file.

setPyramidsFormat

Sets the raster pyramid format.

setPyramidsList

Sets the list of pyramids which will be created for the output file.

setPyramidsResampling

param str:

setTiledMode

Sets whether the output should be tiled.

supportedFiltersAndFormats

Returns a list or pairs, with format filter string as first element and GDAL format key as second element.

supportedFormatExtensions

Returns a list of file extensions for supported formats.

tiledMode

Returns whether the output will be tiled.

writeRaster

Write raster file

Attributes

SortRecommended

class FilterFormatDetails
class FilterFormatDetails(a0: QgsRasterFileWriter.FilterFormatDetails)

Bases: sip.wrapper

driverName
filterString
Mode

alias of RasterExportType

class RasterFormatOption

Bases: int

class RasterFormatOptions
class RasterFormatOptions(f: QgsRasterFileWriter.RasterFormatOptions | QgsRasterFileWriter.RasterFormatOption)
class RasterFormatOptions(a0: QgsRasterFileWriter.RasterFormatOptions)

Bases: sip.wrapper

SortRecommended = 2
WriterError

alias of RasterFileWriterResult

buildPyramidsFlag(self) Qgis.RasterBuildPyramidOption

Returns the pyramid building option.

Return type:

Qgis.RasterBuildPyramidOption

createMultiBandRaster(self, dataType: Qgis.DataType, width: int, height: int, extent: QgsRectangle, crs: QgsCoordinateReferenceSystem, nBands: int) QgsRasterDataProvider | None

Create a raster file with given number of bands without initializing the pixel data. Returned provider may be used to initialize the raster using writeBlock() calls. Ownership of the returned provider is passed to the caller.

Return type:

Optional[QgsRasterDataProvider]

Returns:

Instance of data provider in editing mode (on success) or None on error.

Note

Does not work with tiled mode enabled.

Parameters:
createOneBandRaster(self, dataType: Qgis.DataType, width: int, height: int, extent: QgsRectangle, crs: QgsCoordinateReferenceSystem) QgsRasterDataProvider | None

Create a raster file with one band without initializing the pixel data. Returned provider may be used to initialize the raster using writeBlock() calls. Ownership of the returned provider is passed to the caller.

Return type:

Optional[QgsRasterDataProvider]

Returns:

Instance of data provider in editing mode (on success) or None on error.

Note

Does not work with tiled mode enabled.

Parameters:
createOptions(self) List[str]

Returns the list of data source creation options which will be used when creating the output raster file.

Return type:

List[str]

driverForExtension(extension: str | None) str

Returns the GDAL driver name for a specified file extension. E.g. the driver name for the “.tif” extension is “GTiff”. If no suitable drivers are found then an empty string is returned.

Note that this method works for all GDAL drivers, including those without create support (and which are not supported by QgsRasterFileWriter).

Parameters:

extension (Optional[str])

Return type:

str

extensionsForFormat(format: str | None) List[str]

Returns a list of known file extensions for the given GDAL driver format. E.g. returns “tif”, “tiff” for the format “GTiff”.

If no matching format driver is found an empty list will be returned.

Note that this method works for all GDAL drivers, including those without create support (and which are not supported by QgsRasterFileWriter).

Parameters:

format (Optional[str])

Return type:

List[str]

filterForDriver(driverName: str | None) str

Creates a filter for an GDAL driver key

Parameters:

driverName (Optional[str])

Return type:

str

maxTileHeight(self) int

Returns the maximum tile height (in pixels) for tiled outputs.

See also

maxTileWidth()

See also

tiledMode()

Return type:

int

maxTileWidth(self) int

Returns the maximum tile width (in pixels) for tiled outputs.

See also

maxTileHeight()

See also

tiledMode()

Return type:

int

outputFormat(self) str

Returns the output format.

For GDAL disk based outputs this will match the GDAL driver name, e.g. “GTiff” for GeoTiff exports.

Return type:

str

outputProviderKey(self) str

Returns the name of the data provider for the raster output.

Return type:

str

outputUrl(self) str

Returns the output URL (filename) for the raster.

Return type:

str

pyramidsConfigOptions(self) List[str]

Returns the list of configuration options used when creating the pyramids for the output raster file.

Return type:

List[str]

pyramidsFormat(self) Qgis.RasterPyramidFormat

Returns the raster pyramid format.

Return type:

Qgis.RasterPyramidFormat

pyramidsList(self) List[int]

Returns the list of pyramids which will be created for the output file.

Return type:

List[int]

pyramidsResampling(self) str
Return type:

str

setBuildPyramidsFlag(self, f: Qgis.RasterBuildPyramidOption)

Sets the pyramid building option.

Parameters:

f (Qgis.RasterBuildPyramidOption)

setCreateOptions(self, list: Iterable[str | None])

Sets a list of data source creation options to use when creating the output raster file.

See also

createOptions()

Parameters:

list (Iterable[Optional[str]])

setMaxTileHeight(self, h: int)

Sets the maximum tile height (in pixels) for tiled outputs.

See also

maxTileHeight()

See also

tiledMode()

Parameters:

h (int)

setMaxTileWidth(self, w: int)

Sets the maximum tile width (in pixels) for tiled outputs.

See also

maxTileWidth()

See also

tiledMode()

Parameters:

w (int)

setOutputFormat(self, format: str | None)

Sets the output format.

For GDAL disk based outputs this should match the GDAL driver name, e.g. “GTiff” for GeoTiff exports.

See also

outputFormat()

Parameters:

format (Optional[str])

setOutputProviderKey(self, key: str | None)

Sets the name of the data provider for the raster output.

E.g. set to “gdal” to use GDAL to create disk based raster files.

Parameters:

key (Optional[str])

setPyramidsConfigOptions(self, list: Iterable[str | None])

Sets a list of configuration options to use when creating the pyramids for the output raster file.

Parameters:

list (Iterable[Optional[str]])

setPyramidsFormat(self, f: Qgis.RasterPyramidFormat)

Sets the raster pyramid format.

See also

pyramidsFormat()

Parameters:

f (Qgis.RasterPyramidFormat)

setPyramidsList(self, list: Iterable[int])

Sets the list of pyramids which will be created for the output file.

See also

pyramidsList()

Parameters:

list (Iterable[int])

setPyramidsResampling(self, str: str | None)
Parameters:

str (Optional[str])

setTiledMode(self, t: bool)

Sets whether the output should be tiled.

Tiled outputs will automatically split the raster into multiple parts, based on the maxTileWidth() value.

See also

tiledMode()

Parameters:

t (bool)

supportedFiltersAndFormats(options: QgsRasterFileWriter.RasterFormatOptions | QgsRasterFileWriter.RasterFormatOption = QgsRasterFileWriter.SortRecommended) List[QgsRasterFileWriter.FilterFormatDetails]

Returns a list or pairs, with format filter string as first element and GDAL format key as second element. Relies on GDAL_DMD_EXTENSIONS metadata, if it is empty corresponding driver will be skipped even if supported.

The options argument can be used to control the sorting and filtering of returned formats.

Parameters:

options (Union[QgsRasterFileWriter.RasterFormatOptions)

Return type:

List[QgsRasterFileWriter.FilterFormatDetails]

supportedFormatExtensions(options: QgsRasterFileWriter.RasterFormatOptions | QgsRasterFileWriter.RasterFormatOption = QgsRasterFileWriter.SortRecommended) List[str]

Returns a list of file extensions for supported formats.

The options argument can be used to control the sorting and filtering of returned formats.

Parameters:

options (Union[QgsRasterFileWriter.RasterFormatOptions)

Return type:

List[str]

tiledMode(self) bool

Returns whether the output will be tiled.

See also

setTiledMode()

Return type:

bool

writeRaster(self, pipe: QgsRasterPipe | None, nCols: int, nRows: int, outputExtent: QgsRectangle, crs: QgsCoordinateReferenceSystem, feedback: QgsRasterBlockFeedback | None = None) Qgis.RasterFileWriterResult

Write raster file

Parameters:
  • pipe (Optional[QgsRasterPipe]) – raster pipe

  • nCols (int) – number of output columns

  • nRows (int) – number of output rows (or -1 to automatically calculate row number to have square pixels)

  • outputExtent (QgsRectangle) – extent to output

  • crs (QgsCoordinateReferenceSystem) – crs to reproject to

  • feedback (Optional[QgsRasterBlockFeedback] = None) – optional feedback object for progress reports

Deprecated since version QGIS: 3.8 use version with transformContext instead

writeRaster(self, pipe: Optional[QgsRasterPipe], nCols: int, nRows: int, outputExtent: QgsRectangle, crs: QgsCoordinateReferenceSystem, transformContext: QgsCoordinateTransformContext, feedback: Optional[QgsRasterBlockFeedback] = None) -> Qgis.RasterFileWriterResult Write raster file

Parameters:
  • pipe – raster pipe

  • nCols – number of output columns

  • nRows – number of output rows (or -1 to automatically calculate row number to have square pixels)

  • outputExtent – extent to output

  • crs – crs to reproject to

  • transformContext – coordinate transform context

  • feedback – optional feedback object for progress reports

Added in version 3.8.

Return type:

Qgis.RasterFileWriterResult