QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
qgsalgorithmlinesubstring.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmlinesubstring.cpp
3 ---------------------
4 begin : August 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 "qgscurve.h"
22
24
25QString QgsLineSubstringAlgorithm::name() const
26{
27 return QStringLiteral( "linesubstring" );
28}
29
30QString QgsLineSubstringAlgorithm::displayName() const
31{
32 return QObject::tr( "Line substring" );
33}
34
35QStringList QgsLineSubstringAlgorithm::tags() const
36{
37 return QObject::tr( "linestring,curve,split,shorten,shrink,portion,part,reference,referencing,distance,interpolate" ).split( ',' );
38}
39
40QString QgsLineSubstringAlgorithm::group() const
41{
42 return QObject::tr( "Vector geometry" );
43}
44
45QString QgsLineSubstringAlgorithm::groupId() const
46{
47 return QStringLiteral( "vectorgeometry" );
48}
49
50QString QgsLineSubstringAlgorithm::outputName() const
51{
52 return QObject::tr( "Substring" );
53}
54
55QString QgsLineSubstringAlgorithm::shortHelpString() const
56{
57 return QObject::tr( "This algorithm returns the portion of a line (or curve) which falls "
58 "between the specified start and end distances (measured from the "
59 "beginning of the line).\n\n"
60 "Z and M values are linearly interpolated from existing values.\n\n"
61 "If a multipart geometry is encountered, only the first part is considered when "
62 "calculating the substring." );
63}
64
65QString QgsLineSubstringAlgorithm::shortDescription() const
66{
67 return QObject::tr( "Returns the substring of lines which fall between start and end distances." );
68}
69
70QList<int> QgsLineSubstringAlgorithm::inputLayerTypes() const
71{
72 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorLine );
73}
74
75Qgis::ProcessingSourceType QgsLineSubstringAlgorithm::outputLayerType() const
76{
78}
79
80QgsLineSubstringAlgorithm *QgsLineSubstringAlgorithm::createInstance() const
81{
82 return new QgsLineSubstringAlgorithm();
83}
84
85void QgsLineSubstringAlgorithm::initParameters( const QVariantMap & )
86{
87 auto startDistance = std::make_unique<QgsProcessingParameterDistance>( QStringLiteral( "START_DISTANCE" ), QObject::tr( "Start distance" ), 0.0, QStringLiteral( "INPUT" ), false, 0 );
88 startDistance->setIsDynamic( true );
89 startDistance->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "Start Distance" ), QObject::tr( "Start distance" ), QgsPropertyDefinition::DoublePositive ) );
90 startDistance->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
91 addParameter( startDistance.release() );
92
93 auto endDistance = std::make_unique<QgsProcessingParameterDistance>( QStringLiteral( "END_DISTANCE" ), QObject::tr( "End distance" ), 1.0, QStringLiteral( "INPUT" ), false, 0 );
94 endDistance->setIsDynamic( true );
95 endDistance->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "End Distance" ), QObject::tr( "End distance" ), QgsPropertyDefinition::DoublePositive ) );
96 endDistance->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
97 addParameter( endDistance.release() );
98}
99
100Qgis::ProcessingFeatureSourceFlags QgsLineSubstringAlgorithm::sourceFlags() const
101{
102 // skip geometry checks - this algorithm doesn't care about invalid geometries
104}
105
106bool QgsLineSubstringAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
107{
108 mStartDistance = parameterAsDouble( parameters, QStringLiteral( "START_DISTANCE" ), context );
109 mDynamicStartDistance = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "START_DISTANCE" ) );
110 if ( mDynamicStartDistance )
111 mStartDistanceProperty = parameters.value( QStringLiteral( "START_DISTANCE" ) ).value<QgsProperty>();
112
113 mEndDistance = parameterAsDouble( parameters, QStringLiteral( "END_DISTANCE" ), context );
114 mDynamicEndDistance = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "END_DISTANCE" ) );
115 if ( mDynamicEndDistance )
116 mEndDistanceProperty = parameters.value( QStringLiteral( "END_DISTANCE" ) ).value<QgsProperty>();
117
118 return true;
119}
120
121QgsFeatureList QgsLineSubstringAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
122{
123 QgsFeature f = feature;
124 if ( f.hasGeometry() )
125 {
126 const QgsGeometry geometry = f.geometry();
127 double startDistance = mStartDistance;
128 if ( mDynamicStartDistance )
129 startDistance = mStartDistanceProperty.valueAsDouble( context.expressionContext(), startDistance );
130
131 double endDistance = mEndDistance;
132 if ( mDynamicEndDistance )
133 endDistance = mEndDistanceProperty.valueAsDouble( context.expressionContext(), endDistance );
134
135 const QgsCurve *curve = nullptr;
136 if ( !geometry.isMultipart() )
137 curve = qgsgeometry_cast<const QgsCurve *>( geometry.constGet() );
138 else
139 {
141 {
142 if ( collection->numGeometries() > 0 )
143 {
144 curve = qgsgeometry_cast<const QgsCurve *>( collection->geometryN( 0 ) );
145 }
146 }
147 }
148 if ( curve )
149 {
150 std::unique_ptr<QgsCurve> substring( curve->curveSubstring( startDistance, endDistance ) );
151 const QgsGeometry result( std::move( substring ) );
152 f.setGeometry( result );
153 }
154 else
155 {
156 f.clearGeometry();
157 }
158 }
159 return QgsFeatureList() << f;
160}
161
ProcessingSourceType
Processing data source types.
Definition qgis.h:3531
@ VectorLine
Vector line layers.
Definition qgis.h:3535
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Definition qgis.h:3711
QFlags< ProcessingFeatureSourceFlag > ProcessingFeatureSourceFlags
Flags which control how QgsProcessingFeatureSource fetches features.
Definition qgis.h:3722
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.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
QgsGeometry geometry
Definition qgsfeature.h:69
void clearGeometry()
Removes any geometry associated with the feature.
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.
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
bool isMultipart() const
Returns true if WKB of the geometry is of WKBMulti* type.
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.
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.
T qgsgeometry_cast(QgsAbstractGeometry *geom)
QList< QgsFeature > QgsFeatureList