QGIS API Documentation 3.99.0-Master (09f76ad7019)
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
19
20#include "qgsapplication.h"
21#include "qgscurve.h"
23
24#include <QString>
25
26using namespace Qt::StringLiterals;
27
29
30QString QgsPointsAlongGeometryAlgorithm::name() const
31{
32 return u"pointsalonglines"_s;
33}
34
35QString QgsPointsAlongGeometryAlgorithm::displayName() const
36{
37 return QObject::tr( "Points along geometry" );
38}
39
40QStringList QgsPointsAlongGeometryAlgorithm::tags() const
41{
42 return QObject::tr( "create,interpolate,points,lines,regular,distance,by" ).split( ',' );
43}
44
45QString QgsPointsAlongGeometryAlgorithm::group() const
46{
47 return QObject::tr( "Vector geometry" );
48}
49
50QString QgsPointsAlongGeometryAlgorithm::groupId() const
51{
52 return u"vectorgeometry"_s;
53}
54
55QString QgsPointsAlongGeometryAlgorithm::outputName() const
56{
57 return QObject::tr( "Interpolated points" );
58}
59
60QString QgsPointsAlongGeometryAlgorithm::shortHelpString() const
61{
62 return QObject::tr( "This algorithm creates a points layer, with points distributed along the lines of an "
63 "input vector layer. The distance between points (measured along the line) is defined as a parameter.\n\n"
64 "Start and end offset distances can be defined, so the first and last point will not fall exactly on the line's "
65 "first and last nodes. These start and end offsets are defined as distances, measured along the line from the first and last "
66 "nodes of the lines." );
67}
68
69QString QgsPointsAlongGeometryAlgorithm::shortDescription() const
70{
71 return QObject::tr( "Creates regularly spaced points along line features." );
72}
73
74Qgis::ProcessingAlgorithmDocumentationFlags QgsPointsAlongGeometryAlgorithm::documentationFlags() const
75{
77}
78
79QList<int> QgsPointsAlongGeometryAlgorithm::inputLayerTypes() const
80{
81 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorLine ) << static_cast<int>( Qgis::ProcessingSourceType::VectorPolygon );
82}
83
84Qgis::ProcessingSourceType QgsPointsAlongGeometryAlgorithm::outputLayerType() const
85{
87}
88
89Qgis::WkbType QgsPointsAlongGeometryAlgorithm::outputWkbType( Qgis::WkbType inputType ) const
90{
92 if ( QgsWkbTypes::hasZ( inputType ) )
93 out = QgsWkbTypes::addZ( out );
94 if ( QgsWkbTypes::hasM( inputType ) )
95 out = QgsWkbTypes::addM( out );
96 return out;
97}
98
99QgsFields QgsPointsAlongGeometryAlgorithm::outputFields( const QgsFields &inputFields ) const
100{
101 QgsFields newFields;
102 newFields.append( QgsField( u"distance"_s, QMetaType::Type::Double ) );
103 newFields.append( QgsField( u"angle"_s, QMetaType::Type::Double ) );
104 return QgsProcessingUtils::combineFields( inputFields, newFields );
105}
106
107QgsPointsAlongGeometryAlgorithm *QgsPointsAlongGeometryAlgorithm::createInstance() const
108{
109 return new QgsPointsAlongGeometryAlgorithm();
110}
111
112void QgsPointsAlongGeometryAlgorithm::initParameters( const QVariantMap & )
113{
114 auto distance = std::make_unique<QgsProcessingParameterDistance>( u"DISTANCE"_s, QObject::tr( "Distance" ), 1.0, u"INPUT"_s, false, 0 );
115 distance->setIsDynamic( true );
116 distance->setDynamicPropertyDefinition( QgsPropertyDefinition( u"DISTANCE"_s, QObject::tr( "Distance" ), QgsPropertyDefinition::DoublePositive ) );
117 distance->setDynamicLayerParameterName( u"INPUT"_s );
118 addParameter( distance.release() );
119
120 auto startOffset = std::make_unique<QgsProcessingParameterDistance>( u"START_OFFSET"_s, QObject::tr( "Start offset" ), 0.0, u"INPUT"_s, false, 0 );
121 startOffset->setIsDynamic( true );
122 startOffset->setDynamicPropertyDefinition( QgsPropertyDefinition( u"START_OFFSET"_s, QObject::tr( "Start offset" ), QgsPropertyDefinition::DoublePositive ) );
123 startOffset->setDynamicLayerParameterName( u"INPUT"_s );
124 addParameter( startOffset.release() );
125
126 auto endOffset = std::make_unique<QgsProcessingParameterDistance>( u"END_OFFSET"_s, QObject::tr( "End offset" ), 0.0, u"INPUT"_s, false, 0 );
127 endOffset->setIsDynamic( true );
128 endOffset->setDynamicPropertyDefinition( QgsPropertyDefinition( u"END_OFFSET"_s, QObject::tr( "End offset" ), QgsPropertyDefinition::DoublePositive ) );
129 endOffset->setDynamicLayerParameterName( u"INPUT"_s );
130 addParameter( endOffset.release() );
131}
132
133QIcon QgsPointsAlongGeometryAlgorithm::icon() const
134{
135 return QgsApplication::getThemeIcon( u"/algorithms/mAlgorithmExtractVertices.svg"_s );
136}
137
138QString QgsPointsAlongGeometryAlgorithm::svgIconPath() const
139{
140 return QgsApplication::iconPath( u"/algorithms/mAlgorithmExtractVertices.svg"_s );
141}
142
143Qgis::ProcessingFeatureSourceFlags QgsPointsAlongGeometryAlgorithm::sourceFlags() const
144{
145 // skip geometry checks - this algorithm doesn't care about invalid geometries
147}
148
149QgsFeatureSink::SinkFlags QgsPointsAlongGeometryAlgorithm::sinkFlags() const
150{
152}
153
154bool QgsPointsAlongGeometryAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
155{
156 mDistance = parameterAsDouble( parameters, u"DISTANCE"_s, context );
157 mDynamicDistance = QgsProcessingParameters::isDynamic( parameters, u"DISTANCE"_s );
158 if ( mDynamicDistance )
159 mDistanceProperty = parameters.value( u"DISTANCE"_s ).value<QgsProperty>();
160
161 mStartOffset = parameterAsDouble( parameters, u"START_OFFSET"_s, context );
162 mDynamicStartOffset = QgsProcessingParameters::isDynamic( parameters, u"START_OFFSET"_s );
163 if ( mDynamicStartOffset )
164 mStartOffsetProperty = parameters.value( u"START_OFFSET"_s ).value<QgsProperty>();
165
166 mEndOffset = parameterAsDouble( parameters, u"END_OFFSET"_s, context );
167 mDynamicEndOffset = QgsProcessingParameters::isDynamic( parameters, u"END_OFFSET"_s );
168 if ( mDynamicEndOffset )
169 mEndOffsetProperty = parameters.value( u"END_OFFSET"_s ).value<QgsProperty>();
170
171 return true;
172}
173
174QgsFeatureList QgsPointsAlongGeometryAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
175{
176 QgsFeature f = feature;
177 if ( f.hasGeometry() )
178 {
179 const QgsGeometry geometry = f.geometry();
180
181 double distance = mDistance;
182 if ( mDynamicDistance )
183 distance = mDistanceProperty.valueAsDouble( context.expressionContext(), distance );
184 if ( distance <= 0 )
185 return QgsFeatureList();
186
187 double startOffset = mStartOffset;
188 if ( mDynamicStartOffset )
189 startOffset = mStartOffsetProperty.valueAsDouble( context.expressionContext(), startOffset );
190
191 double endOffset = mEndOffset;
192 if ( mDynamicEndOffset )
193 endOffset = mEndOffsetProperty.valueAsDouble( context.expressionContext(), endOffset );
194
195 const double totalLength = geometry.type() == Qgis::GeometryType::Polygon ? geometry.constGet()->perimeter()
196 : geometry.length() - endOffset;
197
198 double currentDistance = startOffset;
199 QgsFeatureList out;
200 out.reserve( static_cast<int>( std::ceil( ( totalLength - startOffset ) / distance ) ) );
201 while ( currentDistance <= totalLength )
202 {
203 const QgsGeometry point = geometry.interpolate( currentDistance );
204 const double angle = ( 180 / M_PI ) * geometry.interpolateAngle( currentDistance );
205 QgsFeature outputFeature;
206 outputFeature.setGeometry( point );
207 QgsAttributes outAttr = f.attributes();
208 outAttr << currentDistance << angle;
209 outputFeature.setAttributes( outAttr );
210 out.append( outputFeature );
211 currentDistance += distance;
212 if ( feedback->isCanceled() ) // better check here -- a ridiculously small distance might take forever
213 break;
214 }
215 return out;
216 }
217 else
218 {
219 QgsAttributes outAttr = f.attributes();
220 outAttr << QVariant() << QVariant();
221 f.setAttributes( outAttr );
222 return QgsFeatureList() << f;
223 }
224}
225
ProcessingSourceType
Processing data source types.
Definition qgis.h:3602
@ VectorPoint
Vector point layers.
Definition qgis.h:3605
@ VectorPolygon
Vector polygon layers.
Definition qgis.h:3607
@ VectorLine
Vector line layers.
Definition qgis.h:3606
@ Polygon
Polygons.
Definition qgis.h:368
@ RegeneratesPrimaryKey
Algorithm always drops any existing primary keys or FID values and regenerates them in outputs.
Definition qgis.h:3690
QFlags< ProcessingAlgorithmDocumentationFlag > ProcessingAlgorithmDocumentationFlags
Flags describing algorithm behavior for documentation purposes.
Definition qgis.h:3701
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Definition qgis.h:3782
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:280
@ Point
Point.
Definition qgis.h:282
QFlags< ProcessingFeatureSourceFlag > ProcessingFeatureSourceFlags
Flags which control how QgsProcessingFeatureSource fetches features.
Definition qgis.h:3793
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:60
QgsAttributes attributes
Definition qgsfeature.h:69
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
QgsGeometry geometry
Definition qgsfeature.h:71
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:55
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:56
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:76
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...
static QgsFields combineFields(const QgsFields &fieldsA, const QgsFields &fieldsB, const QString &fieldsBPrefix=QString())
Combines two field lists, avoiding duplicate field names (in a case-insensitive manner).
Definition for a property.
Definition qgsproperty.h:47
@ DoublePositive
Positive double value (including 0).
Definition qgsproperty.h:58
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 Q_INVOKABLE bool hasZ(Qgis::WkbType type)
Tests whether a WKB type contains the z-dimension.
static Q_INVOKABLE 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