QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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 = qgis::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 
QgsGeometry interpolate(double distance) const
Returns an interpolated point on the geometry at the specified distance.
Base class for providing feedback from a processing algorithm.
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:571
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:106
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:55
Positive double value (including 0)
Definition: qgsproperty.h:58
static bool hasZ(Type type)
Tests whether a WKB type contains the z-dimension.
Definition: qgswkbtypes.h:906
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:68
static Type addM(Type type)
Adds the m dimension to a WKB type and returns the new type.
Definition: qgswkbtypes.h:1027
static Type addZ(Type type)
Adds the z dimension to a WKB type and returns the new type.
Definition: qgswkbtypes.h:1002
Vector polygon layers.
Definition: qgsprocessing.h:50
A store for object properties.
Definition: qgsproperty.h:229
QgsExpressionContext & expressionContext()
Returns the expression context.
Definition for a property.
Definition: qgsproperty.h:46
Flag
Flags controlling how QgsProcessingFeatureSource fetches features.
Vector point layers.
Definition: qgsprocessing.h:48
Vector line layers.
Definition: qgsprocessing.h:49
void setGeometry(const QgsGeometry &geometry)
Set the feature&#39;s geometry.
Definition: qgsfeature.cpp:137
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:197
SourceType
Data source types enum.
Definition: qgsprocessing.h:44
static bool hasM(Type type)
Tests whether a WKB type contains m values.
Definition: qgswkbtypes.h:956
QgsGeometry geometry
Definition: qgsfeature.h:67
Contains information about the context in which a processing algorithm is executed.
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...