QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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
20#include "qgscircularstring.h"
21#include "qgscompoundcurve.h"
22#include "qgscurve.h"
24#include "qgslinestring.h"
25
26#include <QString>
27
28using namespace Qt::StringLiterals;
29
31
32QString QgsSplitLinesByLengthAlgorithm::name() const
33{
34 return u"splitlinesbylength"_s;
35}
36
37QString QgsSplitLinesByLengthAlgorithm::displayName() const
38{
39 return QObject::tr( "Split lines by maximum length" );
40}
41
42QStringList QgsSplitLinesByLengthAlgorithm::tags() const
43{
44 return QObject::tr( "segments,parts,distance,cut,chop" ).split( ',' );
45}
46
47QString QgsSplitLinesByLengthAlgorithm::group() const
48{
49 return QObject::tr( "Vector geometry" );
50}
51
52QString QgsSplitLinesByLengthAlgorithm::groupId() const
53{
54 return u"vectorgeometry"_s;
55}
56
57QString QgsSplitLinesByLengthAlgorithm::shortHelpString() const
58{
59 return QObject::tr(
60 "This algorithm takes a line (or curve) layer and splits each feature into multiple parts, "
61 "where each part is of a specified maximum length.\n\n"
62 "Z and M values at the start and end of the new line substrings are linearly interpolated from existing values."
63 );
64}
65
66QString QgsSplitLinesByLengthAlgorithm::shortDescription() const
67{
68 return QObject::tr( "Splits lines into parts which are not longer than a specified length." );
69}
70
71Qgis::ProcessingAlgorithmDocumentationFlags QgsSplitLinesByLengthAlgorithm::documentationFlags() const
72{
74}
75
76QList<int> QgsSplitLinesByLengthAlgorithm::inputLayerTypes() const
77{
78 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorLine );
79}
80
81Qgis::ProcessingSourceType QgsSplitLinesByLengthAlgorithm::outputLayerType() const
82{
84}
85
86QgsSplitLinesByLengthAlgorithm *QgsSplitLinesByLengthAlgorithm::createInstance() const
87{
88 return new QgsSplitLinesByLengthAlgorithm();
89}
90
91void QgsSplitLinesByLengthAlgorithm::initParameters( const QVariantMap & )
92{
93 auto length = std::make_unique<QgsProcessingParameterDistance>( u"LENGTH"_s, QObject::tr( "Maximum line length" ), 10, u"INPUT"_s, false, 0 );
94 length->setIsDynamic( true );
95 length->setDynamicPropertyDefinition( QgsPropertyDefinition( u"LENGTH"_s, QObject::tr( "Maximum length" ), QgsPropertyDefinition::DoublePositive ) );
96 length->setDynamicLayerParameterName( u"INPUT"_s );
97 addParameter( length.release() );
98}
99
100bool QgsSplitLinesByLengthAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
101{
102 mLength = parameterAsDouble( parameters, u"LENGTH"_s, context );
103 mDynamicLength = QgsProcessingParameters::isDynamic( parameters, u"LENGTH"_s );
104 if ( mDynamicLength )
105 mLengthProperty = parameters.value( u"LENGTH"_s ).value<QgsProperty>();
106
107 return true;
108}
109
110QString QgsSplitLinesByLengthAlgorithm::outputName() const
111{
112 return QObject::tr( "Split" );
113}
114
115Qgis::WkbType QgsSplitLinesByLengthAlgorithm::outputWkbType( Qgis::WkbType inputWkbType ) const
116{
117 return QgsWkbTypes::singleType( inputWkbType );
118}
119
120QgsFields QgsSplitLinesByLengthAlgorithm::outputFields( const QgsFields &inputFields ) const
121{
122 QgsFields newFields;
123 newFields.append( QgsField( "order", QMetaType::Type::Int ) );
124 return QgsProcessingUtils::combineFields( inputFields, newFields );
125}
126
127QgsFeatureList QgsSplitLinesByLengthAlgorithm::processFeature( const QgsFeature &f, QgsProcessingContext &context, QgsProcessingFeedback * )
128{
129 if ( !f.hasGeometry() )
130 {
131 return QgsFeatureList() << f;
132 }
133 else
134 {
135 double distance = mLength;
136 if ( mDynamicLength )
137 distance = mLengthProperty.valueAsDouble( context.expressionContext(), distance );
138
139 QgsFeature outputFeature;
140 QgsFeatureList features;
141 const QgsGeometry inputGeom = f.geometry();
142 int order = 0;
143 for ( auto it = inputGeom.const_parts_begin(); it != inputGeom.const_parts_end(); ++it )
144 {
145 const QgsCurve *part = qgsgeometry_cast<const QgsCurve *>( *it );
146 if ( !part )
147 continue;
148
149 double start = 0.0;
150 double end = distance;
151 const double length = part->length();
152 while ( start < length )
153 {
154 outputFeature.setGeometry( QgsGeometry( part->curveSubstring( start, end ) ) );
155 outputFeature.setAttributes( f.attributes() << order );
156 order++;
157 start += distance;
158 end += distance;
159 features << outputFeature;
160 }
161 }
162 return features;
163 }
164}
165
166Qgis::ProcessingFeatureSourceFlags QgsSplitLinesByLengthAlgorithm::sourceFlags() const
167{
169}
170
171QgsFeatureSink::SinkFlags QgsSplitLinesByLengthAlgorithm::sinkFlags() const
172{
174}
175
176
ProcessingSourceType
Processing data source types.
Definition qgis.h:3645
@ VectorLine
Vector line layers.
Definition qgis.h:3649
@ 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
QFlags< ProcessingFeatureSourceFlag > ProcessingFeatureSourceFlags
Flags which control how QgsProcessingFeatureSource fetches features.
Definition qgis.h:3839
virtual double length() const
Returns the planar, 2-dimensional length of the geometry.
Abstract base class for curved geometry type.
Definition qgscurve.h:36
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: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.
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.
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...
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.
static Qgis::WkbType singleType(Qgis::WkbType type)
Returns the single type for a WKB type.
Definition qgswkbtypes.h:53
T qgsgeometry_cast(QgsAbstractGeometry *geom)
QList< QgsFeature > QgsFeatureList