Skip to content

Commit

Permalink
add creation options support to some native raster Processing algorithms
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbruy committed Jun 26, 2024
1 parent 1f1c5c9 commit 556c73f
Show file tree
Hide file tree
Showing 14 changed files with 155 additions and 20 deletions.
10 changes: 10 additions & 0 deletions src/analysis/processing/qgsalgorithmcellstatistics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ void QgsCellStatisticsAlgorithmBase::initAlgorithm( const QVariantMap & )
output_nodata_parameter->setFlags( output_nodata_parameter->flags() | Qgis::ProcessingParameterFlag::Advanced );
addParameter( output_nodata_parameter.release() );

std::unique_ptr< QgsProcessingParameterString > createOptsParam = std::make_unique< QgsProcessingParameterString >( QStringLiteral( "CREATE_OPTIONS" ), QObject::tr( "Creation options" ), QVariant(), false, true );
createOptsParam->setMetadata( QVariantMap( {{QStringLiteral( "widget_wrapper" ), QVariantMap( {{QStringLiteral( "widget_type" ), QStringLiteral( "rasteroptions" ) }} ) }} ) );
createOptsParam->setFlags( createOptsParam->flags() | Qgis::ProcessingParameterFlag::Advanced );
addParameter( createOptsParam.release() );

addParameter( new QgsProcessingParameterRasterDestination( QStringLiteral( "OUTPUT" ),
QObject::tr( "Output layer" ) ) );

Expand Down Expand Up @@ -122,12 +127,17 @@ bool QgsCellStatisticsAlgorithmBase::prepareAlgorithm( const QVariantMap &parame

QVariantMap QgsCellStatisticsAlgorithmBase::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
{
const QString createOptions = parameterAsString( parameters, QStringLiteral( "CREATE_OPTIONS" ), context ).trimmed();
const QString outputFile = parameterAsOutputLayer( parameters, QStringLiteral( "OUTPUT" ), context );
QFileInfo fi( outputFile );
const QString outputFormat = QgsRasterFileWriter::driverForExtension( fi.suffix() );

std::unique_ptr< QgsRasterFileWriter > writer = std::make_unique< QgsRasterFileWriter >( outputFile );
writer->setOutputProviderKey( QStringLiteral( "gdal" ) );
if ( !createOptions.isEmpty() )
{
writer->setCreateOptions( createOptions.split( '|' ) );
}
writer->setOutputFormat( outputFormat );
mOutputRasterDataProvider.reset( writer->createOneBandRaster( mDataType, mLayerWidth, mLayerHeight, mExtent, mCrs ) );
if ( !mOutputRasterDataProvider )
Expand Down
11 changes: 11 additions & 0 deletions src/analysis/processing/qgsalgorithmconstantraster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ void QgsConstantRasterAlgorithm::initAlgorithm( const QVariantMap & )
rasterTypeParameter->setFlags( Qgis::ProcessingParameterFlag::Advanced );
addParameter( rasterTypeParameter.release() );

std::unique_ptr< QgsProcessingParameterString > createOptsParam = std::make_unique< QgsProcessingParameterString >( QStringLiteral( "CREATE_OPTIONS" ), QObject::tr( "Creation options" ), QVariant(), false, true );
createOptsParam->setMetadata( QVariantMap( {{QStringLiteral( "widget_wrapper" ), QVariantMap( {{QStringLiteral( "widget_type" ), QStringLiteral( "rasteroptions" ) }} ) }} ) );
createOptsParam->setFlags( createOptsParam->flags() | Qgis::ProcessingParameterFlag::Advanced );
addParameter( createOptsParam.release() );

addParameter( new QgsProcessingParameterRasterDestination( QStringLiteral( "OUTPUT" ), QObject::tr( "Constant" ) ) );
}

Expand Down Expand Up @@ -152,6 +157,8 @@ QVariantMap QgsConstantRasterAlgorithm::processAlgorithm( const QVariantMap &par
default:
break;
}

const QString createOptions = parameterAsString( parameters, QStringLiteral( "CREATE_OPTIONS" ), context ).trimmed();
const QString outputFile = parameterAsOutputLayer( parameters, QStringLiteral( "OUTPUT" ), context );
const QFileInfo fi( outputFile );
const QString outputFormat = QgsRasterFileWriter::driverForExtension( fi.suffix() );
Expand All @@ -165,6 +172,10 @@ QVariantMap QgsConstantRasterAlgorithm::processAlgorithm( const QVariantMap &par

std::unique_ptr< QgsRasterFileWriter > writer = std::make_unique< QgsRasterFileWriter >( outputFile );
writer->setOutputProviderKey( QStringLiteral( "gdal" ) );
if ( !createOptions.isEmpty() )
{
writer->setCreateOptions( createOptions.split( '|' ) );
}
writer->setOutputFormat( outputFormat );
std::unique_ptr<QgsRasterDataProvider > provider( writer->createOneBandRaster( rasterDataType, cols, rows, rasterExtent, crs ) );
if ( !provider )
Expand Down
18 changes: 13 additions & 5 deletions src/analysis/processing/qgsalgorithmexportmesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -775,11 +775,14 @@ void QgsMeshRasterizeAlgorithm::initAlgorithm( const QVariantMap &configuration
QStringLiteral( "DATASET_GROUPS" ) ) );

addParameter( new QgsProcessingParameterExtent( QStringLiteral( "EXTENT" ), QObject::tr( "Extent" ), QVariant(), true ) );

addParameter( new QgsProcessingParameterDistance( QStringLiteral( "PIXEL_SIZE" ), QObject::tr( "Pixel size" ), 1, QStringLiteral( "INPUT" ), false ) );

addParameter( new QgsProcessingParameterCrs( QStringLiteral( "CRS_OUTPUT" ), QObject::tr( "Output coordinate system" ), QVariant(), true ) );

std::unique_ptr< QgsProcessingParameterString > createOptsParam = std::make_unique< QgsProcessingParameterString >( QStringLiteral( "CREATE_OPTIONS" ), QObject::tr( "Creation options" ), QVariant(), false, true );
createOptsParam->setMetadata( QVariantMap( {{QStringLiteral( "widget_wrapper" ), QVariantMap( {{QStringLiteral( "widget_type" ), QStringLiteral( "rasteroptions" ) }} ) }} ) );
createOptsParam->setFlags( createOptsParam->flags() | Qgis::ProcessingParameterFlag::Advanced );
addParameter( createOptsParam.release() );

addParameter( new QgsProcessingParameterRasterDestination( QStringLiteral( "OUTPUT" ), QObject::tr( "Output raster layer" ) ) );
}

Expand Down Expand Up @@ -845,11 +848,16 @@ QVariantMap QgsMeshRasterizeAlgorithm::processAlgorithm( const QVariantMap &para
int width = extent.width() / pixelSize;
int height = extent.height() / pixelSize;

QString fileName = parameterAsOutputLayer( parameters, QStringLiteral( "OUTPUT" ), context );
QFileInfo fileInfo( fileName );
QString outputFormat = QgsRasterFileWriter::driverForExtension( fileInfo.suffix() );
const QString createOptions = parameterAsString( parameters, QStringLiteral( "CREATE_OPTIONS" ), context ).trimmed();
const QString fileName = parameterAsOutputLayer( parameters, QStringLiteral( "OUTPUT" ), context );
const QFileInfo fileInfo( fileName );
const QString outputFormat = QgsRasterFileWriter::driverForExtension( fileInfo.suffix() );
QgsRasterFileWriter rasterFileWriter( fileName );
rasterFileWriter.setOutputProviderKey( QStringLiteral( "gdal" ) );
if ( !createOptions.isEmpty() )
{
rasterFileWriter.setCreateOptions( createOptions.split( '|' ) );
}
rasterFileWriter.setOutputFormat( outputFormat );

std::unique_ptr<QgsRasterDataProvider> rasterDataProvider(
Expand Down
12 changes: 11 additions & 1 deletion src/analysis/processing/qgsalgorithmfillnodata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ void QgsFillNoDataAlgorithm::initAlgorithm( const QVariantMap & )
addParameter( new QgsProcessingParameterRasterLayer( QStringLiteral( "INPUT" ), QStringLiteral( "Raster input" ) ) );
addParameter( new QgsProcessingParameterBand( QStringLiteral( "BAND" ), QObject::tr( "Band Number" ), 1, QStringLiteral( "INPUT" ) ) );
addParameter( new QgsProcessingParameterNumber( QStringLiteral( "FILL_VALUE" ), QObject::tr( "Fill value" ), Qgis::ProcessingNumberParameterType::Double, 1, false ) );

std::unique_ptr< QgsProcessingParameterString > createOptsParam = std::make_unique< QgsProcessingParameterString >( QStringLiteral( "CREATE_OPTIONS" ), QObject::tr( "Creation options" ), QVariant(), false, true );
createOptsParam->setMetadata( QVariantMap( {{QStringLiteral( "widget_wrapper" ), QVariantMap( {{QStringLiteral( "widget_type" ), QStringLiteral( "rasteroptions" ) }} ) }} ) );
createOptsParam->setFlags( createOptsParam->flags() | Qgis::ProcessingParameterFlag::Advanced );
addParameter( createOptsParam.release() );

addParameter( new QgsProcessingParameterRasterDestination( QStringLiteral( "OUTPUT" ), QObject::tr( "Output raster" ) ) );
}

Expand Down Expand Up @@ -98,11 +104,16 @@ QVariantMap QgsFillNoDataAlgorithm::processAlgorithm( const QVariantMap &paramet
feedback->reportError( QObject::tr( "Input raster has no NoData values. There exist no NoData cells to fill." ), false );

//prepare output dataset
const QString createOptions = parameterAsString( parameters, QStringLiteral( "CREATE_OPTIONS" ), context ).trimmed();
const QString outputFile = parameterAsOutputLayer( parameters, QStringLiteral( "OUTPUT" ), context );
const QFileInfo fi( outputFile );
const QString outputFormat = QgsRasterFileWriter::driverForExtension( fi.suffix() );
std::unique_ptr< QgsRasterFileWriter > writer = std::make_unique< QgsRasterFileWriter >( outputFile );
writer->setOutputProviderKey( QStringLiteral( "gdal" ) );
if ( !createOptions.isEmpty() )
{
writer->setCreateOptions( createOptions.split( '|' ) );
}
writer->setOutputFormat( outputFormat );
std::unique_ptr<QgsRasterDataProvider > provider( writer->createOneBandRaster( mInterface->dataType( mBand ), mNbCellsXProvider, mNbCellsYProvider, mExtent, mCrs ) );
if ( !provider )
Expand Down Expand Up @@ -161,5 +172,4 @@ QVariantMap QgsFillNoDataAlgorithm::processAlgorithm( const QVariantMap &paramet
return outputs;
}


///@endcond
14 changes: 10 additions & 4 deletions src/analysis/processing/qgsalgorithmfuzzifyraster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ void QgsFuzzifyRasterAlgorithmBase::initAlgorithm( const QVariantMap & )
addOutput( new QgsProcessingOutputNumber( QStringLiteral( "HEIGHT_IN_PIXELS" ), QObject::tr( "Height in pixels" ) ) );
addOutput( new QgsProcessingOutputNumber( QStringLiteral( "TOTAL_PIXEL_COUNT" ), QObject::tr( "Total pixel count" ) ) );

std::unique_ptr< QgsProcessingParameterString > createOptsParam = std::make_unique< QgsProcessingParameterString >( QStringLiteral( "CREATE_OPTIONS" ), QObject::tr( "Creation options" ), QVariant(), false, true );
createOptsParam->setMetadata( QVariantMap( {{QStringLiteral( "widget_wrapper" ), QVariantMap( {{QStringLiteral( "widget_type" ), QStringLiteral( "rasteroptions" ) }} ) }} ) );
createOptsParam->setFlags( createOptsParam->flags() | Qgis::ProcessingParameterFlag::Advanced );
addParameter( createOptsParam.release() );

addParameter( new QgsProcessingParameterRasterDestination( QStringLiteral( "OUTPUT" ), QObject::tr( "Fuzzified raster" ) ) );
}

Expand Down Expand Up @@ -77,15 +82,19 @@ bool QgsFuzzifyRasterAlgorithmBase::prepareAlgorithm( const QVariantMap &paramet
return true;
}


QVariantMap QgsFuzzifyRasterAlgorithmBase::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
{
const QString createOptions = parameterAsString( parameters, QStringLiteral( "CREATE_OPTIONS" ), context ).trimmed();
const QString outputFile = parameterAsOutputLayer( parameters, QStringLiteral( "OUTPUT" ), context );
const QFileInfo fi( outputFile );
const QString outputFormat = QgsRasterFileWriter::driverForExtension( fi.suffix() );

std::unique_ptr< QgsRasterFileWriter > writer = std::make_unique< QgsRasterFileWriter >( outputFile );
writer->setOutputProviderKey( QStringLiteral( "gdal" ) );
if ( !createOptions.isEmpty() )
{
writer->setCreateOptions( createOptions.split( '|' ) );
}
writer->setOutputFormat( outputFormat );
std::unique_ptr<QgsRasterDataProvider > provider( writer->createOneBandRaster( mDataType, mNbCellsXProvider, mNbCellsYProvider, mExtent, mCrs ) );
if ( !provider )
Expand Down Expand Up @@ -778,6 +787,3 @@ void QgsFuzzifyRasterNearMembershipAlgorithm::fuzzify( QgsRasterDataProvider *de


///@endcond



11 changes: 11 additions & 0 deletions src/analysis/processing/qgsalgorithmlinedensity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ void QgsLineDensityAlgorithm::initAlgorithm( const QVariantMap & )
addParameter( new QgsProcessingParameterDistance( QStringLiteral( "RADIUS" ), QObject::tr( "Search radius" ), 10, QStringLiteral( "INPUT" ), false, 0 ) );
addParameter( new QgsProcessingParameterDistance( QStringLiteral( "PIXEL_SIZE" ), QObject::tr( "Pixel size" ), 10, QStringLiteral( "INPUT" ), false ) );

std::unique_ptr< QgsProcessingParameterString > createOptsParam = std::make_unique< QgsProcessingParameterString >( QStringLiteral( "CREATE_OPTIONS" ), QObject::tr( "Creation options" ), QVariant(), false, true );
createOptsParam->setMetadata( QVariantMap( {{QStringLiteral( "widget_wrapper" ), QVariantMap( {{QStringLiteral( "widget_type" ), QStringLiteral( "rasteroptions" ) }} ) }} ) );
createOptsParam->setFlags( createOptsParam->flags() | Qgis::ProcessingParameterFlag::Advanced );
addParameter( createOptsParam.release() );

addParameter( new QgsProcessingParameterRasterDestination( QStringLiteral( "OUTPUT" ), QObject::tr( "Line density raster" ) ) );
}

Expand Down Expand Up @@ -126,6 +131,7 @@ QVariantMap QgsLineDensityAlgorithm::processAlgorithm( const QVariantMap &parame
}
}

const QString createOptions = parameterAsString( parameters, QStringLiteral( "CREATE_OPTIONS" ), context ).trimmed();
const QString outputFile = parameterAsOutputLayer( parameters, QStringLiteral( "OUTPUT" ), context );
const QFileInfo fi( outputFile );
const QString outputFormat = QgsRasterFileWriter::driverForExtension( fi.suffix() );
Expand All @@ -140,6 +146,11 @@ QVariantMap QgsLineDensityAlgorithm::processAlgorithm( const QVariantMap &parame
QgsRasterFileWriter writer = QgsRasterFileWriter( outputFile );
writer.setOutputProviderKey( QStringLiteral( "gdal" ) );
writer.setOutputFormat( outputFormat );
if ( !createOptions.isEmpty() )
{
writer.setCreateOptions( createOptions.split( '|' ) );
}

std::unique_ptr<QgsRasterDataProvider > provider( writer.createOneBandRaster( Qgis::DataType::Float32, cols, rows, rasterExtent, mCrs ) );
if ( !provider )
throw QgsProcessingException( QObject::tr( "Could not create raster output: %1" ).arg( outputFile ) );
Expand Down
11 changes: 11 additions & 0 deletions src/analysis/processing/qgsalgorithmrandomraster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ void QgsRandomRasterAlgorithmBase::initAlgorithm( const QVariantMap & )
//add specific parameters
addAlgorithmParams();

std::unique_ptr< QgsProcessingParameterString > createOptsParam = std::make_unique< QgsProcessingParameterString >( QStringLiteral( "CREATE_OPTIONS" ), QObject::tr( "Creation options" ), QVariant(), false, true );
createOptsParam->setMetadata( QVariantMap( {{QStringLiteral( "widget_wrapper" ), QVariantMap( {{QStringLiteral( "widget_type" ), QStringLiteral( "rasteroptions" ) }} ) }} ) );
createOptsParam->setFlags( createOptsParam->flags() | Qgis::ProcessingParameterFlag::Advanced );
addParameter( createOptsParam.release() );

addParameter( new QgsProcessingParameterRasterDestination( QStringLiteral( "OUTPUT" ), QObject::tr( "Output raster" ) ) );
}

Expand Down Expand Up @@ -74,6 +79,7 @@ QVariantMap QgsRandomRasterAlgorithmBase::processAlgorithm( const QVariantMap &p
std::random_device rd {};
std::mt19937 mersenneTwister{rd()};

const QString createOptions = parameterAsString( parameters, QStringLiteral( "CREATE_OPTIONS" ), context ).trimmed();
const QString outputFile = parameterAsOutputLayer( parameters, QStringLiteral( "OUTPUT" ), context );
const QFileInfo fi( outputFile );
const QString outputFormat = QgsRasterFileWriter::driverForExtension( fi.suffix() );
Expand All @@ -87,6 +93,11 @@ QVariantMap QgsRandomRasterAlgorithmBase::processAlgorithm( const QVariantMap &p

std::unique_ptr< QgsRasterFileWriter > writer = std::make_unique< QgsRasterFileWriter >( outputFile );
writer->setOutputProviderKey( QStringLiteral( "gdal" ) );
if ( !createOptions.isEmpty() )
{
writer->setCreateOptions( createOptions.split( '|' ) );
}

writer->setOutputFormat( outputFormat );
std::unique_ptr<QgsRasterDataProvider > provider( writer->createOneBandRaster( mRasterDataType, cols, rows, rasterExtent, mCrs ) );
if ( !provider )
Expand Down
14 changes: 14 additions & 0 deletions src/analysis/processing/qgsalgorithmrasterdtmslopebasedfilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ void QgsRasterDtmSlopeBasedFilterAlgorithm::initAlgorithm( const QVariantMap & )
stDevParam->setHelp( QObject::tr( "The standard deviation used to calculate a 5% confidence interval applied to the height threshold." ) );
addParameter( stDevParam.release() );

std::unique_ptr< QgsProcessingParameterString > createOptsParam = std::make_unique< QgsProcessingParameterString >( QStringLiteral( "CREATE_OPTIONS" ), QObject::tr( "Creation options" ), QVariant(), false, true );
createOptsParam->setMetadata( QVariantMap( {{QStringLiteral( "widget_wrapper" ), QVariantMap( {{QStringLiteral( "widget_type" ), QStringLiteral( "rasteroptions" ) }} ) }} ) );
createOptsParam->setFlags( createOptsParam->flags() | Qgis::ProcessingParameterFlag::Advanced );
addParameter( createOptsParam.release() );

std::unique_ptr< QgsProcessingParameterRasterDestination > outputLayerGroundParam = std::make_unique<QgsProcessingParameterRasterDestination>( QStringLiteral( "OUTPUT_GROUND" ),
QObject::tr( "Output layer (ground)" ), QVariant(), true, true );
outputLayerGroundParam->setHelp( QObject::tr( "The filtered DEM containing only cells classified as ground." ) );
Expand Down Expand Up @@ -135,6 +140,7 @@ bool QgsRasterDtmSlopeBasedFilterAlgorithm::prepareAlgorithm( const QVariantMap

QVariantMap QgsRasterDtmSlopeBasedFilterAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
{
const QString createOptions = parameterAsString( parameters, QStringLiteral( "CREATE_OPTIONS" ), context ).trimmed();
const QString groundOutputFile = parameterAsOutputLayer( parameters, QStringLiteral( "OUTPUT_GROUND" ), context );
std::unique_ptr< QgsRasterFileWriter > groundWriter;
std::unique_ptr<QgsRasterDataProvider > groundDestProvider;
Expand All @@ -146,6 +152,10 @@ QVariantMap QgsRasterDtmSlopeBasedFilterAlgorithm::processAlgorithm( const QVari

groundWriter = std::make_unique< QgsRasterFileWriter >( groundOutputFile );
groundWriter->setOutputProviderKey( QStringLiteral( "gdal" ) );
if ( !createOptions.isEmpty() )
{
groundWriter->setCreateOptions( createOptions.split( '|' ) );
}
groundWriter->setOutputFormat( outputFormat );

groundDestProvider.reset( groundWriter->createOneBandRaster( mDataType, mLayerWidth, mLayerHeight, mExtent, mCrs ) );
Expand All @@ -170,6 +180,10 @@ QVariantMap QgsRasterDtmSlopeBasedFilterAlgorithm::processAlgorithm( const QVari

nonGroundWriter = std::make_unique< QgsRasterFileWriter >( nonGroundOutputFile );
nonGroundWriter->setOutputProviderKey( QStringLiteral( "gdal" ) );
if ( !createOptions.isEmpty() )
{
nonGroundWriter->setCreateOptions( createOptions.split( '|' ) );
}
nonGroundWriter->setOutputFormat( outputFormat );

nonGroundDestProvider.reset( nonGroundWriter->createOneBandRaster( mDataType, mLayerWidth, mLayerHeight, mExtent, mCrs ) );
Expand Down
Loading

0 comments on commit 556c73f

Please sign in to comment.