QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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(
63 "This algorithm creates a points layer, with points distributed along the lines of an "
64 "input vector layer. The distance between points (measured along the line) is defined as a parameter.\n\n"
65 "Start and end offset distances can be defined, so the first and last point will not fall exactly on the line's "
66 "first and last nodes. These start and end offsets are defined as distances, measured along the line from the first and last "
67 "nodes of the lines."
68 );
69}
70
71QString QgsPointsAlongGeometryAlgorithm::shortDescription() const
72{
73 return QObject::tr( "Creates regularly spaced points along line features." );
74}
75
76Qgis::ProcessingAlgorithmDocumentationFlags QgsPointsAlongGeometryAlgorithm::documentationFlags() const
77{
79}
80
81QList<int> QgsPointsAlongGeometryAlgorithm::inputLayerTypes() const
82{
83 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorLine ) << static_cast<int>( Qgis::ProcessingSourceType::VectorPolygon );
84}
85
86Qgis::ProcessingSourceType QgsPointsAlongGeometryAlgorithm::outputLayerType() const
87{
89}
90
91Qgis::WkbType QgsPointsAlongGeometryAlgorithm::outputWkbType( Qgis::WkbType inputType ) const
92{
94 if ( QgsWkbTypes::hasZ( inputType ) )
95 out = QgsWkbTypes::addZ( out );
96 if ( QgsWkbTypes::hasM( inputType ) )
97 out = QgsWkbTypes::addM( out );
98 return out;
99}
100
101QgsFields QgsPointsAlongGeometryAlgorithm::outputFields( const QgsFields &inputFields ) const
102{
103 QgsFields newFields;
104 newFields.append( QgsField( u"distance"_s, QMetaType::Type::Double ) );
105 newFields.append( QgsField( u"angle"_s, QMetaType::Type::Double ) );
106 return QgsProcessingUtils::combineFields( inputFields, newFields );
107}
108
109QgsPointsAlongGeometryAlgorithm *QgsPointsAlongGeometryAlgorithm::createInstance() const
110{
111 return new QgsPointsAlongGeometryAlgorithm();
112}
113
114void QgsPointsAlongGeometryAlgorithm::initParameters( const QVariantMap & )
115{
116 auto distance = std::make_unique<QgsProcessingParameterDistance>( u"DISTANCE"_s, QObject::tr( "Distance" ), 1.0, u"INPUT"_s, false, 0 );
117 distance->setIsDynamic( true );
118 distance->setDynamicPropertyDefinition( QgsPropertyDefinition( u"DISTANCE"_s, QObject::tr( "Distance" ), QgsPropertyDefinition::DoublePositive ) );
119 distance->setDynamicLayerParameterName( u"INPUT"_s );
120 addParameter( distance.release() );
121
122 auto startOffset = std::make_unique<QgsProcessingParameterDistance>( u"START_OFFSET"_s, QObject::tr( "Start offset" ), 0.0, u"INPUT"_s, false, 0 );
123 startOffset->setIsDynamic( true );
124 startOffset->setDynamicPropertyDefinition( QgsPropertyDefinition( u"START_OFFSET"_s, QObject::tr( "Start offset" ), QgsPropertyDefinition::DoublePositive ) );
125 startOffset->setDynamicLayerParameterName( u"INPUT"_s );
126 addParameter( startOffset.release() );
127
128 auto endOffset = std::make_unique<QgsProcessingParameterDistance>( u"END_OFFSET"_s, QObject::tr( "End offset" ), 0.0, u"INPUT"_s, false, 0 );
129 endOffset->setIsDynamic( true );
130 endOffset->setDynamicPropertyDefinition( QgsPropertyDefinition( u"END_OFFSET"_s, QObject::tr( "End offset" ), QgsPropertyDefinition::DoublePositive ) );
131 endOffset->setDynamicLayerParameterName( u"INPUT"_s );
132 addParameter( endOffset.release() );
133}
134
135QIcon QgsPointsAlongGeometryAlgorithm::icon() const
136{
137 return QgsApplication::getThemeIcon( u"/algorithms/mAlgorithmExtractVertices.svg"_s );
138}
139
140QString QgsPointsAlongGeometryAlgorithm::svgIconPath() const
141{
142 return QgsApplication::iconPath( u"/algorithms/mAlgorithmExtractVertices.svg"_s );
143}
144
145Qgis::ProcessingFeatureSourceFlags QgsPointsAlongGeometryAlgorithm::sourceFlags() const
146{
147 // skip geometry checks - this algorithm doesn't care about invalid geometries
149}
150
151QgsFeatureSink::SinkFlags QgsPointsAlongGeometryAlgorithm::sinkFlags() const
152{
154}
155
156bool QgsPointsAlongGeometryAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
157{
158 mDistance = parameterAsDouble( parameters, u"DISTANCE"_s, context );
159 mDynamicDistance = QgsProcessingParameters::isDynamic( parameters, u"DISTANCE"_s );
160 if ( mDynamicDistance )
161 mDistanceProperty = parameters.value( u"DISTANCE"_s ).value<QgsProperty>();
162
163 mStartOffset = parameterAsDouble( parameters, u"START_OFFSET"_s, context );
164 mDynamicStartOffset = QgsProcessingParameters::isDynamic( parameters, u"START_OFFSET"_s );
165 if ( mDynamicStartOffset )
166 mStartOffsetProperty = parameters.value( u"START_OFFSET"_s ).value<QgsProperty>();
167
168 mEndOffset = parameterAsDouble( parameters, u"END_OFFSET"_s, context );
169 mDynamicEndOffset = QgsProcessingParameters::isDynamic( parameters, u"END_OFFSET"_s );
170 if ( mDynamicEndOffset )
171 mEndOffsetProperty = parameters.value( u"END_OFFSET"_s ).value<QgsProperty>();
172
173 return true;
174}
175
176QgsFeatureList QgsPointsAlongGeometryAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
177{
178 QgsFeature f = feature;
179 if ( f.hasGeometry() )
180 {
181 const QgsGeometry geometry = f.geometry();
182
183 double distance = mDistance;
184 if ( mDynamicDistance )
185 distance = mDistanceProperty.valueAsDouble( context.expressionContext(), distance );
186 if ( distance <= 0 )
187 return QgsFeatureList();
188
189 double startOffset = mStartOffset;
190 if ( mDynamicStartOffset )
191 startOffset = mStartOffsetProperty.valueAsDouble( context.expressionContext(), startOffset );
192
193 double endOffset = mEndOffset;
194 if ( mDynamicEndOffset )
195 endOffset = mEndOffsetProperty.valueAsDouble( context.expressionContext(), endOffset );
196
197 const double totalLength = geometry.type() == Qgis::GeometryType::Polygon ? geometry.constGet()->perimeter() : geometry.length() - endOffset;
198
199 double currentDistance = startOffset;
200 QgsFeatureList out;
201 out.reserve( static_cast<int>( std::ceil( ( totalLength - startOffset ) / distance ) ) );
202 while ( currentDistance <= totalLength )
203 {
204 const QgsGeometry point = geometry.interpolate( currentDistance );
205 const double angle = ( 180 / M_PI ) * geometry.interpolateAngle( currentDistance );
206 QgsFeature outputFeature;
207 outputFeature.setGeometry( point );
208 QgsAttributes outAttr = f.attributes();
209 outAttr << currentDistance << angle;
210 outputFeature.setAttributes( outAttr );
211 out.append( outputFeature );
212 currentDistance += distance;
213 if ( feedback->isCanceled() ) // better check here -- a ridiculously small distance might take forever
214 break;
215 }
216 return out;
217 }
218 else
219 {
220 QgsAttributes outAttr = f.attributes();
221 outAttr << QVariant() << QVariant();
222 f.setAttributes( outAttr );
223 return QgsFeatureList() << f;
224 }
225}
226
ProcessingSourceType
Processing data source types.
Definition qgis.h:3645
@ VectorPoint
Vector point layers.
Definition qgis.h:3648
@ VectorPolygon
Vector polygon layers.
Definition qgis.h:3650
@ VectorLine
Vector line layers.
Definition qgis.h:3649
@ Polygon
Polygons.
Definition qgis.h:382
@ RegeneratesPrimaryKey
Algorithm always drops any existing primary keys or FID values and regenerates them in outputs.
Definition qgis.h:3734
QFlags< ProcessingAlgorithmDocumentationFlag > ProcessingAlgorithmDocumentationFlags
Flags describing algorithm behavior for documentation purposes.
Definition qgis.h:3745
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Definition qgis.h:3828
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:294
@ Point
Point.
Definition qgis.h:296
QFlags< ProcessingFeatureSourceFlag > ProcessingFeatureSourceFlags
Flags which control how QgsProcessingFeatureSource fetches features.
Definition qgis.h:3839
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:56
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:75
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:57
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