QGIS API Documentation 3.43.0-Master (261ee7da134)
qgsalgorithmalignsingleraster.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmalignsingleraster.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
20#include "qgsalignraster.h"
21#include "qgsalignrasterdata.h"
22#include "qgis.h"
23
25
26Qgis::ProcessingAlgorithmFlags QgsAlignSingleRasterAlgorithm::flags() const
27{
29}
30
31QString QgsAlignSingleRasterAlgorithm::name() const
32{
33 return QStringLiteral( "alignsingleraster" );
34}
35
36QString QgsAlignSingleRasterAlgorithm::displayName() const
37{
38 return QObject::tr( "Align raster" );
39}
40
41QStringList QgsAlignSingleRasterAlgorithm::tags() const
42{
43 return QObject::tr( "raster,align,resample,rescale" ).split( ',' );
44}
45
46QString QgsAlignSingleRasterAlgorithm::group() const
47{
48 return QObject::tr( "Raster tools" );
49}
50
51QString QgsAlignSingleRasterAlgorithm::groupId() const
52{
53 return QStringLiteral( "rastertools" );
54}
55
56QString QgsAlignSingleRasterAlgorithm::shortHelpString() const
57{
58 return QObject::tr( "This algorithm aligns raster by resampling it to the same cell size and reprojecting to the same CRS as a reference raster." );
59}
60
61QString QgsAlignSingleRasterAlgorithm::shortDescription() const
62{
63 return QObject::tr( "Aligns raster by resampling it to the same cell size and reprojecting to the same CRS as a reference raster." );
64}
65
66QgsAlignSingleRasterAlgorithm *QgsAlignSingleRasterAlgorithm::createInstance() const
67{
68 return new QgsAlignSingleRasterAlgorithm();
69}
70
71void QgsAlignSingleRasterAlgorithm::initAlgorithm( const QVariantMap & )
72{
73 addParameter( new QgsProcessingParameterRasterLayer( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ) ) );
74
75 QStringList resamplingMethods;
76 resamplingMethods << QObject::tr( "Nearest Neighbour" )
77 << QObject::tr( "Bilinear (2x2 Kernel)" )
78 << QObject::tr( "Cubic (4x4 Kernel)" )
79 << QObject::tr( "Cubic B-Spline (4x4 Kernel)" )
80 << QObject::tr( "Lanczos (6x6 Kernel)" )
81 << QObject::tr( "Average" )
82 << QObject::tr( "Mode" )
83 << QObject::tr( "Maximum" )
84 << QObject::tr( "Minimum" )
85 << QObject::tr( "Median" )
86 << QObject::tr( "First Quartile (Q1)" )
87 << QObject::tr( "Third Quartile (Q3)" );
88 addParameter( new QgsProcessingParameterEnum( QStringLiteral( "RESAMPLING_METHOD" ), QObject::tr( "Resampling method" ), resamplingMethods, false, 0, false ) );
89 addParameter( new QgsProcessingParameterBoolean( QStringLiteral( "RESCALE" ), QObject::tr( "Rescale values according to the cell size" ), false ) );
90 addParameter( new QgsProcessingParameterRasterLayer( QStringLiteral( "REFERENCE_LAYER" ), QObject::tr( "Reference layer" ) ) );
91 addParameter( new QgsProcessingParameterCrs( QStringLiteral( "CRS" ), QObject::tr( "Override reference CRS" ), QVariant(), true ) );
92 addParameter( new QgsProcessingParameterNumber( QStringLiteral( "CELL_SIZE_X" ), QObject::tr( "Override reference cell size X" ), Qgis::ProcessingNumberParameterType::Double, QVariant(), true, 1e-9 ) );
93 addParameter( new QgsProcessingParameterNumber( QStringLiteral( "CELL_SIZE_Y" ), QObject::tr( "Override reference cell size Y" ), Qgis::ProcessingNumberParameterType::Double, QVariant(), true, 1e-9 ) );
94 addParameter( new QgsProcessingParameterNumber( QStringLiteral( "GRID_OFFSET_X" ), QObject::tr( "Override reference grid offset X" ), Qgis::ProcessingNumberParameterType::Double, QVariant(), true, 1e-9 ) );
95 addParameter( new QgsProcessingParameterNumber( QStringLiteral( "GRID_OFFSET_Y" ), QObject::tr( "Override reference grid offset Y" ), Qgis::ProcessingNumberParameterType::Double, QVariant(), true, 1e-9 ) );
96 addParameter( new QgsProcessingParameterExtent( QStringLiteral( "EXTENT" ), QObject::tr( "Clip to extent" ), QVariant(), true ) );
97 addParameter( new QgsProcessingParameterRasterDestination( QStringLiteral( "OUTPUT" ), QObject::tr( "Aligned raster" ) ) );
98}
99
100
101QVariantMap QgsAlignSingleRasterAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
102{
103 QgsRasterLayer *inputLayer = parameterAsRasterLayer( parameters, QStringLiteral( "INPUT" ), context );
104 if ( !inputLayer )
105 throw QgsProcessingException( invalidRasterError( parameters, QStringLiteral( "INPUT" ) ) );
106
107 QgsRasterLayer *referenceLayer = parameterAsRasterLayer( parameters, QStringLiteral( "REFERENCE_LAYER" ), context );
108 if ( !referenceLayer )
109 throw QgsProcessingException( invalidRasterError( parameters, QStringLiteral( "REFERENCE_LAYER" ) ) );
110
111 const int method = parameterAsInt( parameters, QStringLiteral( "RESAMPLING_METHOD" ), context );
112 const bool rescale = parameterAsBoolean( parameters, QStringLiteral( "RESCALE" ), context );
113 const QString outputFile = parameterAsOutputLayer( parameters, QStringLiteral( "OUTPUT" ), context );
114
116 switch ( method )
117 {
118 case 0:
120 break;
121 case 1:
123 break;
124 case 2:
126 break;
127 case 3:
129 break;
130 case 4:
132 break;
133 case 5:
135 break;
136 case 6:
138 break;
139 case 7:
141 break;
142 case 8:
144 break;
145 case 9:
147 break;
148 case 10:
150 break;
151 case 11:
153 break;
154 default:
155 break;
156 }
157
158 QgsAlignRasterData::RasterItem item( inputLayer->source(), outputFile );
159 item.resampleMethod = resampleAlg;
160 item.rescaleValues = rescale;
161
163 items << item;
164
165 QgsAlignRaster rasterAlign;
166 rasterAlign.setRasters( items );
167
168 QString customCRSWkt;
169 QSizeF customCellSize;
170 QPointF customGridOffset( -1, -1 );
171
172 if ( parameters.value( QStringLiteral( "CRS" ) ).isValid() )
173 {
174 QgsCoordinateReferenceSystem crs = parameterAsCrs( parameters, QStringLiteral( "CRS" ), context );
176 }
177
178 bool hasXValue = parameters.value( QStringLiteral( "CELL_SIZE_X" ) ).isValid();
179 bool hasYValue = parameters.value( QStringLiteral( "CELL_SIZE_Y" ) ).isValid();
180 if ( ( hasXValue && !hasYValue ) || ( !hasXValue && hasYValue ) )
181 {
182 throw QgsProcessingException( QObject::tr( "Either set both X and Y cell size values or keep both as 'Not set'." ) );
183 }
184 else if ( hasXValue && hasYValue )
185 {
186 double xSize = parameterAsDouble( parameters, QStringLiteral( "CELL_SIZE_X" ), context );
187 double ySize = parameterAsDouble( parameters, QStringLiteral( "CELL_SIZE_Y" ), context );
188 customCellSize = QSizeF( xSize, ySize );
189 }
190
191 hasXValue = parameters.value( QStringLiteral( "GRID_OFFSET_X" ) ).isValid();
192 hasYValue = parameters.value( QStringLiteral( "GRID_OFFSET_Y" ) ).isValid();
193 if ( ( hasXValue && !hasYValue ) || ( !hasXValue && hasYValue ) )
194 {
195 throw QgsProcessingException( QObject::tr( "Either set both X and Y grid offset values or keep both as 'Not set'." ) );
196 }
197 else if ( hasXValue && hasYValue )
198 {
199 double xSize = parameterAsDouble( parameters, QStringLiteral( "GRID_OFFSET_X" ), context );
200 double ySize = parameterAsDouble( parameters, QStringLiteral( "GRID_OFFSET_Y" ), context );
201 customGridOffset = QPointF( xSize, ySize );
202 }
203
204 if ( parameters.value( QStringLiteral( "EXTENT" ) ).isValid() )
205 {
206 QgsRectangle extent = parameterAsExtent( parameters, QStringLiteral( "EXTENT" ), context );
207 rasterAlign.setClipExtent( extent );
208 }
209
210 struct QgsAlignRasterProgress : public QgsAlignRaster::ProgressHandler
211 {
212 explicit QgsAlignRasterProgress( QgsFeedback *feedback )
213 : mFeedback( feedback ) {}
214 bool progress( double complete ) override
215 {
216 mFeedback->setProgress( complete * 100 );
217 return true;
218 }
219
220 protected:
221 QgsFeedback *mFeedback = nullptr;
222 };
223
224 rasterAlign.setProgressHandler( new QgsAlignRasterProgress( feedback ) );
225
226 bool result = rasterAlign.setParametersFromRaster( referenceLayer->source(), customCRSWkt, customCellSize, customGridOffset );
227 if ( !result )
228 {
229 throw QgsProcessingException( QObject::tr( "It is not possible to reproject reference raster to target CRS." ) );
230 }
231
232 result = rasterAlign.run();
233 if ( !result )
234 {
235 throw QgsProcessingException( QObject::tr( "Failed to align rasters: %1" ).arg( rasterAlign.errorMessage() ) );
236 }
237
238 QVariantMap outputs;
239 outputs.insert( QStringLiteral( "OUTPUT" ), outputFile );
240 return outputs;
241}
242
GdalResampleAlgorithm
Resampling algorithm to be used (equivalent to GDAL's enum GDALResampleAlg)
Definition qgis.h:5518
@ RA_Lanczos
Lanczos windowed sinc interpolation (6x6 kernel)
@ RA_Q3
Third quartile (selects the third quartile of all non-NODATA contributing pixels)
@ RA_CubicSpline
Cubic B-Spline Approximation (4x4 kernel)
@ RA_Q1
First quartile (selects the first quartile of all non-NODATA contributing pixels)
@ RA_Min
Minimum (selects the minimum of all non-NODATA contributing pixels)
@ RA_Median
Median (selects the median of all non-NODATA contributing pixels)
@ RA_NearestNeighbour
Nearest neighbour (select on one input pixel)
@ RA_Average
Average (computes the average of all non-NODATA contributing pixels)
@ RA_Max
Maximum (selects the maximum of all non-NODATA contributing pixels)
@ RA_Bilinear
Bilinear (2x2 kernel)
@ RA_Mode
Mode (selects the value which appears most often of all the sampled points)
@ RA_Cubic
Cubic Convolution Approximation (4x4 kernel)
QFlags< ProcessingAlgorithmFlag > ProcessingAlgorithmFlags
Flags indicating how and when an algorithm operates and should be exposed to users.
Definition qgis.h:3476
@ PreferredGdal
Preferred format for conversion of CRS to WKT for use with the GDAL library.
@ HideFromToolbox
Algorithm should be hidden from the toolbox.
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.
QList< QgsAlignRasterData::RasterItem > List
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.
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition qgsfeedback.h:44
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.
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 boolean parameter for processing algorithms.
A coordinate reference system parameter for processing algorithms.
An enum based parameter for processing algorithms, allowing for selection from predefined values.
A rectangular map extent parameter for processing algorithms.
A numeric parameter for processing algorithms.
A raster layer destination parameter, for specifying the destination path for a raster layer created ...
A raster layer parameter for processing algorithms.
Represents a raster layer.
A rectangle specified with double values.
const QgsCoordinateReferenceSystem & crs
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.