QGIS API Documentation 3.99.0-Master (09f76ad7019)
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
20#include <QString>
21
22using namespace Qt::StringLiterals;
23
25
26QString QgsExtendLinesAlgorithm::name() const
27{
28 return u"extendlines"_s;
29}
30
31QString QgsExtendLinesAlgorithm::displayName() const
32{
33 return QObject::tr( "Extend lines" );
34}
35
36QStringList QgsExtendLinesAlgorithm::tags() const
37{
38 return QObject::tr( "linestring,continue,grow,extrapolate" ).split( ',' );
39}
40
41QString QgsExtendLinesAlgorithm::group() const
42{
43 return QObject::tr( "Vector geometry" );
44}
45
46QString QgsExtendLinesAlgorithm::groupId() const
47{
48 return u"vectorgeometry"_s;
49}
50
51QString QgsExtendLinesAlgorithm::outputName() const
52{
53 return QObject::tr( "Extended" );
54}
55
56QString QgsExtendLinesAlgorithm::shortHelpString() const
57{
58 return QObject::tr( "This algorithm extends line geometries by a specified amount at the start and end "
59 "of the line. Lines are extended using the bearing of the first and last segment "
60 "in the line." );
61}
62
63QString QgsExtendLinesAlgorithm::shortDescription() const
64{
65 return QObject::tr( "Extends LineString geometries by extrapolating the start and end segments." );
66}
67
68QList<int> QgsExtendLinesAlgorithm::inputLayerTypes() const
69{
70 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorLine );
71}
72
73Qgis::ProcessingSourceType QgsExtendLinesAlgorithm::outputLayerType() const
74{
76}
77
78QgsExtendLinesAlgorithm *QgsExtendLinesAlgorithm::createInstance() const
79{
80 return new QgsExtendLinesAlgorithm();
81}
82
83void QgsExtendLinesAlgorithm::initParameters( const QVariantMap & )
84{
85 auto startDistance = std::make_unique<QgsProcessingParameterDistance>( u"START_DISTANCE"_s, QObject::tr( "Start distance" ), 0.0, u"INPUT"_s, false, 0 );
86 startDistance->setIsDynamic( true );
87 startDistance->setDynamicPropertyDefinition( QgsPropertyDefinition( u"Start Distance"_s, QObject::tr( "Start distance" ), QgsPropertyDefinition::DoublePositive ) );
88 startDistance->setDynamicLayerParameterName( u"INPUT"_s );
89 addParameter( startDistance.release() );
90
91 auto endDistance = std::make_unique<QgsProcessingParameterDistance>( u"END_DISTANCE"_s, QObject::tr( "End distance" ), 0.0, u"INPUT"_s, false, 0 );
92 endDistance->setIsDynamic( true );
93 endDistance->setDynamicPropertyDefinition( QgsPropertyDefinition( u"End Distance"_s, QObject::tr( "End distance" ), QgsPropertyDefinition::DoublePositive ) );
94 endDistance->setDynamicLayerParameterName( u"INPUT"_s );
95 addParameter( endDistance.release() );
96}
97
98Qgis::ProcessingFeatureSourceFlags QgsExtendLinesAlgorithm::sourceFlags() const
99{
100 // skip geometry checks - this algorithm doesn't care about invalid geometries
102}
103
104bool QgsExtendLinesAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
105{
106 mStartDistance = parameterAsDouble( parameters, u"START_DISTANCE"_s, context );
107 mDynamicStartDistance = QgsProcessingParameters::isDynamic( parameters, u"START_DISTANCE"_s );
108 if ( mDynamicStartDistance )
109 mStartDistanceProperty = parameters.value( u"START_DISTANCE"_s ).value<QgsProperty>();
110
111 mEndDistance = parameterAsDouble( parameters, u"END_DISTANCE"_s, context );
112 mDynamicEndDistance = QgsProcessingParameters::isDynamic( parameters, u"END_DISTANCE"_s );
113 if ( mDynamicEndDistance )
114 mEndDistanceProperty = parameters.value( u"END_DISTANCE"_s ).value<QgsProperty>();
115
116 return true;
117}
118
119QgsFeatureList QgsExtendLinesAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
120{
121 QgsFeature f = feature;
122 if ( f.hasGeometry() )
123 {
124 const QgsGeometry geometry = f.geometry();
125 double startDistance = mStartDistance;
126 if ( mDynamicStartDistance )
127 startDistance = mStartDistanceProperty.valueAsDouble( context.expressionContext(), startDistance );
128
129 double endDistance = mEndDistance;
130 if ( mDynamicEndDistance )
131 endDistance = mEndDistanceProperty.valueAsDouble( context.expressionContext(), endDistance );
132
133 const QgsGeometry outGeometry = geometry.extendLine( startDistance, endDistance );
134 if ( outGeometry.isNull() )
135 throw QgsProcessingException( QObject::tr( "Error calculating extended line" ) ); // don't think this can actually happen!
136
137 f.setGeometry( outGeometry );
138 }
139 return QgsFeatureList() << f;
140}
141
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
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
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: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.
QList< QgsFeature > QgsFeatureList