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