add create options support to QgsGridFileWirter (follow-up 2aa6c5d)

This commit is contained in:
Alexander Bruy 2025-03-13 13:14:37 +00:00 committed by Nyall Dawson
parent 33c34c581a
commit a54654da92
5 changed files with 90 additions and 0 deletions

View File

@ -37,6 +37,24 @@ Writes the grid file.
An optional ``feedback`` object can be set for progress reports and cancellation support
:return: 0 in case of success
%End
void setCreateOptions( const QStringList &list );
%Docstring
Sets a list of data source creation options to use when creating the output raster file.
.. seealso:: :py:func:`createOptions`
.. versionadded:: 3.44
%End
QStringList createOptions() const;
%Docstring
Returns the list of data source creation options which will be used when creating the output raster file.
.. seealso:: :py:func:`setCreateOptions`
.. versionadded:: 3.44
%End
};

View File

@ -37,6 +37,24 @@ Writes the grid file.
An optional ``feedback`` object can be set for progress reports and cancellation support
:return: 0 in case of success
%End
void setCreateOptions( const QStringList &list );
%Docstring
Sets a list of data source creation options to use when creating the output raster file.
.. seealso:: :py:func:`createOptions`
.. versionadded:: 3.44
%End
QStringList createOptions() const;
%Docstring
Returns the list of data source creation options which will be used when creating the output raster file.
.. seealso:: :py:func:`setCreateOptions`
.. versionadded:: 3.44
%End
};

View File

@ -45,6 +45,7 @@ int QgsGridFileWriter::writeFile( QgsFeedback *feedback )
auto writer = std::make_unique<QgsRasterFileWriter>( mOutputFilePath );
writer->setOutputProviderKey( QStringLiteral( "gdal" ) );
writer->setOutputFormat( outputFormat );
writer->setCreateOptions( mCreateOptions );
std::unique_ptr<QgsRasterDataProvider> provider( writer->createOneBandRaster( Qgis::DataType::Float32, mNumColumns, mNumRows, mInterpolationExtent, crs ) );
if ( !provider )

View File

@ -53,6 +53,22 @@ class ANALYSIS_EXPORT QgsGridFileWriter
*/
int writeFile( QgsFeedback *feedback = nullptr );
/**
* Sets a list of data source creation options to use when creating the output raster file.
*
* \see createOptions()
* \since QGIS 3.44t
*/
void setCreateOptions( const QStringList &list ) { mCreateOptions = list; }
/**
* Returns the list of data source creation options which will be used when creating the output raster file.
*
* \see setCreateOptions()
* \since QGIS 3.44
*/
QStringList createOptions() const { return mCreateOptions; }
private:
QgsGridFileWriter() = delete;
@ -64,6 +80,8 @@ class ANALYSIS_EXPORT QgsGridFileWriter
double mCellSizeX = 0;
double mCellSizeY = 0;
QStringList mCreateOptions;
};
#endif

View File

@ -79,6 +79,23 @@ class TestInterpolation(QgisTestCase):
writer = QgsGridFileWriter(interpolator, output_file, extent, cols, rows)
writer.writeFile()
checker = QgsRasterChecker()
ok = checker.runTest(
"gdal",
output_file,
"gdal",
os.path.join(TEST_DATA_DIR, "analysis", "idw_interpolation.tif"),
)
self.report += checker.report()
self.assertTrue(ok)
tfw_file = os.path.join(tempfile.gettempdir(), "idw_interpolation.tfw")
self.assertFalse(os.path.exists(tfw_file))
# with raster creation options
writer.setCreateOptions(["TFW=yes"])
writer.writeFile()
checker = QgsRasterChecker()
ok = checker.runTest(
"gdal",
@ -93,6 +110,7 @@ class TestInterpolation(QgisTestCase):
f.write(self.report)
self.assertTrue(ok)
self.assertTrue(os.path.exists(tfw_file))
def test_tin_interpolator(self):
layer = QgsVectorLayer(
@ -124,6 +142,22 @@ class TestInterpolation(QgisTestCase):
writer.writeFile()
checker = QgsRasterChecker()
ok = checker.runTest(
"gdal",
output_file,
"gdal",
os.path.join(TEST_DATA_DIR, "analysis", "tin_interpolation.tif"),
)
self.report += checker.report()
self.assertTrue(ok)
tfw_file = os.path.join(tempfile.gettempdir(), "tin_interpolation.tfw")
self.assertFalse(os.path.exists(tfw_file))
# with raster creation options
writer.setCreateOptions(["TFW=yes"])
writer.writeFile()
ok = checker.runTest(
"gdal",
output_file,
@ -137,6 +171,7 @@ class TestInterpolation(QgisTestCase):
f.write(self.report)
self.assertTrue(ok)
self.assertTrue(os.path.exists(tfw_file))
if __name__ == "__main__":