QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 
QgsProperty
A store for object properties.
Definition: qgsproperty.h:231
QgsWkbTypes::Point
@ Point
Definition: qgswkbtypes.h:71
QgsProcessingParameters::isDynamic
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: qgsprocessingparameters.cpp:111
QgsProcessingFeedback
Definition: qgsprocessingfeedback.h:37
QgsGeometry::interpolate
QgsGeometry interpolate(double distance) const
Returns an interpolated point on the geometry at the specified distance.
Definition: qgsgeometry.cpp:2272
QgsProcessing::TypeVectorPolygon
@ TypeVectorPolygon
Vector polygon layers.
Definition: qgsprocessing.h:50
QgsFeature::geometry
QgsGeometry geometry
Definition: qgsfeature.h:71
QgsProcessing::TypeVectorLine
@ TypeVectorLine
Vector line layers.
Definition: qgsprocessing.h:49
QgsProcessingFeatureSource::FlagSkipGeometryValidityChecks
@ FlagSkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Definition: qgsprocessingutils.h:475
QgsWkbTypes::Type
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:68
QgsWkbTypes::hasZ
static bool hasZ(Type type)
Tests whether a WKB type contains the z-dimension.
Definition: qgswkbtypes.h:1042
QgsWkbTypes::addM
static Type addM(Type type)
Adds the m dimension to a WKB type and returns the new type.
Definition: qgswkbtypes.h:1163
QgsProcessing::TypeVectorPoint
@ TypeVectorPoint
Vector point layers.
Definition: qgsprocessing.h:48
QgsFeature::setGeometry
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
Definition: qgsfeature.cpp:137
QgsProcessingContext
Definition: qgsprocessingcontext.h:43
QgsFeatureList
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:572
QgsPropertyDefinition
Definition for a property.
Definition: qgsproperty.h:47
QgsWkbTypes::addZ
static Type addZ(Type type)
Adds the z dimension to a WKB type and returns the new type.
Definition: qgswkbtypes.h:1138
QgsWkbTypes::hasM
static bool hasM(Type type)
Tests whether a WKB type contains m values.
Definition: qgswkbtypes.h:1092
QgsProcessingFeatureSource::Flag
Flag
Flags controlling how QgsProcessingFeatureSource fetches features.
Definition: qgsprocessingutils.h:473
qgscurve.h
QgsGeometry
Definition: qgsgeometry.h:122
QgsFeature::hasGeometry
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:197
QgsProcessingContext::expressionContext
QgsExpressionContext & expressionContext()
Returns the expression context.
Definition: qgsprocessingcontext.h:119
qgsalgorithminterpolatepoint.h
qgsgeometrycollection.h
QgsFeature
Definition: qgsfeature.h:55
QgsProcessing::SourceType
SourceType
Data source types enum.
Definition: qgsprocessing.h:44
QgsPropertyDefinition::DoublePositive
@ DoublePositive
Positive double value (including 0)
Definition: qgsproperty.h:59