QGIS API Documentation 3.41.0-Master (45a0abf3bec)
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" ),
110 QObject::tr( "Distance" ), 1.0, QStringLiteral( "INPUT" ), false, 0 );
111 distance->setIsDynamic( true );
112 distance->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "DISTANCE" ), QObject::tr( "Distance" ), QgsPropertyDefinition::DoublePositive ) );
113 distance->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
114 addParameter( distance.release() );
115
116 std::unique_ptr< QgsProcessingParameterDistance> startOffset = std::make_unique< QgsProcessingParameterDistance >( QStringLiteral( "START_OFFSET" ),
117 QObject::tr( "Start offset" ), 0.0, QStringLiteral( "INPUT" ), false, 0 );
118 startOffset->setIsDynamic( true );
119 startOffset->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "START_OFFSET" ), QObject::tr( "Start offset" ), QgsPropertyDefinition::DoublePositive ) );
120 startOffset->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
121 addParameter( startOffset.release() );
122
123 std::unique_ptr< QgsProcessingParameterDistance> endOffset = std::make_unique< QgsProcessingParameterDistance >( QStringLiteral( "END_OFFSET" ),
124 QObject::tr( "End offset" ), 0.0, QStringLiteral( "INPUT" ), false, 0 );
125 endOffset->setIsDynamic( true );
126 endOffset->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "END_OFFSET" ), QObject::tr( "End offset" ), QgsPropertyDefinition::DoublePositive ) );
127 endOffset->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
128 addParameter( endOffset.release() );
129}
130
131QIcon QgsPointsAlongGeometryAlgorithm::icon() const
132{
133 return QgsApplication::getThemeIcon( QStringLiteral( "/algorithms/mAlgorithmExtractVertices.svg" ) );
134}
135
136QString QgsPointsAlongGeometryAlgorithm::svgIconPath() const
137{
138 return QgsApplication::iconPath( QStringLiteral( "/algorithms/mAlgorithmExtractVertices.svg" ) );
139}
140
141Qgis::ProcessingFeatureSourceFlags QgsPointsAlongGeometryAlgorithm::sourceFlags() const
142{
143 // skip geometry checks - this algorithm doesn't care about invalid geometries
145}
146
147QgsFeatureSink::SinkFlags QgsPointsAlongGeometryAlgorithm::sinkFlags() const
148{
150}
151
152bool QgsPointsAlongGeometryAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
153{
154 mDistance = parameterAsDouble( parameters, QStringLiteral( "DISTANCE" ), context );
155 mDynamicDistance = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "DISTANCE" ) );
156 if ( mDynamicDistance )
157 mDistanceProperty = parameters.value( QStringLiteral( "DISTANCE" ) ).value< QgsProperty >();
158
159 mStartOffset = parameterAsDouble( parameters, QStringLiteral( "START_OFFSET" ), context );
160 mDynamicStartOffset = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "START_OFFSET" ) );
161 if ( mDynamicStartOffset )
162 mStartOffsetProperty = parameters.value( QStringLiteral( "START_OFFSET" ) ).value< QgsProperty >();
163
164 mEndOffset = parameterAsDouble( parameters, QStringLiteral( "END_OFFSET" ), context );
165 mDynamicEndOffset = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "END_OFFSET" ) );
166 if ( mDynamicEndOffset )
167 mEndOffsetProperty = parameters.value( QStringLiteral( "END_OFFSET" ) ).value< QgsProperty >();
168
169 return true;
170}
171
172QgsFeatureList QgsPointsAlongGeometryAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
173{
174 QgsFeature f = feature;
175 if ( f.hasGeometry() )
176 {
177 const QgsGeometry geometry = f.geometry();
178
179 double distance = mDistance;
180 if ( mDynamicDistance )
181 distance = mDistanceProperty.valueAsDouble( context.expressionContext(), distance );
182 if ( distance <= 0 )
183 return QgsFeatureList();
184
185 double startOffset = mStartOffset;
186 if ( mDynamicStartOffset )
187 startOffset = mStartOffsetProperty.valueAsDouble( context.expressionContext(), startOffset );
188
189 double endOffset = mEndOffset;
190 if ( mDynamicEndOffset )
191 endOffset = mEndOffsetProperty.valueAsDouble( context.expressionContext(), endOffset );
192
193 const double totalLength = geometry.type() == Qgis::GeometryType::Polygon ? geometry.constGet()->perimeter()
194 : geometry.length() - endOffset;
195
196 double currentDistance = startOffset;
197 QgsFeatureList out;
198 out.reserve( static_cast< int >( std::ceil( ( totalLength - startOffset ) / distance ) ) );
199 while ( currentDistance <= totalLength )
200 {
201 const QgsGeometry point = geometry.interpolate( currentDistance );
202 const double angle = ( 180 / M_PI ) * geometry.interpolateAngle( currentDistance );
203 QgsFeature outputFeature;
204 outputFeature.setGeometry( point );
205 QgsAttributes outAttr = f.attributes();
206 outAttr << currentDistance << angle;
207 outputFeature.setAttributes( outAttr );
208 out.append( outputFeature );
209 currentDistance += distance;
210 if ( feedback->isCanceled() ) // better check here -- a ridiculously small distance might take forever
211 break;
212 }
213 return out;
214 }
215 else
216 {
217 QgsAttributes outAttr = f.attributes();
218 outAttr << QVariant() << QVariant();
219 f.setAttributes( outAttr );
220 return QgsFeatureList() << f;
221 }
222}
223
225
226
ProcessingSourceType
Processing data source types.
Definition qgis.h:3270
@ 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:3367
@ 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:3444
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