QGIS API Documentation 3.41.0-Master (af5edcb665c)
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
20#include "qgsalignraster.h"
21#include "qgsalignrasterdata.h"
22
24
25Qgis::ProcessingAlgorithmFlags QgsAlignRastersAlgorithm::flags() const
26{
28}
29
30QString QgsAlignRastersAlgorithm::name() const
31{
32 return QStringLiteral( "alignrasters" );
33}
34
35QString QgsAlignRastersAlgorithm::displayName() const
36{
37 return QObject::tr( "Align rasters" );
38}
39
40QStringList QgsAlignRastersAlgorithm::tags() const
41{
42 return QObject::tr( "raster,align,resample,rescale" ).split( ',' );
43}
44
45QString QgsAlignRastersAlgorithm::group() const
46{
47 return QObject::tr( "Raster tools" );
48}
49
50QString QgsAlignRastersAlgorithm::groupId() const
51{
52 return QStringLiteral( "rastertools" );
53}
54
55QString QgsAlignRastersAlgorithm::shortHelpString() const
56{
57 return QObject::tr( "Aligns rasters by resampling them to the same cell size and reprojecting to the same CRS." );
58}
59
60QgsAlignRastersAlgorithm *QgsAlignRastersAlgorithm::createInstance() const
61{
62 return new QgsAlignRastersAlgorithm();
63}
64
65bool QgsAlignRastersAlgorithm::checkParameterValues( const QVariantMap &parameters, QgsProcessingContext &context, QString *message ) const
66{
67 const QVariant layersVariant = parameters.value( parameterDefinition( QStringLiteral( "LAYERS" ) )->name() );
68 const QList<QgsAlignRasterData::RasterItem> items = QgsProcessingParameterAlignRasterLayers::parameterAsItems( layersVariant, context );
69 bool unconfiguredLayers = false;
70 for ( const QgsAlignRasterData::RasterItem &item : items )
71 {
72 if ( item.outputFilename.isEmpty() )
73 {
74 unconfiguredLayers = true;
75 break;
76 }
77 }
78 if ( unconfiguredLayers )
79 {
80 *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." );
81 return false;
82 }
83 return QgsProcessingAlgorithm::checkParameterValues( parameters, context );
84}
85
86void QgsAlignRastersAlgorithm::initAlgorithm( const QVariantMap & )
87{
88 addParameter( new QgsProcessingParameterAlignRasterLayers( QStringLiteral( "LAYERS" ), QObject::tr( "Input layers" ) ) );
89 addParameter( new QgsProcessingParameterRasterLayer( QStringLiteral( "REFERENCE_LAYER" ), QObject::tr( "Reference layer" ) ) );
90
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
98 addOutput( new QgsProcessingOutputMultipleLayers( QStringLiteral( "OUTPUT_LAYERS" ), QObject::tr( "Aligned rasters" ) ) );
99}
100
101QVariantMap QgsAlignRastersAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
102{
103 QgsRasterLayer *referenceLayer = parameterAsRasterLayer( parameters, QStringLiteral( "REFERENCE_LAYER" ), context );
104 if ( !referenceLayer )
105 throw QgsProcessingException( invalidRasterError( parameters, QStringLiteral( "REFERENCE_LAYER" ) ) );
106
107 const QVariant layersVariant = parameters.value( parameterDefinition( QStringLiteral( "LAYERS" ) )->name() );
108 const QList<QgsAlignRasterData::RasterItem> items = QgsProcessingParameterAlignRasterLayers::parameterAsItems( layersVariant, context );
109 QStringList outputLayers;
110 outputLayers.reserve( items.size() );
111 for ( const QgsAlignRasterData::RasterItem &item : items )
112 {
113 outputLayers << item.outputFilename;
114 }
115
116 QgsAlignRaster rasterAlign;
117 rasterAlign.setRasters( items );
118
119 QString customCRSWkt;
120 QSizeF customCellSize;
121 QPointF customGridOffset( -1, -1 );
122
123 if ( parameters.value( QStringLiteral( "CRS" ) ).isValid() )
124 {
125 QgsCoordinateReferenceSystem crs = parameterAsCrs( parameters, QStringLiteral( "CRS" ), context );
127 }
128
129 bool hasXValue = parameters.value( QStringLiteral( "CELL_SIZE_X" ) ).isValid();
130 bool hasYValue = parameters.value( QStringLiteral( "CELL_SIZE_Y" ) ).isValid();
131 if ( ( hasXValue && !hasYValue ) || ( !hasXValue && hasYValue ) )
132 {
133 throw QgsProcessingException( QObject::tr( "Either set both X and Y cell size values or keep both as 'Not set'." ) );
134 }
135 else if ( hasXValue && hasYValue )
136 {
137 double xSize = parameterAsDouble( parameters, QStringLiteral( "CELL_SIZE_X" ), context );
138 double ySize = parameterAsDouble( parameters, QStringLiteral( "CELL_SIZE_Y" ), context );
139 customCellSize = QSizeF( xSize, ySize );
140 }
141
142 hasXValue = parameters.value( QStringLiteral( "GRID_OFFSET_X" ) ).isValid();
143 hasYValue = parameters.value( QStringLiteral( "GRID_OFFSET_Y" ) ).isValid();
144 if ( ( hasXValue && !hasYValue ) || ( !hasXValue && hasYValue ) )
145 {
146 throw QgsProcessingException( QObject::tr( "Either set both X and Y grid offset values or keep both as 'Not set'." ) );
147 }
148 else if ( hasXValue && hasYValue )
149 {
150 double xSize = parameterAsDouble( parameters, QStringLiteral( "GRID_OFFSET_X" ), context );
151 double ySize = parameterAsDouble( parameters, QStringLiteral( "GRID_OFFSET_Y" ), context );
152 customGridOffset = QPointF( xSize, ySize );
153 }
154
155 if ( parameters.value( QStringLiteral( "EXTENT" ) ).isValid() )
156 {
157 QgsRectangle extent = parameterAsExtent( parameters, QStringLiteral( "EXTENT" ), context );
158 rasterAlign.setClipExtent( extent );
159 }
160
161 struct QgsAlignRasterProgress : public QgsAlignRaster::ProgressHandler
162 {
163 explicit QgsAlignRasterProgress( QgsFeedback *feedback )
164 : mFeedback( feedback ) {}
165 bool progress( double complete ) override
166 {
167 mFeedback->setProgress( complete * 100 );
168 return true;
169 }
170
171 protected:
172 QgsFeedback *mFeedback = nullptr;
173 };
174
175 rasterAlign.setProgressHandler( new QgsAlignRasterProgress( feedback ) );
176
177 bool result = rasterAlign.setParametersFromRaster( referenceLayer->source(), customCRSWkt, customCellSize, customGridOffset );
178 if ( !result )
179 {
180 throw QgsProcessingException( QObject::tr( "It is not possible to reproject reference raster to target CRS." ) );
181 }
182
183 result = rasterAlign.run();
184 if ( !result )
185 {
186 throw QgsProcessingException( QObject::tr( "Failed to align rasters: %1" ).arg( rasterAlign.errorMessage() ) );
187 }
188
189 QVariantMap outputs;
190 outputs.insert( QStringLiteral( "OUTPUT_LAYERS" ), outputLayers );
191 return outputs;
192}
193
QFlags< ProcessingAlgorithmFlag > ProcessingAlgorithmFlags
Flags indicating how and when an algorithm operates and should be exposed to users.
Definition qgis.h:3410
@ PreferredGdal
Preferred format for conversion of CRS to WKT for use with the GDAL library.
@ HideFromModeler
Algorithm should be hidden from the modeler.
QgsAlignRaster takes one or more raster layers and warps (resamples) them so they have the same:
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.
This class 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.
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 that need a list of input raster layers to align - this paramet...
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.
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.