QGIS API Documentation 3.41.0-Master (cea29feecf2)
Loading...
Searching...
No Matches
qgsalgorithmsplitlinesbylength.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmsplitlinesbylength.cpp
3 ---------------------
4 begin : December 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 "qgscurve.h"
20#include "qgslinestring.h"
21#include "qgscircularstring.h"
22#include "qgscompoundcurve.h"
24
26
27QString QgsSplitLinesByLengthAlgorithm::name() const
28{
29 return QStringLiteral( "splitlinesbylength" );
30}
31
32QString QgsSplitLinesByLengthAlgorithm::displayName() const
33{
34 return QObject::tr( "Split lines by maximum length" );
35}
36
37QStringList QgsSplitLinesByLengthAlgorithm::tags() const
38{
39 return QObject::tr( "segments,parts,distance,cut,chop" ).split( ',' );
40}
41
42QString QgsSplitLinesByLengthAlgorithm::group() const
43{
44 return QObject::tr( "Vector geometry" );
45}
46
47QString QgsSplitLinesByLengthAlgorithm::groupId() const
48{
49 return QStringLiteral( "vectorgeometry" );
50}
51
52QString QgsSplitLinesByLengthAlgorithm::shortHelpString() const
53{
54 return QObject::tr( "This algorithm takes a line (or curve) layer and splits each feature into multiple parts, "
55 "where each part is of a specified maximum length.\n\n"
56 "Z and M values at the start and end of the new line substrings are linearly interpolated from existing values." );
57}
58
59QString QgsSplitLinesByLengthAlgorithm::shortDescription() const
60{
61 return QObject::tr( "Splits lines into parts which are no longer than a specified length." );
62}
63
64Qgis::ProcessingAlgorithmDocumentationFlags QgsSplitLinesByLengthAlgorithm::documentationFlags() const
65{
67}
68
69QList<int> QgsSplitLinesByLengthAlgorithm::inputLayerTypes() const
70{
71 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorLine );
72}
73
74Qgis::ProcessingSourceType QgsSplitLinesByLengthAlgorithm::outputLayerType() const
75{
77}
78
79QgsSplitLinesByLengthAlgorithm *QgsSplitLinesByLengthAlgorithm::createInstance() const
80{
81 return new QgsSplitLinesByLengthAlgorithm();
82}
83
84void QgsSplitLinesByLengthAlgorithm::initParameters( const QVariantMap & )
85{
86 std::unique_ptr<QgsProcessingParameterDistance> length = std::make_unique<QgsProcessingParameterDistance>( QStringLiteral( "LENGTH" ), QObject::tr( "Maximum line length" ), 10, QStringLiteral( "INPUT" ), false, 0 );
87 length->setIsDynamic( true );
88 length->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "LENGTH" ), QObject::tr( "Maximum length" ), QgsPropertyDefinition::DoublePositive ) );
89 length->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
90 addParameter( length.release() );
91}
92
93bool QgsSplitLinesByLengthAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
94{
95 mLength = parameterAsDouble( parameters, QStringLiteral( "LENGTH" ), context );
96 mDynamicLength = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "LENGTH" ) );
97 if ( mDynamicLength )
98 mLengthProperty = parameters.value( QStringLiteral( "LENGTH" ) ).value<QgsProperty>();
99
100 return true;
101}
102
103QString QgsSplitLinesByLengthAlgorithm::outputName() const
104{
105 return QObject::tr( "Split" );
106}
107
108Qgis::WkbType QgsSplitLinesByLengthAlgorithm::outputWkbType( Qgis::WkbType inputWkbType ) const
109{
110 return QgsWkbTypes::singleType( inputWkbType );
111}
112
113QgsFeatureList QgsSplitLinesByLengthAlgorithm::processFeature( const QgsFeature &f, QgsProcessingContext &context, QgsProcessingFeedback * )
114{
115 if ( !f.hasGeometry() )
116 {
117 return QgsFeatureList() << f;
118 }
119 else
120 {
121 double distance = mLength;
122 if ( mDynamicLength )
123 distance = mLengthProperty.valueAsDouble( context.expressionContext(), distance );
124
125 QgsFeature outputFeature;
126 outputFeature.setAttributes( f.attributes() );
127 QgsFeatureList features;
128 const QgsGeometry inputGeom = f.geometry();
129 for ( auto it = inputGeom.const_parts_begin(); it != inputGeom.const_parts_end(); ++it )
130 {
131 const QgsCurve *part = qgsgeometry_cast<const QgsCurve *>( *it );
132 if ( !part )
133 continue;
134
135 double start = 0.0;
136 double end = distance;
137 const double length = part->length();
138 while ( start < length )
139 {
140 outputFeature.setGeometry( QgsGeometry( part->curveSubstring( start, end ) ) );
141 start += distance;
142 end += distance;
143 features << outputFeature;
144 }
145 }
146 return features;
147 }
148}
149
150Qgis::ProcessingFeatureSourceFlags QgsSplitLinesByLengthAlgorithm::sourceFlags() const
151{
153}
154
155QgsFeatureSink::SinkFlags QgsSplitLinesByLengthAlgorithm::sinkFlags() const
156{
158}
159
160
ProcessingSourceType
Processing data source types.
Definition qgis.h:3315
@ VectorLine
Vector line layers.
@ 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:3412
@ 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:3489
virtual double length() const
Returns the planar, 2-dimensional length of the geometry.
Abstract base class for curved geometry type.
Definition qgscurve.h:35
virtual QgsCurve * curveSubstring(double startDistance, double endDistance) const =0
Returns a new curve representing a substring of this curve.
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.
A geometry is the spatial representation of a feature.
QgsAbstractGeometry::const_part_iterator const_parts_begin() const
Returns STL-style const iterator pointing to the first part of the geometry.
QgsAbstractGeometry::const_part_iterator const_parts_end() const
Returns STL-style iterator pointing to the imaginary part after the last part of the geometry.
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.
static Qgis::WkbType singleType(Qgis::WkbType type)
Returns the single type for a WKB type.
Definition qgswkbtypes.h:53
QList< QgsFeature > QgsFeatureList