26using namespace Qt::StringLiterals;
35QString QgsAlignRastersAlgorithm::name()
const
37 return u
"alignrasters"_s;
40QString QgsAlignRastersAlgorithm::displayName()
const
42 return QObject::tr(
"Align rasters" );
45QStringList QgsAlignRastersAlgorithm::tags()
const
47 return QObject::tr(
"raster,align,resample,rescale" ).split(
',' );
50QString QgsAlignRastersAlgorithm::group()
const
52 return QObject::tr(
"Raster tools" );
55QString QgsAlignRastersAlgorithm::groupId()
const
57 return u
"rastertools"_s;
60QString QgsAlignRastersAlgorithm::shortHelpString()
const
62 return QObject::tr(
"This algorithm aligns rasters by resampling them to the same cell size and reprojecting to the same CRS." );
65QString QgsAlignRastersAlgorithm::shortDescription()
const
67 return QObject::tr(
"Aligns rasters by resampling them to the same cell size and reprojecting to the same CRS." );
70QgsAlignRastersAlgorithm *QgsAlignRastersAlgorithm::createInstance()
const
72 return new QgsAlignRastersAlgorithm();
75bool QgsAlignRastersAlgorithm::checkParameterValues(
const QVariantMap ¶meters,
QgsProcessingContext &context, QString *message )
const
77 const QVariant layersVariant = parameters.value( parameterDefinition( u
"LAYERS"_s )->name() );
79 bool unconfiguredLayers =
false;
82 if ( item.outputFilename.isEmpty() )
84 unconfiguredLayers =
true;
88 if ( unconfiguredLayers )
90 *message = QObject::tr(
"An output file is not configured for one or more input layers. Configure output files via 'Configure Raster…' under Input layers parameter." );
96void QgsAlignRastersAlgorithm::initAlgorithm(
const QVariantMap & )
113 QgsRasterLayer *referenceLayer = parameterAsRasterLayer( parameters, u
"REFERENCE_LAYER"_s, context );
114 if ( !referenceLayer )
117 const QVariant layersVariant = parameters.value( parameterDefinition( u
"LAYERS"_s )->name() );
119 QStringList outputLayers;
120 outputLayers.reserve( items.size() );
123 outputLayers << item.outputFilename;
129 QString customCRSWkt;
130 QSizeF customCellSize;
131 QPointF customGridOffset( -1, -1 );
133 if ( parameters.value( u
"CRS"_s ).isValid() )
139 bool hasXValue = parameters.value( u
"CELL_SIZE_X"_s ).isValid();
140 bool hasYValue = parameters.value( u
"CELL_SIZE_Y"_s ).isValid();
141 if ( ( hasXValue && !hasYValue ) || ( !hasXValue && hasYValue ) )
143 throw QgsProcessingException( QObject::tr(
"Either set both X and Y cell size values or keep both as 'Not set'." ) );
145 else if ( hasXValue && hasYValue )
147 double xSize = parameterAsDouble( parameters, u
"CELL_SIZE_X"_s, context );
148 double ySize = parameterAsDouble( parameters, u
"CELL_SIZE_Y"_s, context );
149 customCellSize = QSizeF( xSize, ySize );
152 hasXValue = parameters.value( u
"GRID_OFFSET_X"_s ).isValid();
153 hasYValue = parameters.value( u
"GRID_OFFSET_Y"_s ).isValid();
154 if ( ( hasXValue && !hasYValue ) || ( !hasXValue && hasYValue ) )
156 throw QgsProcessingException( QObject::tr(
"Either set both X and Y grid offset values or keep both as 'Not set'." ) );
158 else if ( hasXValue && hasYValue )
160 double xSize = parameterAsDouble( parameters, u
"GRID_OFFSET_X"_s, context );
161 double ySize = parameterAsDouble( parameters, u
"GRID_OFFSET_Y"_s, context );
162 customGridOffset = QPointF( xSize, ySize );
165 if ( parameters.value( u
"EXTENT"_s ).isValid() )
167 QgsRectangle extent = parameterAsExtent( parameters, u
"EXTENT"_s, context );
173 explicit QgsAlignRasterProgress( QgsFeedback *feedback )
174 : mFeedback( feedback ) {}
175 bool progress(
double complete )
override
177 mFeedback->setProgress( complete * 100 );
182 QgsFeedback *mFeedback =
nullptr;
190 throw QgsProcessingException( QObject::tr(
"It is not possible to reproject reference raster to target CRS." ) );
193 result = rasterAlign.
run();
200 outputs.insert( u
"OUTPUT_LAYERS"_s, outputLayers );
QFlags< ProcessingAlgorithmFlag > ProcessingAlgorithmFlags
Flags indicating how and when an algorithm operates and should be exposed to users.
@ PreferredGdal
Preferred format for conversion of CRS to WKT for use with the GDAL library.
@ HideFromModeler
Algorithm should be hidden from the modeler.
@ Double
Double/float values.
Takes one or more raster layers and warps (resamples) them to a common grid.
bool 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.
bool run()
Run the alignment process.
void setClipExtent(double xmin, double ymin, double xmax, double ymax)
Configure clipping extent (region of interest).
void setProgressHandler(ProgressHandler *progressHandler)
Assign a progress handler instance. Does not take ownership. nullptr can be passed.
QString errorMessage() const
Returns the error from a previous run() call.
void setRasters(const List &list)
Sets list of rasters that will be aligned.
Represents a coordinate reference system (CRS).
QString toWkt(Qgis::CrsWktVariant variant=Qgis::CrsWktVariant::Wkt1Gdal, bool multiline=false, int indentationWidth=4) const
Returns a WKT representation of this CRS.
QString source() const
Returns the source for the layer.
virtual Qgis::ProcessingAlgorithmFlags flags() const
Returns the flags indicating how and when the algorithm operates and should be exposed to users.
virtual bool checkParameterValues(const QVariantMap ¶meters, QgsProcessingContext &context, QString *message=nullptr) const
Checks the supplied parameter values to verify that they satisfy the requirements of this algorithm i...
Contains information about the context in which a processing algorithm is executed.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
A multi-layer output for processing algorithms which create map layers, when the number and nature of...
A parameter for Processing algorithms specifying how rasters should be aligned.
static QList< QgsAlignRasterData::RasterItem > parameterAsItems(const QVariant &layersVariant, QgsProcessingContext &context)
Converts a QVariant value (a QVariantList) to a list of input layers.
A coordinate reference system parameter for processing algorithms.
A rectangular map extent parameter for processing algorithms.
A numeric parameter for processing algorithms.
A raster layer parameter for processing algorithms.
Represents a raster layer.
A rectangle specified with double values.
Definition of one raster layer for alignment.
Helper struct to be sub-classed for progress reporting.
virtual bool progress(double complete)=0
Method to be overridden for progress reporting.