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