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