QGIS API Documentation 3.34.0-Prizren (ffbdd678812)
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>() << QgsProcessing::TypeVectorLine;
67}
68
69QgsProcessing::SourceType 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" ),
82 QObject::tr( "Start distance" ), 0.0, QStringLiteral( "INPUT" ), false, 0 );
83 startDistance->setIsDynamic( true );
84 startDistance->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "Start Distance" ), QObject::tr( "Start distance" ), QgsPropertyDefinition::DoublePositive ) );
85 startDistance->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
86 addParameter( startDistance.release() );
87
88 std::unique_ptr< QgsProcessingParameterDistance> endDistance = std::make_unique< QgsProcessingParameterDistance >( QStringLiteral( "END_DISTANCE" ),
89 QObject::tr( "End distance" ), 0.0, QStringLiteral( "INPUT" ), false, 0 );
90 endDistance->setIsDynamic( true );
91 endDistance->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "End Distance" ), QObject::tr( "End distance" ), QgsPropertyDefinition::DoublePositive ) );
92 endDistance->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
93 addParameter( endDistance.release() );
94}
95
96QgsProcessingFeatureSource::Flag QgsExtendLinesAlgorithm::sourceFlags() const
97{
98 // skip geometry checks - this algorithm doesn't care about invalid geometries
100}
101
102bool QgsExtendLinesAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
103{
104 mStartDistance = parameterAsDouble( parameters, QStringLiteral( "START_DISTANCE" ), context );
105 mDynamicStartDistance = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "START_DISTANCE" ) );
106 if ( mDynamicStartDistance )
107 mStartDistanceProperty = parameters.value( QStringLiteral( "START_DISTANCE" ) ).value< QgsProperty >();
108
109 mEndDistance = parameterAsDouble( parameters, QStringLiteral( "END_DISTANCE" ), context );
110 mDynamicEndDistance = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "END_DISTANCE" ) );
111 if ( mDynamicEndDistance )
112 mEndDistanceProperty = parameters.value( QStringLiteral( "END_DISTANCE" ) ).value< QgsProperty >();
113
114 return true;
115}
116
117QgsFeatureList QgsExtendLinesAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
118{
119 QgsFeature f = feature;
120 if ( f.hasGeometry() )
121 {
122 const QgsGeometry geometry = f.geometry();
123 double startDistance = mStartDistance;
124 if ( mDynamicStartDistance )
125 startDistance = mStartDistanceProperty.valueAsDouble( context.expressionContext(), startDistance );
126
127 double endDistance = mEndDistance;
128 if ( mDynamicEndDistance )
129 endDistance = mEndDistanceProperty.valueAsDouble( context.expressionContext(), endDistance );
130
131 const QgsGeometry outGeometry = geometry.extendLine( startDistance, endDistance );
132 if ( outGeometry.isNull() )
133 throw QgsProcessingException( QObject::tr( "Error calculating extended line" ) ); // don't think this can actually happen!
134
135 f.setGeometry( outGeometry );
136 }
137 return QgsFeatureList() << f;
138}
139
141
142
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:56
QgsGeometry geometry
Definition qgsfeature.h:67
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.
Flag
Flags controlling how QgsProcessingFeatureSource fetches features.
@ FlagSkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
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...
SourceType
Data source types enum.
@ TypeVectorLine
Vector line layers.
Definition for a property.
Definition qgsproperty.h:46
@ DoublePositive
Positive double value (including 0)
Definition qgsproperty.h:57
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
Definition qgsfeature.h:920