QGIS API Documentation 4.3.0-Master (4a47d6a9bb1)
Loading...
Searching...
No Matches
qgsalgorithmidwinterpolation.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmidwinterpolation.cpp
3 -----------------------------------
4 begin : August 2026
5 copyright : (C) 2026 by Nyall Dawson
6 email : nyall dot dawson 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 "qgsgridfilewriter.h"
21#include "qgsidwinterpolator.h"
24#include "qgsprocessingutils.h"
25
26#include <QString>
27
28using namespace Qt::StringLiterals;
29
31QgsIdwInterpolationAlgorithm::QgsIdwInterpolationAlgorithm() = default;
32
33QString QgsIdwInterpolationAlgorithm::name() const
34{
35 return u"idwinterpolation"_s;
36}
37
38QString QgsIdwInterpolationAlgorithm::displayName() const
39{
40 return QObject::tr( "IDW interpolation" );
41}
42
43QStringList QgsIdwInterpolationAlgorithm::tags() const
44{
45 return QObject::tr( "idw,inverse,distance,weighted,interpolation,surface,grid,breaklines,breaks,structures" ).split( ',' );
46}
47
48QString QgsIdwInterpolationAlgorithm::group() const
49{
50 return QObject::tr( "Interpolation" );
51}
52
53QString QgsIdwInterpolationAlgorithm::groupId() const
54{
55 return u"interpolation"_s;
56}
57
58QString QgsIdwInterpolationAlgorithm::shortDescription() const
59{
60 return QObject::tr( "Generates an Inverse Distance Weighted (IDW) interpolation from vector layers." );
61}
62
63QString QgsIdwInterpolationAlgorithm::shortHelpString() const
64{
65 return QObject::tr(
66 "This algorithm generates an Inverse Distance Weighted (IDW) interpolation surface raster from one or more vector layers.\n\n"
67 "IDW calculates cell values using a linearly weighted combination of sample points. The weighting is an inverse function of "
68 "distance, meaning closer sample points exert greater influence on the target cell value than more distant points.\n\n"
69 "Input layers can supply sample values from feature attributes or feature Z coordinates. Features can be specified "
70 "as discrete points, structure lines, or breaklines."
71 );
72}
73
74QgsIdwInterpolationAlgorithm *QgsIdwInterpolationAlgorithm::createInstance() const
75{
76 return new QgsIdwInterpolationAlgorithm();
77}
78
79void QgsIdwInterpolationAlgorithm::initAlgorithm( const QVariantMap & )
80{
81 auto dataParam = std::make_unique<QgsProcessingParameterInterpolationSource>( u"INTERPOLATION_DATA"_s, QObject::tr( "Input layer(s)" ) );
82 dataParam->setHelp( QObject::tr( "Vector layers to use for interpolation along with their attributes and source types." ) );
83 addParameter( dataParam.release() );
84
85 auto distanceParam
86 = std::make_unique<QgsProcessingParameterNumber>( u"DISTANCE_COEFFICIENT"_s, QObject::tr( "Distance coefficient P" ), Qgis::ProcessingNumberParameterType::Double, 2.0, false, 0.0, 99.99 );
87 distanceParam->setHelp( QObject::tr( "Distance coefficient determines how influence decreases with distance." ) );
88 addParameter( distanceParam.release() );
89
90 auto extentParam = std::make_unique<QgsProcessingParameterExtent>( u"EXTENT"_s, QObject::tr( "Extent" ), QVariant(), false );
91 extentParam->setHelp( QObject::tr( "Bounding box defining the extent of the output raster grid." ) );
92 addParameter( extentParam.release() );
93
94 auto pixelSizeParam = std::make_unique<QgsProcessingParameterInterpolationPixelSize>( u"PIXEL_SIZE"_s, QObject::tr( "Output raster size" ), u"INTERPOLATION_DATA"_s, u"EXTENT"_s, 0.1 );
95 pixelSizeParam->setHelp( QObject::tr( "Pixel size in layer units used to calculate output grid dimensions." ) );
96 addParameter( pixelSizeParam.release() );
97
98 auto colsParam = std::make_unique<QgsProcessingParameterNumber>( u"COLUMNS"_s, QObject::tr( "Number of columns" ), Qgis::ProcessingNumberParameterType::Integer, QVariant(), true, 0, 10000000 );
99 colsParam->setFlags( colsParam->flags() | Qgis::ProcessingParameterFlag::Hidden );
100 addParameter( colsParam.release() );
101
102 auto rowsParam = std::make_unique<QgsProcessingParameterNumber>( u"ROWS"_s, QObject::tr( "Number of rows" ), Qgis::ProcessingNumberParameterType::Integer, QVariant(), true, 0, 10000000 );
103 rowsParam->setFlags( rowsParam->flags() | Qgis::ProcessingParameterFlag::Hidden );
104 addParameter( rowsParam.release() );
105
106 auto outputNodataParam = std::make_unique<QgsProcessingParameterNumber>( u"NODATA"_s, QObject::tr( "Output NoData value" ), Qgis::ProcessingNumberParameterType::Double, -9999.0 );
107 outputNodataParam->setHelp( QObject::tr( "The NODATA value to use in the output raster." ) );
108 outputNodataParam->setFlags( outputNodataParam->flags() | Qgis::ProcessingParameterFlag::Advanced );
109 addParameter( outputNodataParam.release() );
110
111 auto creationOptsParam = std::make_unique<QgsProcessingParameterString>( u"CREATION_OPTIONS"_s, QObject::tr( "Creation options" ), QVariant(), false, true );
112 creationOptsParam->setHelp( QObject::tr( "The raster creation options for the output raster. These options control things like colorimetry, compression, etc." ) );
113 creationOptsParam->setMetadata( QVariantMap( { { u"widget_wrapper"_s, QVariantMap( { { u"widget_type"_s, u"rasteroptions"_s } } ) } } ) );
114 creationOptsParam->setFlags( creationOptsParam->flags() | Qgis::ProcessingParameterFlag::Advanced );
115 addParameter( creationOptsParam.release() );
116
117 auto outputParam = std::make_unique<QgsProcessingParameterRasterDestination>( u"OUTPUT"_s, QObject::tr( "Interpolated" ) );
118 addParameter( outputParam.release() );
119}
120
121QVariantMap QgsIdwInterpolationAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
122{
123 const QString interpolationData = parameterAsString( parameters, u"INTERPOLATION_DATA"_s, context );
124
125 const double coefficient = parameterAsDouble( parameters, u"DISTANCE_COEFFICIENT"_s, context );
126 const QgsRectangle boundingBox = parameterAsExtent( parameters, u"EXTENT"_s, context );
127 const double pixelSize = parameterAsDouble( parameters, u"PIXEL_SIZE"_s, context );
128 const QString creationOptions = parameterAsString( parameters, u"CREATION_OPTIONS"_s, context ).trimmed();
129 const double outputNodata = parameterAsDouble( parameters, u"NODATA"_s, context );
130 const QString output = parameterAsOutputLayer( parameters, u"OUTPUT"_s, context );
131
132 int columns = parameterAsInt( parameters, u"COLUMNS"_s, context );
133 int rows = parameterAsInt( parameters, u"ROWS"_s, context );
134 if ( columns == 0 )
135 {
136 columns = std::max( static_cast<int>( std::ceil( boundingBox.width() / pixelSize ) ), 1 );
137 }
138 if ( rows == 0 )
139 {
140 rows = std::max( static_cast<int>( std::ceil( boundingBox.height() / pixelSize ) ), 1 );
141 }
142
143 if ( interpolationData.isEmpty() )
144 {
145 throw QgsProcessingException( QObject::tr( "At least one input layer must be specified." ) );
146 }
147
148 QList<QgsInterpolator::LayerData> layerDataList;
149 std::vector<std::unique_ptr<QgsFeatureSource>> sourceHolders;
150
151 const QStringList layerRows = interpolationData.split( "::|::"_L1, Qt::SkipEmptyParts );
152 for ( const QString &row : std::as_const( layerRows ) )
153 {
154 const QStringList tokens = row.split( "::~::"_L1 );
155 if ( tokens.size() < 4 )
156 {
157 continue;
158 }
159
161 std::unique_ptr<QgsProcessingFeatureSource> source( QgsProcessingUtils::variantToSource( tokens.at( 0 ), context ) );
162
163 if ( !source )
164 {
165 throw QgsProcessingException( QObject::tr( "Could not load source layer for input %1." ).arg( tokens.at( 0 ) ) );
166 }
167
168 data.source = source.get();
169 data.transformContext = context.transformContext();
170
171 data.valueSource = static_cast<Qgis::InterpolationValueSource>( tokens.at( 1 ).toInt() );
172
173 bool intOk = false;
174 data.interpolationAttribute = tokens.at( 2 ).toInt( &intOk );
175
177 {
178 if ( !intOk )
179 {
180 data.interpolationAttribute = source->fields().lookupField( tokens.at( 2 ) );
181 if ( data.interpolationAttribute < 0 )
182 {
183 throw QgsProcessingException( QObject::tr( "Field %1 does not exist in layer %2." ).arg( tokens.at( 2 ), source->sourceName() ) );
184 }
185 }
186 else if ( data.interpolationAttribute < 0 )
187 {
188 throw QgsProcessingException( QObject::tr( "Layer %1 is set to use a value attribute, but no attribute was set." ).arg( source->sourceName() ) );
189 }
190 else if ( data.interpolationAttribute >= source->fields().size() )
191 {
192 throw QgsProcessingException( QObject::tr( "Layer %1 is set to use an invalid attribute." ).arg( source->sourceName() ) );
193 }
194 }
195
196 data.sourceType = static_cast<Qgis::InterpolationSourceType>( tokens.at( 3 ).toInt() );
197
198 layerDataList.append( data );
199 sourceHolders.push_back( std::move( source ) );
200 }
201
202 QgsIDWInterpolator interpolator( layerDataList );
203 interpolator.setDistanceCoefficient( coefficient );
204
205 QgsGridFileWriter writer( &interpolator, output, boundingBox, columns, rows );
206 if ( !creationOptions.isEmpty() )
207 {
208 writer.setCreationOptions( creationOptions.split( '|' ) );
209 }
210 writer.setNoDataValue( outputNodata );
211
212 writer.writeFile( feedback );
213
214 QVariantMap outputs;
215 outputs.insert( u"OUTPUT"_s, output );
216 return outputs;
217}
InterpolationSourceType
Interpolation source types.
Definition qgis.h:6639
InterpolationValueSource
Source for interpolated values from features.
Definition qgis.h:6652
@ Attribute
Take value from feature's attribute.
Definition qgis.h:6653
@ Hidden
Parameter is hidden and should not be shown to users.
Definition qgis.h:3983
@ Advanced
Parameter is an advanced parameter which should be hidden from users by default.
Definition qgis.h:3982
@ Double
Double/float values.
Definition qgis.h:4023
Handles interpolation to a grid and writes the results to a raster grid file.
Inverse distance weight interpolator.
Contains information about the context in which a processing algorithm is executed.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
static QgsProcessingFeatureSource * variantToSource(const QVariant &value, QgsProcessingContext &context, const QVariant &fallbackValue=QVariant())
Converts a variant value to a new feature source.
A rectangle specified with double values.
A source together with the information about interpolation attribute / z-coordinate interpolation and...
QgsFeatureSource * source
Feature source.
Qgis::InterpolationSourceType sourceType
Source type.
QgsCoordinateTransformContext transformContext
Coordinate transform context.
int interpolationAttribute
Index of feature attribute to use for interpolation.
Qgis::InterpolationValueSource valueSource
Source for feature values to interpolate.