QGIS API Documentation 4.3.0-Master (4a47d6a9bb1)
Loading...
Searching...
No Matches
qgsalgorithmtininterpolation.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmtininterpolation.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"
23#include "qgsprocessingutils.h"
24#include "qgstininterpolator.h"
25
26#include <QString>
27
28using namespace Qt::StringLiterals;
29
31QgsTinInterpolationAlgorithm::QgsTinInterpolationAlgorithm() = default;
32
33QString QgsTinInterpolationAlgorithm::name() const
34{
35 return u"tininterpolation"_s;
36}
37
38QString QgsTinInterpolationAlgorithm::displayName() const
39{
40 return QObject::tr( "TIN interpolation" );
41}
42
43QStringList QgsTinInterpolationAlgorithm::tags() const
44{
45 return QObject::tr( "tin,triangulated,irregular,network,delaunay,interpolation,surface,breaklines,breaks,structures" ).split( ',' );
46}
47
48QString QgsTinInterpolationAlgorithm::group() const
49{
50 return QObject::tr( "Interpolation" );
51}
52
53QString QgsTinInterpolationAlgorithm::groupId() const
54{
55 return u"interpolation"_s;
56}
57
58QString QgsTinInterpolationAlgorithm::shortDescription() const
59{
60 return QObject::tr( "Generates a Triangulated Irregular Network (TIN) interpolation from vector layers." );
61}
62
63QString QgsTinInterpolationAlgorithm::shortHelpString() const
64{
65 return QObject::tr(
66 "This algorithm generates a Triangulated Irregular Network (TIN) interpolation surface raster from one or more vector layers.\n\n"
67 "The TIN method constructs a Delaunay triangulation network from sample features. Surfaces within the constructed triangles "
68 "are interpolated using either Linear or Clough-Tocher (cubic) methods. To do this, circumcircles around selected "
69 "sample points are created and their intersections are connected to a network of non overlapping and as compact "
70 "as possible triangles.\n\n"
71 "Input layers can supply sample values from feature attributes or feature Z coordinates. Features can be specified as discrete "
72 "points, structure lines, or breaklines.\n\n"
73 "Optionally, the algorithm can also output a vector line layer representing the triangulation network boundaries."
74 );
75}
76
77QgsTinInterpolationAlgorithm *QgsTinInterpolationAlgorithm::createInstance() const
78{
79 return new QgsTinInterpolationAlgorithm();
80}
81
82void QgsTinInterpolationAlgorithm::initAlgorithm( const QVariantMap & )
83{
84 auto dataParam = std::make_unique<QgsProcessingParameterInterpolationSource>( u"INTERPOLATION_DATA"_s, QObject::tr( "Input layer(s)" ) );
85 dataParam->setHelp( QObject::tr( "Vector layers to use for triangulation along with their attributes and source types." ) );
86 addParameter( dataParam.release() );
87
88 const QStringList methods = { QObject::tr( "Linear" ), QObject::tr( "Clough-Toucher (cubic)" ) };
89 auto methodParam = std::make_unique<QgsProcessingParameterEnum>( u"METHOD"_s, QObject::tr( "Interpolation method" ), methods, false, 0 );
90 methodParam->setHelp( QObject::tr( "Method used to interpolate values within constructed triangles." ) );
91 addParameter( methodParam.release() );
92
93 auto extentParam = std::make_unique<QgsProcessingParameterExtent>( u"EXTENT"_s, QObject::tr( "Extent" ), QVariant(), false );
94 extentParam->setHelp( QObject::tr( "Bounding box defining output raster extent." ) );
95 addParameter( extentParam.release() );
96
97 auto pixelSizeParam = std::make_unique<QgsProcessingParameterInterpolationPixelSize>( u"PIXEL_SIZE"_s, QObject::tr( "Output raster size" ), u"INTERPOLATION_DATA"_s, u"EXTENT"_s, 0.1 );
98 pixelSizeParam->setHelp( QObject::tr( "Pixel size in layer units used to calculate output grid dimensions." ) );
99 addParameter( pixelSizeParam.release() );
100
101 auto colsParam = std::make_unique<QgsProcessingParameterNumber>( u"COLUMNS"_s, QObject::tr( "Number of columns" ), Qgis::ProcessingNumberParameterType::Integer, QVariant(), true, 0, 10000000 );
102 colsParam->setFlags( colsParam->flags() | Qgis::ProcessingParameterFlag::Hidden );
103 addParameter( colsParam.release() );
104
105 auto rowsParam = std::make_unique<QgsProcessingParameterNumber>( u"ROWS"_s, QObject::tr( "Number of rows" ), Qgis::ProcessingNumberParameterType::Integer, QVariant(), true, 0, 10000000 );
106 rowsParam->setFlags( rowsParam->flags() | Qgis::ProcessingParameterFlag::Hidden );
107 addParameter( rowsParam.release() );
108
109 auto outputNodataParam = std::make_unique<QgsProcessingParameterNumber>( u"NODATA"_s, QObject::tr( "Output NoData value" ), Qgis::ProcessingNumberParameterType::Double, -9999.0 );
110 outputNodataParam->setHelp( QObject::tr( "The NODATA value to use in the output raster." ) );
111 outputNodataParam->setFlags( outputNodataParam->flags() | Qgis::ProcessingParameterFlag::Advanced );
112 addParameter( outputNodataParam.release() );
113
114 auto creationOptsParam = std::make_unique<QgsProcessingParameterString>( u"CREATION_OPTIONS"_s, QObject::tr( "Creation options" ), QVariant(), false, true );
115 creationOptsParam->setHelp( QObject::tr( "The raster creation options for the output raster. These options control things like colorimetry, compression, etc." ) );
116 creationOptsParam->setMetadata( QVariantMap( { { u"widget_wrapper"_s, QVariantMap( { { u"widget_type"_s, u"rasteroptions"_s } } ) } } ) );
117 creationOptsParam->setFlags( creationOptsParam->flags() | Qgis::ProcessingParameterFlag::Advanced );
118 addParameter( creationOptsParam.release() );
119
120 auto outputParam = std::make_unique<QgsProcessingParameterRasterDestination>( u"OUTPUT"_s, QObject::tr( "Interpolated" ) );
121 addParameter( outputParam.release() );
122
123 auto triangulationParam = std::make_unique<QgsProcessingParameterFeatureSink>( u"TRIANGULATION"_s, QObject::tr( "Triangulation" ), Qgis::ProcessingSourceType::VectorLine, QVariant(), true );
124 triangulationParam->setCreateByDefault( false );
125 triangulationParam->setHelp( QObject::tr( "Optional output vector layer to save triangulation network lines." ) );
126 addParameter( triangulationParam.release() );
127}
128
129QVariantMap QgsTinInterpolationAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
130{
131 const QString interpolationData = parameterAsString( parameters, u"INTERPOLATION_DATA"_s, context );
132 const int method = parameterAsEnum( parameters, u"METHOD"_s, context );
133 const QgsRectangle boundingBox = parameterAsExtent( parameters, u"EXTENT"_s, context );
134 const double pixelSize = parameterAsDouble( parameters, u"PIXEL_SIZE"_s, context );
135 const QString creationOptions = parameterAsString( parameters, u"CREATION_OPTIONS"_s, context ).trimmed();
136 const double outputNodata = parameterAsDouble( parameters, u"NODATA"_s, context );
137 const QString output = parameterAsOutputLayer( parameters, u"OUTPUT"_s, context );
138
139 int columns = parameterAsInt( parameters, u"COLUMNS"_s, context );
140 int rows = parameterAsInt( parameters, u"ROWS"_s, context );
141 if ( columns == 0 )
142 {
143 columns = std::max( static_cast<int>( std::ceil( boundingBox.width() / pixelSize ) ), 1 );
144 }
145 if ( rows == 0 )
146 {
147 rows = std::max( static_cast<int>( std::ceil( boundingBox.height() / pixelSize ) ), 1 );
148 }
149
150 if ( interpolationData.isEmpty() )
151 {
152 throw QgsProcessingException( QObject::tr( "At least one input layer must be specified." ) );
153 }
154
155 QList<QgsInterpolator::LayerData> layerDataList;
156 std::vector<std::unique_ptr<QgsFeatureSource>> sourceHolders;
158
159 const QStringList layerRows = interpolationData.split( "::|::"_L1, Qt::SkipEmptyParts );
160 for ( const QString &row : std::as_const( layerRows ) )
161 {
162 const QStringList tokens = row.split( "::~::"_L1 );
163 if ( tokens.size() < 4 )
164 {
165 continue;
166 }
167
169 std::unique_ptr<QgsProcessingFeatureSource> source( QgsProcessingUtils::variantToSource( tokens.at( 0 ), context ) );
170
171 if ( !source )
172 {
173 throw QgsProcessingException( QObject::tr( "Could not load source layer for input %1." ).arg( tokens.at( 0 ) ) );
174 }
175
176 data.source = source.get();
177 data.transformContext = context.transformContext();
178
179 if ( !layerCrs.isValid() )
180 {
181 layerCrs = source->sourceCrs();
182 }
183
184 data.valueSource = static_cast<Qgis::InterpolationValueSource>( tokens.at( 1 ).toInt() );
185
186 bool intOk = false;
187 data.interpolationAttribute = tokens.at( 2 ).toInt( &intOk );
188
190 {
191 if ( !intOk )
192 {
193 data.interpolationAttribute = source->fields().lookupField( tokens.at( 2 ) );
194 if ( data.interpolationAttribute < 0 )
195 {
196 throw QgsProcessingException( QObject::tr( "Field %1 does not exist in layer %2." ).arg( tokens.at( 2 ), source->sourceName() ) );
197 }
198 }
199 else if ( data.interpolationAttribute < 0 )
200 {
201 throw QgsProcessingException( QObject::tr( "Layer %1 is set to use a value attribute, but no attribute was set." ).arg( source->sourceName() ) );
202 }
203 else if ( data.interpolationAttribute >= source->fields().size() )
204 {
205 throw QgsProcessingException( QObject::tr( "Layer %1 is set to use an invalid attribute." ).arg( source->sourceName() ) );
206 }
207 }
208
209 data.sourceType = static_cast<Qgis::InterpolationSourceType>( tokens.at( 3 ).toInt() );
210
211 layerDataList.append( data );
212 sourceHolders.push_back( std::move( source ) );
213 }
214
215 const QgsTinInterpolator::TinInterpolation tinMethod = ( method == 0 ) ? QgsTinInterpolator::TinInterpolation::Linear : QgsTinInterpolator::TinInterpolation::CloughTocher;
216
217 QString triangulationDestinationId;
218 std::unique_ptr<QgsFeatureSink> triangulationSink(
219 parameterAsSink( parameters, u"TRIANGULATION"_s, context, triangulationDestinationId, QgsTinInterpolator::triangulationFields(), Qgis::WkbType::LineString, layerCrs )
220 );
221
222 QgsTinInterpolator interpolator( layerDataList, tinMethod, feedback );
223 if ( triangulationSink )
224 {
225 interpolator.setTriangulationSink( triangulationSink.get() );
226 }
227
228 QgsGridFileWriter writer( &interpolator, output, boundingBox, columns, rows );
229 if ( !creationOptions.isEmpty() )
230 {
231 writer.setCreationOptions( creationOptions.split( '|' ) );
232 }
233 writer.setNoDataValue( outputNodata );
234 writer.writeFile( feedback );
235
236 if ( triangulationSink )
237 {
238 triangulationSink->finalize();
239 if ( feedback )
240 {
241 feedback->featureSinkFinalized( u"TRIANGULATION"_s );
242 }
243 }
244
245 QVariantMap outputs;
246 outputs.insert( u"OUTPUT"_s, output );
247 if ( triangulationSink )
248 {
249 outputs.insert( u"TRIANGULATION"_s, triangulationDestinationId );
250 }
251 return outputs;
252}
@ VectorLine
Vector line layers.
Definition qgis.h:3751
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
@ LineString
LineString.
Definition qgis.h:297
@ 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
Represents a coordinate reference system (CRS).
bool isValid() const
Returns whether this CRS is correctly initialized and usable.
Handles interpolation to a grid and writes the results to a raster grid file.
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.
void featureSinkFinalized(const QString &output)
Reports that a feature sink has been finalized.
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.
Interpolation in a triangular irregular network.
static QgsFields triangulationFields()
Returns the fields output by features when saving the triangulation.
TinInterpolation
Indicates the type of interpolation to be performed.
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.