QGIS API Documentation 3.99.0-Master (357b655ed83)
Loading...
Searching...
No Matches
qgsalgorithminterpolatepoint.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithminterpolatepoint.cpp
3 ---------------------
4 begin : August 2018
5 copyright : (C) 2018 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 "qgscurve.h"
22
23#include <QString>
24
25using namespace Qt::StringLiterals;
26
28
29QString QgsInterpolatePointAlgorithm::name() const
30{
31 return u"interpolatepoint"_s;
32}
33
34QString QgsInterpolatePointAlgorithm::displayName() const
35{
36 return QObject::tr( "Interpolate point on line" );
37}
38
39QStringList QgsInterpolatePointAlgorithm::tags() const
40{
41 return QObject::tr( "linestring,reference,referencing,distance,interpolate" ).split( ',' );
42}
43
44QString QgsInterpolatePointAlgorithm::group() const
45{
46 return QObject::tr( "Vector geometry" );
47}
48
49QString QgsInterpolatePointAlgorithm::groupId() const
50{
51 return u"vectorgeometry"_s;
52}
53
54QString QgsInterpolatePointAlgorithm::outputName() const
55{
56 return QObject::tr( "Interpolated points" );
57}
58
59QString QgsInterpolatePointAlgorithm::shortHelpString() const
60{
61 return QObject::tr( "This algorithm creates a point geometry interpolated at a set distance along line or curve geometries.\n\n"
62 "Z and M values are linearly interpolated from existing values.\n\n"
63 "If a multipart geometry is encountered, only the first part is considered when "
64 "interpolating the point.\n\n"
65 "If the specified distance is greater than the curve's length, the resultant feature will have a null geometry." );
66}
67
68QString QgsInterpolatePointAlgorithm::shortDescription() const
69{
70 return QObject::tr( "Interpolates a point along lines at a set distance." );
71}
72
73QList<int> QgsInterpolatePointAlgorithm::inputLayerTypes() const
74{
75 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorLine ) << static_cast<int>( Qgis::ProcessingSourceType::VectorPolygon );
76}
77
78Qgis::ProcessingSourceType QgsInterpolatePointAlgorithm::outputLayerType() const
79{
81}
82
83Qgis::WkbType QgsInterpolatePointAlgorithm::outputWkbType( Qgis::WkbType inputType ) const
84{
86 if ( QgsWkbTypes::hasZ( inputType ) )
87 out = QgsWkbTypes::addZ( out );
88 if ( QgsWkbTypes::hasM( inputType ) )
89 out = QgsWkbTypes::addM( out );
90 return out;
91}
92
93QgsInterpolatePointAlgorithm *QgsInterpolatePointAlgorithm::createInstance() const
94{
95 return new QgsInterpolatePointAlgorithm();
96}
97
98void QgsInterpolatePointAlgorithm::initParameters( const QVariantMap & )
99{
100 auto distance = std::make_unique<QgsProcessingParameterDistance>( u"DISTANCE"_s, QObject::tr( "Distance" ), 0.0, u"INPUT"_s, false, 0 );
101 distance->setIsDynamic( true );
102 distance->setDynamicPropertyDefinition( QgsPropertyDefinition( u"DISTANCE"_s, QObject::tr( "Distance" ), QgsPropertyDefinition::DoublePositive ) );
103 distance->setDynamicLayerParameterName( u"INPUT"_s );
104 addParameter( distance.release() );
105}
106
107Qgis::ProcessingFeatureSourceFlags QgsInterpolatePointAlgorithm::sourceFlags() const
108{
109 // skip geometry checks - this algorithm doesn't care about invalid geometries
111}
112
113bool QgsInterpolatePointAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
114{
115 mDistance = parameterAsDouble( parameters, u"DISTANCE"_s, context );
116 mDynamicDistance = QgsProcessingParameters::isDynamic( parameters, u"DISTANCE"_s );
117 if ( mDynamicDistance )
118 mDistanceProperty = parameters.value( u"DISTANCE"_s ).value<QgsProperty>();
119
120 return true;
121}
122
123QgsFeatureList QgsInterpolatePointAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
124{
125 QgsFeature f = feature;
126 if ( f.hasGeometry() )
127 {
128 const QgsGeometry geometry = f.geometry();
129 double distance = mDistance;
130 if ( mDynamicDistance )
131 distance = mDistanceProperty.valueAsDouble( context.expressionContext(), distance );
132
133 f.setGeometry( geometry.interpolate( distance ) );
134 }
135 return QgsFeatureList() << f;
136}
137
ProcessingSourceType
Processing data source types.
Definition qgis.h:3602
@ VectorPoint
Vector point layers.
Definition qgis.h:3605
@ VectorPolygon
Vector polygon layers.
Definition qgis.h:3607
@ VectorLine
Vector line layers.
Definition qgis.h:3606
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Definition qgis.h:3782
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:280
@ Point
Point.
Definition qgis.h:282
QFlags< ProcessingFeatureSourceFlag > ProcessingFeatureSourceFlags
Flags which control how QgsProcessingFeatureSource fetches features.
Definition qgis.h:3793
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:60
QgsGeometry geometry
Definition qgsfeature.h:71
bool hasGeometry() const
Returns true if the feature has an associated geometry.
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
A geometry is the spatial representation of a feature.
QgsGeometry interpolate(double distance) const
Returns an interpolated point on the geometry at the specified distance.
Contains information about the context in which a processing algorithm is executed.
QgsExpressionContext & expressionContext()
Returns the expression context.
Base class for providing feedback from a processing algorithm.
static bool isDynamic(const QVariantMap &parameters, const QString &name)
Returns true if the parameter with matching name is a dynamic parameter, and must be evaluated once f...
Definition for a property.
Definition qgsproperty.h:47
@ DoublePositive
Positive double value (including 0).
Definition qgsproperty.h:58
A store for object properties.
static Qgis::WkbType addM(Qgis::WkbType type)
Adds the m dimension to a WKB type and returns the new type.
static Qgis::WkbType addZ(Qgis::WkbType type)
Adds the z dimension to a WKB type and returns the new type.
static Q_INVOKABLE bool hasZ(Qgis::WkbType type)
Tests whether a WKB type contains the z-dimension.
static Q_INVOKABLE bool hasM(Qgis::WkbType type)
Tests whether a WKB type contains m values.
QList< QgsFeature > QgsFeatureList