QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
qgsalgorithmalignrasters.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmalignrasters.cpp
3 ---------------------
4 begin : July 2023
5 copyright : (C) 2023 by Alexander Bruy
6 email : alexander dot bruy at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
19
20#include "qgsalignraster.h"
21#include "qgsalignrasterdata.h"
23
25
26Qgis::ProcessingAlgorithmFlags QgsAlignRastersAlgorithm::flags() const
27{
29}
30
31QString QgsAlignRastersAlgorithm::name() const
32{
33 return QStringLiteral( "alignrasters" );
34}
35
36QString QgsAlignRastersAlgorithm::displayName() const
37{
38 return QObject::tr( "Align rasters" );
39}
40
41QStringList QgsAlignRastersAlgorithm::tags() const
42{
43 return QObject::tr( "raster,align,resample,rescale" ).split( ',' );
44}
45
46QString QgsAlignRastersAlgorithm::group() const
47{
48 return QObject::tr( "Raster tools" );
49}
50
51QString QgsAlignRastersAlgorithm::groupId() const
52{
53 return QStringLiteral( "rastertools" );
54}
55
56QString QgsAlignRastersAlgorithm::shortHelpString() const
57{
58 return QObject::tr( "This algorithm aligns rasters by resampling them to the same cell size and reprojecting to the same CRS." );
59}
60
61QString QgsAlignRastersAlgorithm::shortDescription() const
62{
63 return QObject::tr( "Aligns rasters by resampling them to the same cell size and reprojecting to the same CRS." );
64}
65
66QgsAlignRastersAlgorithm *QgsAlignRastersAlgorithm::createInstance() const
67{
68 return new QgsAlignRastersAlgorithm();
69}
70
71bool QgsAlignRastersAlgorithm::checkParameterValues( const QVariantMap &parameters, QgsProcessingContext &context, QString *message ) const
72{
73 const QVariant layersVariant = parameters.value( parameterDefinition( QStringLiteral( "LAYERS" ) )->name() );
74 const QList<QgsAlignRasterData::RasterItem> items = QgsProcessingParameterAlignRasterLayers::parameterAsItems( layersVariant, context );
75 bool unconfiguredLayers = false;
76 for ( const QgsAlignRasterData::RasterItem &item : items )
77 {
78 if ( item.outputFilename.isEmpty() )
79 {
80 unconfiguredLayers = true;
81 break;
82 }
83 }
84 if ( unconfiguredLayers )
85 {
86 *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." );
87 return false;
88 }
89 return QgsProcessingAlgorithm::checkParameterValues( parameters, context );
90}
91
92void QgsAlignRastersAlgorithm::initAlgorithm( const QVariantMap & )
93{
94 addParameter( new QgsProcessingParameterAlignRasterLayers( QStringLiteral( "LAYERS" ), QObject::tr( "Input layers" ) ) );
95 addParameter( new QgsProcessingParameterRasterLayer( QStringLiteral( "REFERENCE_LAYER" ), QObject::tr( "Reference layer" ) ) );
96
97 addParameter( new QgsProcessingParameterCrs( QStringLiteral( "CRS" ), QObject::tr( "Override reference CRS" ), QVariant(), true ) );
98 addParameter( new QgsProcessingParameterNumber( QStringLiteral( "CELL_SIZE_X" ), QObject::tr( "Override reference cell size X" ), Qgis::ProcessingNumberParameterType::Double, QVariant(), true, 1e-9 ) );
99 addParameter( new QgsProcessingParameterNumber( QStringLiteral( "CELL_SIZE_Y" ), QObject::tr( "Override reference cell size Y" ), Qgis::ProcessingNumberParameterType::Double, QVariant(), true, 1e-9 ) );
100 addParameter( new QgsProcessingParameterNumber( QStringLiteral( "GRID_OFFSET_X" ), QObject::tr( "Override reference grid offset X" ), Qgis::ProcessingNumberParameterType::Double, QVariant(), true, 1e-9 ) );
101 addParameter( new QgsProcessingParameterNumber( QStringLiteral( "GRID_OFFSET_Y" ), QObject::tr( "Override reference grid offset Y" ), Qgis::ProcessingNumberParameterType::Double, QVariant(), true, 1e-9 ) );
102 addParameter( new QgsProcessingParameterExtent( QStringLiteral( "EXTENT" ), QObject::tr( "Clip to extent" ), QVariant(), true ) );
103
104 addOutput( new QgsProcessingOutputMultipleLayers( QStringLiteral( "OUTPUT_LAYERS" ), QObject::tr( "Aligned rasters" ) ) );
105}
106
107QVariantMap QgsAlignRastersAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
108{
109 QgsRasterLayer *referenceLayer = parameterAsRasterLayer( parameters, QStringLiteral( "REFERENCE_LAYER" ), context );
110 if ( !referenceLayer )
111 throw QgsProcessingException( invalidRasterError( parameters, QStringLiteral( "REFERENCE_LAYER" ) ) );
112
113 const QVariant layersVariant = parameters.value( parameterDefinition( QStringLiteral( "LAYERS" ) )->name() );
114 const QList<QgsAlignRasterData::RasterItem> items = QgsProcessingParameterAlignRasterLayers::parameterAsItems( layersVariant, context );
115 QStringList outputLayers;
116 outputLayers.reserve( items.size() );
117 for ( const QgsAlignRasterData::RasterItem &item : items )
118 {
119 outputLayers << item.outputFilename;
120 }
121
122 QgsAlignRaster rasterAlign;
123 rasterAlign.setRasters( items );
124
125 QString customCRSWkt;
126 QSizeF customCellSize;
127 QPointF customGridOffset( -1, -1 );
128
129 if ( parameters.value( QStringLiteral( "CRS" ) ).isValid() )
130 {
131 QgsCoordinateReferenceSystem crs = parameterAsCrs( parameters, QStringLiteral( "CRS" ), context );
132 customCRSWkt = crs.toWkt( Qgis::CrsWktVariant::PreferredGdal );
133 }
134
135 bool hasXValue = parameters.value( QStringLiteral( "CELL_SIZE_X" ) ).isValid();
136 bool hasYValue = parameters.value( QStringLiteral( "CELL_SIZE_Y" ) ).isValid();
137 if ( ( hasXValue && !hasYValue ) || ( !hasXValue && hasYValue ) )
138 {
139 throw QgsProcessingException( QObject::tr( "Either set both X and Y cell size values or keep both as 'Not set'." ) );
140 }
141 else if ( hasXValue && hasYValue )
142 {
143 double xSize = parameterAsDouble( parameters, QStringLiteral( "CELL_SIZE_X" ), context );
144 double ySize = parameterAsDouble( parameters, QStringLiteral( "CELL_SIZE_Y" ), context );
145 customCellSize = QSizeF( xSize, ySize );
146 }
147
148 hasXValue = parameters.value( QStringLiteral( "GRID_OFFSET_X" ) ).isValid();
149 hasYValue = parameters.value( QStringLiteral( "GRID_OFFSET_Y" ) ).isValid();
150 if ( ( hasXValue && !hasYValue ) || ( !hasXValue && hasYValue ) )
151 {
152 throw QgsProcessingException( QObject::tr( "Either set both X and Y grid offset values or keep both as 'Not set'." ) );
153 }
154 else if ( hasXValue && hasYValue )
155 {
156 double xSize = parameterAsDouble( parameters, QStringLiteral( "GRID_OFFSET_X" ), context );
157 double ySize = parameterAsDouble( parameters, QStringLiteral( "GRID_OFFSET_Y" ), context );
158 customGridOffset = QPointF( xSize, ySize );
159 }
160
161 if ( parameters.value( QStringLiteral( "EXTENT" ) ).isValid() )
162 {
163 QgsRectangle extent = parameterAsExtent( parameters, QStringLiteral( "EXTENT" ), context );
164 rasterAlign.setClipExtent( extent );
165 }
166
167 struct QgsAlignRasterProgress : public QgsAlignRaster::ProgressHandler
168 {
169 explicit QgsAlignRasterProgress( QgsFeedback *feedback )
170 : mFeedback( feedback ) {}
171 bool progress( double complete ) override
172 {
173 mFeedback->setProgress( complete * 100 );
174 return true;
175 }
176
177 protected:
178 QgsFeedback *mFeedback = nullptr;
179 };
180
181 rasterAlign.setProgressHandler( new QgsAlignRasterProgress( feedback ) );
182
183 bool result = rasterAlign.setParametersFromRaster( referenceLayer->source(), customCRSWkt, customCellSize, customGridOffset );
184 if ( !result )
185 {
186 throw QgsProcessingException( QObject::tr( "It is not possible to reproject reference raster to target CRS." ) );
187 }
188
189 result = rasterAlign.run();
190 if ( !result )
191 {
192 throw QgsProcessingException( QObject::tr( "Failed to align rasters: %1" ).arg( rasterAlign.errorMessage() ) );
193 }
194
195 QVariantMap outputs;
196 outputs.insert( QStringLiteral( "OUTPUT_LAYERS" ), outputLayers );
197 return outputs;
198}
199
QFlags< ProcessingAlgorithmFlag > ProcessingAlgorithmFlags
Flags indicating how and when an algorithm operates and should be exposed to users.
Definition qgis.h:3609
@ PreferredGdal
Preferred format for conversion of CRS to WKT for use with the GDAL library.
Definition qgis.h:2441
@ HideFromModeler
Algorithm should be hidden from the modeler.
Definition qgis.h:3584
@ Double
Double/float values.
Definition qgis.h:3804
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 &parameters, 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.