QGIS API Documentation 3.41.0-Master (af5edcb665c)
Loading...
Searching...
No Matches
qgsalgorithmpointsalonggeometry.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmpointsalonggeometry.cpp
3 ---------------------
4 begin : June 2019
5 copyright : (C) 2019 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
20#include "qgscurve.h"
21#include "qgsapplication.h"
22
24
25QString QgsPointsAlongGeometryAlgorithm::name() const
26{
27 return QStringLiteral( "pointsalonglines" );
28}
29
30QString QgsPointsAlongGeometryAlgorithm::displayName() const
31{
32 return QObject::tr( "Points along geometry" );
33}
34
35QStringList QgsPointsAlongGeometryAlgorithm::tags() const
36{
37 return QObject::tr( "create,interpolate,points,lines,regular,distance,by" ).split( ',' );
38}
39
40QString QgsPointsAlongGeometryAlgorithm::group() const
41{
42 return QObject::tr( "Vector geometry" );
43}
44
45QString QgsPointsAlongGeometryAlgorithm::groupId() const
46{
47 return QStringLiteral( "vectorgeometry" );
48}
49
50QString QgsPointsAlongGeometryAlgorithm::outputName() const
51{
52 return QObject::tr( "Interpolated points" );
53}
54
55QString QgsPointsAlongGeometryAlgorithm::shortHelpString() const
56{
57 return QObject::tr( "This algorithm creates a points layer, with points distributed along the lines of an "
58 "input vector layer. The distance between points (measured along the line) is defined as a parameter.\n\n"
59 "Start and end offset distances can be defined, so the first and last point will not fall exactly on the line's "
60 "first and last nodes. These start and end offsets are defined as distances, measured along the line from the first and last "
61 "nodes of the lines." );
62}
63
64QString QgsPointsAlongGeometryAlgorithm::shortDescription() const
65{
66 return QObject::tr( "Creates regularly spaced points along line features." );
67}
68
69Qgis::ProcessingAlgorithmDocumentationFlags QgsPointsAlongGeometryAlgorithm::documentationFlags() const
70{
72}
73
74QList<int> QgsPointsAlongGeometryAlgorithm::inputLayerTypes() const
75{
76 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorLine ) << static_cast<int>( Qgis::ProcessingSourceType::VectorPolygon );
77}
78
79Qgis::ProcessingSourceType QgsPointsAlongGeometryAlgorithm::outputLayerType() const
80{
82}
83
84Qgis::WkbType QgsPointsAlongGeometryAlgorithm::outputWkbType( Qgis::WkbType inputType ) const
85{
87 if ( QgsWkbTypes::hasZ( inputType ) )
88 out = QgsWkbTypes::addZ( out );
89 if ( QgsWkbTypes::hasM( inputType ) )
90 out = QgsWkbTypes::addM( out );
91 return out;
92}
93
94QgsFields QgsPointsAlongGeometryAlgorithm::outputFields( const QgsFields &inputFields ) const
95{
96 QgsFields output = inputFields;
97 output.append( QgsField( QStringLiteral( "distance" ), QMetaType::Type::Double ) );
98 output.append( QgsField( QStringLiteral( "angle" ), QMetaType::Type::Double ) );
99 return output;
100}
101
102QgsPointsAlongGeometryAlgorithm *QgsPointsAlongGeometryAlgorithm::createInstance() const
103{
104 return new QgsPointsAlongGeometryAlgorithm();
105}
106
107void QgsPointsAlongGeometryAlgorithm::initParameters( const QVariantMap & )
108{
109 std::unique_ptr<QgsProcessingParameterDistance> distance = std::make_unique<QgsProcessingParameterDistance>( QStringLiteral( "DISTANCE" ), QObject::tr( "Distance" ), 1.0, QStringLiteral( "INPUT" ), false, 0 );
110 distance->setIsDynamic( true );
111 distance->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "DISTANCE" ), QObject::tr( "Distance" ), QgsPropertyDefinition::DoublePositive ) );
112 distance->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
113 addParameter( distance.release() );
114
115 std::unique_ptr<QgsProcessingParameterDistance> startOffset = std::make_unique<QgsProcessingParameterDistance>( QStringLiteral( "START_OFFSET" ), QObject::tr( "Start offset" ), 0.0, QStringLiteral( "INPUT" ), false, 0 );
116 startOffset->setIsDynamic( true );
117 startOffset->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "START_OFFSET" ), QObject::tr( "Start offset" ), QgsPropertyDefinition::DoublePositive ) );
118 startOffset->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
119 addParameter( startOffset.release() );
120
121 std::unique_ptr<QgsProcessingParameterDistance> endOffset = std::make_unique<QgsProcessingParameterDistance>( QStringLiteral( "END_OFFSET" ), QObject::tr( "End offset" ), 0.0, QStringLiteral( "INPUT" ), false, 0 );
122 endOffset->setIsDynamic( true );
123 endOffset->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "END_OFFSET" ), QObject::tr( "End offset" ), QgsPropertyDefinition::DoublePositive ) );
124 endOffset->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
125 addParameter( endOffset.release() );
126}
127
128QIcon QgsPointsAlongGeometryAlgorithm::icon() const
129{
130 return QgsApplication::getThemeIcon( QStringLiteral( "/algorithms/mAlgorithmExtractVertices.svg" ) );
131}
132
133QString QgsPointsAlongGeometryAlgorithm::svgIconPath() const
134{
135 return QgsApplication::iconPath( QStringLiteral( "/algorithms/mAlgorithmExtractVertices.svg" ) );
136}
137
138Qgis::ProcessingFeatureSourceFlags QgsPointsAlongGeometryAlgorithm::sourceFlags() const
139{
140 // skip geometry checks - this algorithm doesn't care about invalid geometries
142}
143
144QgsFeatureSink::SinkFlags QgsPointsAlongGeometryAlgorithm::sinkFlags() const
145{
147}
148
149bool QgsPointsAlongGeometryAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
150{
151 mDistance = parameterAsDouble( parameters, QStringLiteral( "DISTANCE" ), context );
152 mDynamicDistance = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "DISTANCE" ) );
153 if ( mDynamicDistance )
154 mDistanceProperty = parameters.value( QStringLiteral( "DISTANCE" ) ).value<QgsProperty>();
155
156 mStartOffset = parameterAsDouble( parameters, QStringLiteral( "START_OFFSET" ), context );
157 mDynamicStartOffset = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "START_OFFSET" ) );
158 if ( mDynamicStartOffset )
159 mStartOffsetProperty = parameters.value( QStringLiteral( "START_OFFSET" ) ).value<QgsProperty>();
160
161 mEndOffset = parameterAsDouble( parameters, QStringLiteral( "END_OFFSET" ), context );
162 mDynamicEndOffset = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "END_OFFSET" ) );
163 if ( mDynamicEndOffset )
164 mEndOffsetProperty = parameters.value( QStringLiteral( "END_OFFSET" ) ).value<QgsProperty>();
165
166 return true;
167}
168
169QgsFeatureList QgsPointsAlongGeometryAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
170{
171 QgsFeature f = feature;
172 if ( f.hasGeometry() )
173 {
174 const QgsGeometry geometry = f.geometry();
175
176 double distance = mDistance;
177 if ( mDynamicDistance )
178 distance = mDistanceProperty.valueAsDouble( context.expressionContext(), distance );
179 if ( distance <= 0 )
180 return QgsFeatureList();
181
182 double startOffset = mStartOffset;
183 if ( mDynamicStartOffset )
184 startOffset = mStartOffsetProperty.valueAsDouble( context.expressionContext(), startOffset );
185
186 double endOffset = mEndOffset;
187 if ( mDynamicEndOffset )
188 endOffset = mEndOffsetProperty.valueAsDouble( context.expressionContext(), endOffset );
189
190 const double totalLength = geometry.type() == Qgis::GeometryType::Polygon ? geometry.constGet()->perimeter()
191 : geometry.length() - endOffset;
192
193 double currentDistance = startOffset;
194 QgsFeatureList out;
195 out.reserve( static_cast<int>( std::ceil( ( totalLength - startOffset ) / distance ) ) );
196 while ( currentDistance <= totalLength )
197 {
198 const QgsGeometry point = geometry.interpolate( currentDistance );
199 const double angle = ( 180 / M_PI ) * geometry.interpolateAngle( currentDistance );
200 QgsFeature outputFeature;
201 outputFeature.setGeometry( point );
202 QgsAttributes outAttr = f.attributes();
203 outAttr << currentDistance << angle;
204 outputFeature.setAttributes( outAttr );
205 out.append( outputFeature );
206 currentDistance += distance;
207 if ( feedback->isCanceled() ) // better check here -- a ridiculously small distance might take forever
208 break;
209 }
210 return out;
211 }
212 else
213 {
214 QgsAttributes outAttr = f.attributes();
215 outAttr << QVariant() << QVariant();
216 f.setAttributes( outAttr );
217 return QgsFeatureList() << f;
218 }
219}
220
ProcessingSourceType
Processing data source types.
Definition qgis.h:3333
@ VectorPoint
Vector point layers.
@ VectorPolygon
Vector polygon layers.
@ VectorLine
Vector line layers.
@ Polygon
Polygons.
@ RegeneratesPrimaryKey
Algorithm always drops any existing primary keys or FID values and regenerates them in outputs.
QFlags< ProcessingAlgorithmDocumentationFlag > ProcessingAlgorithmDocumentationFlags
Flags describing algorithm behavior for documentation purposes.
Definition qgis.h:3430
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:256
QFlags< ProcessingFeatureSourceFlag > ProcessingFeatureSourceFlags
Flags which control how QgsProcessingFeatureSource fetches features.
Definition qgis.h:3507
virtual double perimeter() const
Returns the planar, 2-dimensional perimeter of the geometry.
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
static QString iconPath(const QString &iconFile)
Returns path to the desired icon file.
A vector of attributes.
QFlags< SinkFlag > SinkFlags
@ RegeneratePrimaryKey
This flag indicates, that a primary key field cannot be guaranteed to be unique and the sink should i...
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
QgsAttributes attributes
Definition qgsfeature.h:67
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
QgsGeometry geometry
Definition qgsfeature.h:69
bool hasGeometry() const
Returns true if the feature has an associated geometry.
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition qgsfeedback.h:53
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:53
Container of fields for a vector layer.
Definition qgsfields.h:46
bool append(const QgsField &field, Qgis::FieldOrigin origin=Qgis::FieldOrigin::Provider, int originIndex=-1)
Appends a field.
Definition qgsfields.cpp:70
A geometry is the spatial representation of a feature.
double length() const
Returns the planar, 2-dimensional length of geometry.
QgsGeometry interpolate(double distance) const
Returns an interpolated point on the geometry at the specified distance.
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
Qgis::GeometryType type
double interpolateAngle(double distance) const
Returns the angle parallel to the linestring or polygon boundary at the specified distance along the ...
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:45
@ DoublePositive
Positive double value (including 0)
Definition qgsproperty.h:56
A store for object properties.
double valueAsDouble(const QgsExpressionContext &context, double defaultValue=0.0, bool *ok=nullptr) const
Calculates the current value of the property and interprets it as a double.
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 bool hasZ(Qgis::WkbType type)
Tests whether a WKB type contains the z-dimension.
static bool hasM(Qgis::WkbType type)
Tests whether a WKB type contains m values.
double ANALYSIS_EXPORT angle(QgsPoint *p1, QgsPoint *p2, QgsPoint *p3, QgsPoint *p4)
Calculates the angle between two segments (in 2 dimension, z-values are ignored)
QList< QgsFeature > QgsFeatureList