28using namespace Qt::StringLiterals;
31QgsTinInterpolationAlgorithm::QgsTinInterpolationAlgorithm() =
default;
33QString QgsTinInterpolationAlgorithm::name()
const
35 return u
"tininterpolation"_s;
38QString QgsTinInterpolationAlgorithm::displayName()
const
40 return QObject::tr(
"TIN interpolation" );
43QStringList QgsTinInterpolationAlgorithm::tags()
const
45 return QObject::tr(
"tin,triangulated,irregular,network,delaunay,interpolation,surface,breaklines,breaks,structures" ).split(
',' );
48QString QgsTinInterpolationAlgorithm::group()
const
50 return QObject::tr(
"Interpolation" );
53QString QgsTinInterpolationAlgorithm::groupId()
const
55 return u
"interpolation"_s;
58QString QgsTinInterpolationAlgorithm::shortDescription()
const
60 return QObject::tr(
"Generates a Triangulated Irregular Network (TIN) interpolation from vector layers." );
63QString QgsTinInterpolationAlgorithm::shortHelpString()
const
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."
77QgsTinInterpolationAlgorithm *QgsTinInterpolationAlgorithm::createInstance()
const
79 return new QgsTinInterpolationAlgorithm();
82void QgsTinInterpolationAlgorithm::initAlgorithm(
const QVariantMap & )
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() );
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() );
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() );
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() );
103 addParameter( colsParam.release() );
107 addParameter( rowsParam.release() );
110 outputNodataParam->setHelp( QObject::tr(
"The NODATA value to use in the output raster." ) );
112 addParameter( outputNodataParam.release() );
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 } } ) } } ) );
118 addParameter( creationOptsParam.release() );
120 auto outputParam = std::make_unique<QgsProcessingParameterRasterDestination>( u
"OUTPUT"_s, QObject::tr(
"Interpolated" ) );
121 addParameter( outputParam.release() );
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() );
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 );
139 int columns = parameterAsInt( parameters, u
"COLUMNS"_s, context );
140 int rows = parameterAsInt( parameters, u
"ROWS"_s, context );
143 columns = std::max(
static_cast<int>( std::ceil( boundingBox.
width() / pixelSize ) ), 1 );
147 rows = std::max(
static_cast<int>( std::ceil( boundingBox.
height() / pixelSize ) ), 1 );
150 if ( interpolationData.isEmpty() )
155 QList<QgsInterpolator::LayerData> layerDataList;
156 std::vector<std::unique_ptr<QgsFeatureSource>> sourceHolders;
159 const QStringList layerRows = interpolationData.split(
"::|::"_L1, Qt::SkipEmptyParts );
160 for (
const QString &row : std::as_const( layerRows ) )
162 const QStringList tokens = row.split(
"::~::"_L1 );
163 if ( tokens.size() < 4 )
173 throw QgsProcessingException( QObject::tr(
"Could not load source layer for input %1." ).arg( tokens.at( 0 ) ) );
176 data.
source = source.get();
181 layerCrs = source->sourceCrs();
196 throw QgsProcessingException( QObject::tr(
"Field %1 does not exist in layer %2." ).arg( tokens.at( 2 ), source->sourceName() ) );
201 throw QgsProcessingException( QObject::tr(
"Layer %1 is set to use a value attribute, but no attribute was set." ).arg( source->sourceName() ) );
205 throw QgsProcessingException( QObject::tr(
"Layer %1 is set to use an invalid attribute." ).arg( source->sourceName() ) );
211 layerDataList.append( data );
212 sourceHolders.push_back( std::move( source ) );
217 QString triangulationDestinationId;
218 std::unique_ptr<QgsFeatureSink> triangulationSink(
223 if ( triangulationSink )
225 interpolator.setTriangulationSink( triangulationSink.get() );
229 if ( !creationOptions.isEmpty() )
231 writer.setCreationOptions( creationOptions.split(
'|' ) );
233 writer.setNoDataValue( outputNodata );
234 writer.writeFile( feedback );
236 if ( triangulationSink )
238 triangulationSink->finalize();
246 outputs.insert( u
"OUTPUT"_s, output );
247 if ( triangulationSink )
249 outputs.insert( u
"TRIANGULATION"_s, triangulationDestinationId );
@ VectorLine
Vector line layers.
InterpolationSourceType
Interpolation source types.
InterpolationValueSource
Source for interpolated values from features.
@ Attribute
Take value from feature's attribute.
@ Hidden
Parameter is hidden and should not be shown to users.
@ Advanced
Parameter is an advanced parameter which should be hidden from users by default.
@ Double
Double/float values.
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.
@ Linear
Linear interpolation.
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.