28using namespace Qt::StringLiterals;
31QgsIdwInterpolationAlgorithm::QgsIdwInterpolationAlgorithm() =
default;
33QString QgsIdwInterpolationAlgorithm::name()
const
35 return u
"idwinterpolation"_s;
38QString QgsIdwInterpolationAlgorithm::displayName()
const
40 return QObject::tr(
"IDW interpolation" );
43QStringList QgsIdwInterpolationAlgorithm::tags()
const
45 return QObject::tr(
"idw,inverse,distance,weighted,interpolation,surface,grid,breaklines,breaks,structures" ).split(
',' );
48QString QgsIdwInterpolationAlgorithm::group()
const
50 return QObject::tr(
"Interpolation" );
53QString QgsIdwInterpolationAlgorithm::groupId()
const
55 return u
"interpolation"_s;
58QString QgsIdwInterpolationAlgorithm::shortDescription()
const
60 return QObject::tr(
"Generates an Inverse Distance Weighted (IDW) interpolation from vector layers." );
63QString QgsIdwInterpolationAlgorithm::shortHelpString()
const
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."
74QgsIdwInterpolationAlgorithm *QgsIdwInterpolationAlgorithm::createInstance()
const
76 return new QgsIdwInterpolationAlgorithm();
79void QgsIdwInterpolationAlgorithm::initAlgorithm(
const QVariantMap & )
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() );
87 distanceParam->setHelp( QObject::tr(
"Distance coefficient determines how influence decreases with distance." ) );
88 addParameter( distanceParam.release() );
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() );
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() );
100 addParameter( colsParam.release() );
104 addParameter( rowsParam.release() );
107 outputNodataParam->setHelp( QObject::tr(
"The NODATA value to use in the output raster." ) );
109 addParameter( outputNodataParam.release() );
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 } } ) } } ) );
115 addParameter( creationOptsParam.release() );
117 auto outputParam = std::make_unique<QgsProcessingParameterRasterDestination>( u
"OUTPUT"_s, QObject::tr(
"Interpolated" ) );
118 addParameter( outputParam.release() );
123 const QString interpolationData = parameterAsString( parameters, u
"INTERPOLATION_DATA"_s, context );
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 );
132 int columns = parameterAsInt( parameters, u
"COLUMNS"_s, context );
133 int rows = parameterAsInt( parameters, u
"ROWS"_s, context );
136 columns = std::max(
static_cast<int>( std::ceil( boundingBox.
width() / pixelSize ) ), 1 );
140 rows = std::max(
static_cast<int>( std::ceil( boundingBox.
height() / pixelSize ) ), 1 );
143 if ( interpolationData.isEmpty() )
148 QList<QgsInterpolator::LayerData> layerDataList;
149 std::vector<std::unique_ptr<QgsFeatureSource>> sourceHolders;
151 const QStringList layerRows = interpolationData.split(
"::|::"_L1, Qt::SkipEmptyParts );
152 for (
const QString &row : std::as_const( layerRows ) )
154 const QStringList tokens = row.split(
"::~::"_L1 );
155 if ( tokens.size() < 4 )
165 throw QgsProcessingException( QObject::tr(
"Could not load source layer for input %1." ).arg( tokens.at( 0 ) ) );
168 data.
source = source.get();
183 throw QgsProcessingException( QObject::tr(
"Field %1 does not exist in layer %2." ).arg( tokens.at( 2 ), source->sourceName() ) );
188 throw QgsProcessingException( QObject::tr(
"Layer %1 is set to use a value attribute, but no attribute was set." ).arg( source->sourceName() ) );
192 throw QgsProcessingException( QObject::tr(
"Layer %1 is set to use an invalid attribute." ).arg( source->sourceName() ) );
198 layerDataList.append( data );
199 sourceHolders.push_back( std::move( source ) );
203 interpolator.setDistanceCoefficient( coefficient );
206 if ( !creationOptions.isEmpty() )
208 writer.setCreationOptions( creationOptions.split(
'|' ) );
210 writer.setNoDataValue( outputNodata );
212 writer.writeFile( feedback );
215 outputs.insert( u
"OUTPUT"_s, output );
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.
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.