QGIS API Documentation 3.41.0-Master (af5edcb665c)
Loading...
Searching...
No Matches
qgsalgorithmextendlines.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmextendlines.cpp
3 ---------------------
4 begin : July 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
21
22QString QgsExtendLinesAlgorithm::name() const
23{
24 return QStringLiteral( "extendlines" );
25}
26
27QString QgsExtendLinesAlgorithm::displayName() const
28{
29 return QObject::tr( "Extend lines" );
30}
31
32QStringList QgsExtendLinesAlgorithm::tags() const
33{
34 return QObject::tr( "linestring,continue,grow,extrapolate" ).split( ',' );
35}
36
37QString QgsExtendLinesAlgorithm::group() const
38{
39 return QObject::tr( "Vector geometry" );
40}
41
42QString QgsExtendLinesAlgorithm::groupId() const
43{
44 return QStringLiteral( "vectorgeometry" );
45}
46
47QString QgsExtendLinesAlgorithm::outputName() const
48{
49 return QObject::tr( "Extended" );
50}
51
52QString QgsExtendLinesAlgorithm::shortHelpString() const
53{
54 return QObject::tr( "This algorithm extends line geometries by a specified amount at the start and end "
55 "of the line. Lines are extended using the bearing of the first and last segment "
56 "in the line." );
57}
58
59QString QgsExtendLinesAlgorithm::shortDescription() const
60{
61 return QObject::tr( "Extends LineString geometries by extrapolating the start and end segments." );
62}
63
64QList<int> QgsExtendLinesAlgorithm::inputLayerTypes() const
65{
66 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorLine );
67}
68
69Qgis::ProcessingSourceType QgsExtendLinesAlgorithm::outputLayerType() const
70{
72}
73
74QgsExtendLinesAlgorithm *QgsExtendLinesAlgorithm::createInstance() const
75{
76 return new QgsExtendLinesAlgorithm();
77}
78
79void QgsExtendLinesAlgorithm::initParameters( const QVariantMap & )
80{
81 std::unique_ptr<QgsProcessingParameterDistance> startDistance = std::make_unique<QgsProcessingParameterDistance>( QStringLiteral( "START_DISTANCE" ), QObject::tr( "Start distance" ), 0.0, QStringLiteral( "INPUT" ), false, 0 );
82 startDistance->setIsDynamic( true );
83 startDistance->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "Start Distance" ), QObject::tr( "Start distance" ), QgsPropertyDefinition::DoublePositive ) );
84 startDistance->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
85 addParameter( startDistance.release() );
86
87 std::unique_ptr<QgsProcessingParameterDistance> endDistance = std::make_unique<QgsProcessingParameterDistance>( QStringLiteral( "END_DISTANCE" ), QObject::tr( "End distance" ), 0.0, QStringLiteral( "INPUT" ), false, 0 );
88 endDistance->setIsDynamic( true );
89 endDistance->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "End Distance" ), QObject::tr( "End distance" ), QgsPropertyDefinition::DoublePositive ) );
90 endDistance->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
91 addParameter( endDistance.release() );
92}
93
94Qgis::ProcessingFeatureSourceFlags QgsExtendLinesAlgorithm::sourceFlags() const
95{
96 // skip geometry checks - this algorithm doesn't care about invalid geometries
98}
99
100bool QgsExtendLinesAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
101{
102 mStartDistance = parameterAsDouble( parameters, QStringLiteral( "START_DISTANCE" ), context );
103 mDynamicStartDistance = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "START_DISTANCE" ) );
104 if ( mDynamicStartDistance )
105 mStartDistanceProperty = parameters.value( QStringLiteral( "START_DISTANCE" ) ).value<QgsProperty>();
106
107 mEndDistance = parameterAsDouble( parameters, QStringLiteral( "END_DISTANCE" ), context );
108 mDynamicEndDistance = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "END_DISTANCE" ) );
109 if ( mDynamicEndDistance )
110 mEndDistanceProperty = parameters.value( QStringLiteral( "END_DISTANCE" ) ).value<QgsProperty>();
111
112 return true;
113}
114
115QgsFeatureList QgsExtendLinesAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
116{
117 QgsFeature f = feature;
118 if ( f.hasGeometry() )
119 {
120 const QgsGeometry geometry = f.geometry();
121 double startDistance = mStartDistance;
122 if ( mDynamicStartDistance )
123 startDistance = mStartDistanceProperty.valueAsDouble( context.expressionContext(), startDistance );
124
125 double endDistance = mEndDistance;
126 if ( mDynamicEndDistance )
127 endDistance = mEndDistanceProperty.valueAsDouble( context.expressionContext(), endDistance );
128
129 const QgsGeometry outGeometry = geometry.extendLine( startDistance, endDistance );
130 if ( outGeometry.isNull() )
131 throw QgsProcessingException( QObject::tr( "Error calculating extended line" ) ); // don't think this can actually happen!
132
133 f.setGeometry( outGeometry );
134 }
135 return QgsFeatureList() << f;
136}
137
ProcessingSourceType
Processing data source types.
Definition qgis.h:3333
@ VectorLine
Vector line layers.
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
QFlags< ProcessingFeatureSourceFlag > ProcessingFeatureSourceFlags
Flags which control how QgsProcessingFeatureSource fetches features.
Definition qgis.h:3507
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
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.
QgsGeometry extendLine(double startDistance, double endDistance) const
Extends a (multi)line geometry by extrapolating out the start or end of the line by a specified dista...
Contains information about the context in which a processing algorithm is executed.
QgsExpressionContext & expressionContext()
Returns the expression context.
Custom exception class for processing related exceptions.
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.
QList< QgsFeature > QgsFeatureList