QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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"
23 #include "qgsgeometrycollection.h"
24 
26 
27 QString QgsSplitLinesByLengthAlgorithm::name() const
28 {
29  return QStringLiteral( "splitlinesbylength" );
30 }
31 
32 QString QgsSplitLinesByLengthAlgorithm::displayName() const
33 {
34  return QObject::tr( "Split lines by maximum length" );
35 }
36 
37 QStringList QgsSplitLinesByLengthAlgorithm::tags() const
38 {
39  return QObject::tr( "segments,parts,distance,cut,chop" ).split( ',' );
40 }
41 
42 QString QgsSplitLinesByLengthAlgorithm::group() const
43 {
44  return QObject::tr( "Vector geometry" );
45 }
46 
47 QString QgsSplitLinesByLengthAlgorithm::groupId() const
48 {
49  return QStringLiteral( "vectorgeometry" );
50 }
51 
52 QString 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 
59 QString QgsSplitLinesByLengthAlgorithm::shortDescription() const
60 {
61  return QObject::tr( "Splits lines into parts which are no longer than a specified length." );
62 }
63 
64 QList<int> QgsSplitLinesByLengthAlgorithm::inputLayerTypes() const
65 {
66  return QList<int>() << QgsProcessing::TypeVectorLine;
67 }
68 
69 QgsProcessing::SourceType QgsSplitLinesByLengthAlgorithm::outputLayerType() const
70 {
72 }
73 
74 QgsSplitLinesByLengthAlgorithm *QgsSplitLinesByLengthAlgorithm::createInstance() const
75 {
76  return new QgsSplitLinesByLengthAlgorithm();
77 }
78 
79 void QgsSplitLinesByLengthAlgorithm::initParameters( const QVariantMap & )
80 {
81  std::unique_ptr< QgsProcessingParameterDistance > length = qgis::make_unique< QgsProcessingParameterDistance >( QStringLiteral( "LENGTH" ),
82  QObject::tr( "Maximum line length" ), 10, QStringLiteral( "INPUT" ), false, 0 );
83  length->setIsDynamic( true );
84  length->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "LENGTH" ), QObject::tr( "Maximum length" ), QgsPropertyDefinition::DoublePositive ) );
85  length->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
86  addParameter( length.release() );
87 }
88 
89 bool QgsSplitLinesByLengthAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
90 {
91  mLength = parameterAsDouble( parameters, QStringLiteral( "LENGTH" ), context );
92  mDynamicLength = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "LENGTH" ) );
93  if ( mDynamicLength )
94  mLengthProperty = parameters.value( QStringLiteral( "LENGTH" ) ).value< QgsProperty >();
95 
96  return true;
97 }
98 
99 QString QgsSplitLinesByLengthAlgorithm::outputName() const
100 {
101  return QObject::tr( "Split" );
102 }
103 
104 QgsWkbTypes::Type QgsSplitLinesByLengthAlgorithm::outputWkbType( QgsWkbTypes::Type inputWkbType ) const
105 {
106  return QgsWkbTypes::singleType( inputWkbType );
107 }
108 
109 QgsFeatureList QgsSplitLinesByLengthAlgorithm::processFeature( const QgsFeature &f, QgsProcessingContext &context, QgsProcessingFeedback * )
110 {
111  if ( !f.hasGeometry() )
112  {
113  return QgsFeatureList() << f;
114  }
115  else
116  {
117  double distance = mLength;
118  if ( mDynamicLength )
119  distance = mLengthProperty.valueAsDouble( context.expressionContext(), distance );
120 
121  QgsFeature outputFeature;
122  outputFeature.setAttributes( f.attributes() );
123  QgsFeatureList features;
124  const QgsGeometry inputGeom = f.geometry();
125  for ( auto it = inputGeom.const_parts_begin(); it != inputGeom.const_parts_end(); ++it )
126  {
127  const QgsCurve *part = qgsgeometry_cast< const QgsCurve * >( *it );
128  if ( !part )
129  continue;
130 
131  double start = 0.0;
132  double end = distance;
133  const double length = part->length();
134  while ( start < length )
135  {
136  outputFeature.setGeometry( QgsGeometry( part->curveSubstring( start, end ) ) );
137  start += distance;
138  end += distance;
139  features << outputFeature;
140  }
141 
142  }
143  return features;
144  }
145 }
146 
147 QgsProcessingFeatureSource::Flag QgsSplitLinesByLengthAlgorithm::sourceFlags() const
148 {
150 }
151 
152 
154 
155 
156 
QgsCurve
Abstract base class for curved geometry type.
Definition: qgscurve.h:35
QgsProperty
A store for object properties.
Definition: qgsproperty.h:231
qgslinestring.h
QgsProcessingParameters::isDynamic
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: qgsprocessingparameters.cpp:111
QgsWkbTypes::singleType
static Type singleType(Type type)
Returns the single type for a WKB type.
Definition: qgswkbtypes.h:156
QgsGeometry::const_parts_end
QgsAbstractGeometry::const_part_iterator const_parts_end() const
Returns STL-style iterator pointing to the imaginary part after the last part of the geometry.
Definition: qgsgeometry.cpp:1895
QgsProcessingFeedback
Definition: qgsprocessingfeedback.h:37
qgscompoundcurve.h
QgsFeature::geometry
QgsGeometry geometry
Definition: qgsfeature.h:71
QgsAbstractGeometry::length
virtual double length() const
Returns the planar, 2-dimensional length of the geometry.
Definition: qgsabstractgeometry.cpp:132
QgsProcessing::TypeVectorLine
@ TypeVectorLine
Vector line layers.
Definition: qgsprocessing.h:49
QgsProcessingFeatureSource::FlagSkipGeometryValidityChecks
@ FlagSkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Definition: qgsprocessingutils.h:475
QgsGeometry::const_parts_begin
QgsAbstractGeometry::const_part_iterator const_parts_begin() const
Returns STL-style const iterator pointing to the first part of the geometry.
Definition: qgsgeometry.cpp:1888
QgsWkbTypes::Type
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:68
QgsFeature::setGeometry
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
Definition: qgsfeature.cpp:137
QgsProcessingContext
Definition: qgsprocessingcontext.h:43
QgsCurve::curveSubstring
virtual QgsCurve * curveSubstring(double startDistance, double endDistance) const =0
Returns a new curve representing a substring of this curve.
QgsFeatureList
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:572
QgsPropertyDefinition
Definition for a property.
Definition: qgsproperty.h:47
qgscircularstring.h
QgsFeature::attributes
QgsAttributes attributes
Definition: qgsfeature.h:69
qgsalgorithmsplitlinesbylength.h
QgsProcessingFeatureSource::Flag
Flag
Flags controlling how QgsProcessingFeatureSource fetches features.
Definition: qgsprocessingutils.h:473
qgscurve.h
QgsGeometry
Definition: qgsgeometry.h:122
QgsFeature::hasGeometry
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:197
QgsProcessingContext::expressionContext
QgsExpressionContext & expressionContext()
Returns the expression context.
Definition: qgsprocessingcontext.h:119
qgsgeometrycollection.h
QgsFeature
Definition: qgsfeature.h:55
QgsFeature::setAttributes
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
Definition: qgsfeature.cpp:127
QgsProcessing::SourceType
SourceType
Data source types enum.
Definition: qgsprocessing.h:44
QgsPropertyDefinition::DoublePositive
@ DoublePositive
Positive double value (including 0)
Definition: qgsproperty.h:59