QGIS API Documentation  3.20.0-Odense (decaadbb31)
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 #include "qgsgeometrycollection.h"
20 #include "qgscurve.h"
21 
23 
24 QString QgsInterpolatePointAlgorithm::name() const
25 {
26  return QStringLiteral( "interpolatepoint" );
27 }
28 
29 QString QgsInterpolatePointAlgorithm::displayName() const
30 {
31  return QObject::tr( "Interpolate point on line" );
32 }
33 
34 QStringList QgsInterpolatePointAlgorithm::tags() const
35 {
36  return QObject::tr( "linestring,reference,referencing,distance,interpolate" ).split( ',' );
37 }
38 
39 QString QgsInterpolatePointAlgorithm::group() const
40 {
41  return QObject::tr( "Vector geometry" );
42 }
43 
44 QString QgsInterpolatePointAlgorithm::groupId() const
45 {
46  return QStringLiteral( "vectorgeometry" );
47 }
48 
49 QString QgsInterpolatePointAlgorithm::outputName() const
50 {
51  return QObject::tr( "Interpolated points" );
52 }
53 
54 QString QgsInterpolatePointAlgorithm::shortHelpString() const
55 {
56  return QObject::tr( "This algorithm creates a point geometry interpolated at a set distance along line or curve geometries.\n\n"
57  "Z and M values are linearly interpolated from existing values.\n\n"
58  "If a multipart geometry is encountered, only the first part is considered when "
59  "interpolating the point.\n\n"
60  "If the specified distance is greater than the curve's length, the resultant feature will have a null geometry." );
61 }
62 
63 QString QgsInterpolatePointAlgorithm::shortDescription() const
64 {
65  return QObject::tr( "Interpolates a point along lines at a set distance." );
66 }
67 
68 QList<int> QgsInterpolatePointAlgorithm::inputLayerTypes() const
69 {
71 }
72 
73 QgsProcessing::SourceType QgsInterpolatePointAlgorithm::outputLayerType() const
74 {
76 }
77 
78 QgsWkbTypes::Type QgsInterpolatePointAlgorithm::outputWkbType( QgsWkbTypes::Type inputType ) const
79 {
81  if ( QgsWkbTypes::hasZ( inputType ) )
82  out = QgsWkbTypes::addZ( out );
83  if ( QgsWkbTypes::hasM( inputType ) )
84  out = QgsWkbTypes::addM( out );
85  return out;
86 }
87 
88 QgsInterpolatePointAlgorithm *QgsInterpolatePointAlgorithm::createInstance() const
89 {
90  return new QgsInterpolatePointAlgorithm();
91 }
92 
93 void QgsInterpolatePointAlgorithm::initParameters( const QVariantMap & )
94 {
95  std::unique_ptr< QgsProcessingParameterDistance> distance = std::make_unique< QgsProcessingParameterDistance >( QStringLiteral( "DISTANCE" ),
96  QObject::tr( "Distance" ), 0.0, QStringLiteral( "INPUT" ), false, 0 );
97  distance->setIsDynamic( true );
98  distance->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "DISTANCE" ), QObject::tr( "Distance" ), QgsPropertyDefinition::DoublePositive ) );
99  distance->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
100  addParameter( distance.release() );
101 }
102 
103 QgsProcessingFeatureSource::Flag QgsInterpolatePointAlgorithm::sourceFlags() const
104 {
105  // skip geometry checks - this algorithm doesn't care about invalid geometries
107 }
108 
109 bool QgsInterpolatePointAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
110 {
111  mDistance = parameterAsDouble( parameters, QStringLiteral( "DISTANCE" ), context );
112  mDynamicDistance = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "DISTANCE" ) );
113  if ( mDynamicDistance )
114  mDistanceProperty = parameters.value( QStringLiteral( "DISTANCE" ) ).value< QgsProperty >();
115 
116  return true;
117 }
118 
119 QgsFeatureList QgsInterpolatePointAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
120 {
121  QgsFeature f = feature;
122  if ( f.hasGeometry() )
123  {
124  const QgsGeometry geometry = f.geometry();
125  double distance = mDistance;
126  if ( mDynamicDistance )
127  distance = mDistanceProperty.valueAsDouble( context.expressionContext(), distance );
128 
129  f.setGeometry( geometry.interpolate( distance ) );
130  }
131  return QgsFeatureList() << f;
132 }
133 
135 
136 
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:56
QgsGeometry geometry
Definition: qgsfeature.h:67
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:205
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
Definition: qgsfeature.cpp:145
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
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.
Flag
Flags controlling how QgsProcessingFeatureSource fetches features.
@ FlagSkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
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...
SourceType
Data source types enum.
Definition: qgsprocessing.h:46
@ TypeVectorLine
Vector line layers.
Definition: qgsprocessing.h:50
@ TypeVectorPolygon
Vector polygon layers.
Definition: qgsprocessing.h:51
@ TypeVectorPoint
Vector point layers.
Definition: qgsprocessing.h:49
Definition for a property.
Definition: qgsproperty.h:48
@ DoublePositive
Positive double value (including 0)
Definition: qgsproperty.h:59
A store for object properties.
Definition: qgsproperty.h:232
static bool hasM(Type type) SIP_HOLDGIL
Tests whether a WKB type contains m values.
Definition: qgswkbtypes.h:1100
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:70
static Type addZ(Type type) SIP_HOLDGIL
Adds the z dimension to a WKB type and returns the new type.
Definition: qgswkbtypes.h:1146
static bool hasZ(Type type) SIP_HOLDGIL
Tests whether a WKB type contains the z-dimension.
Definition: qgswkbtypes.h:1050
static Type addM(Type type) SIP_HOLDGIL
Adds the m dimension to a WKB type and returns the new type.
Definition: qgswkbtypes.h:1171
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:736