QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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(
59 "This algorithm extends line geometries by a specified amount at the start and end "
60 "of the line. Lines are extended using the bearing of the first and last segment "
61 "in the line."
62 );
63}
64
65QString QgsExtendLinesAlgorithm::shortDescription() const
66{
67 return QObject::tr( "Extends LineString geometries by extrapolating the start and end segments." );
68}
69
70QList<int> QgsExtendLinesAlgorithm::inputLayerTypes() const
71{
72 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorLine );
73}
74
75Qgis::ProcessingSourceType QgsExtendLinesAlgorithm::outputLayerType() const
76{
78}
79
80QgsExtendLinesAlgorithm *QgsExtendLinesAlgorithm::createInstance() const
81{
82 return new QgsExtendLinesAlgorithm();
83}
84
85void QgsExtendLinesAlgorithm::initParameters( const QVariantMap & )
86{
87 auto startDistance = std::make_unique<QgsProcessingParameterDistance>( u"START_DISTANCE"_s, QObject::tr( "Start distance" ), 0.0, u"INPUT"_s, false, 0 );
88 startDistance->setIsDynamic( true );
89 startDistance->setDynamicPropertyDefinition( QgsPropertyDefinition( u"Start Distance"_s, QObject::tr( "Start distance" ), QgsPropertyDefinition::DoublePositive ) );
90 startDistance->setDynamicLayerParameterName( u"INPUT"_s );
91 addParameter( startDistance.release() );
92
93 auto endDistance = std::make_unique<QgsProcessingParameterDistance>( u"END_DISTANCE"_s, QObject::tr( "End distance" ), 0.0, u"INPUT"_s, false, 0 );
94 endDistance->setIsDynamic( true );
95 endDistance->setDynamicPropertyDefinition( QgsPropertyDefinition( u"End Distance"_s, QObject::tr( "End distance" ), QgsPropertyDefinition::DoublePositive ) );
96 endDistance->setDynamicLayerParameterName( u"INPUT"_s );
97 addParameter( endDistance.release() );
98}
99
100Qgis::ProcessingFeatureSourceFlags QgsExtendLinesAlgorithm::sourceFlags() const
101{
102 // skip geometry checks - this algorithm doesn't care about invalid geometries
104}
105
106bool QgsExtendLinesAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
107{
108 mStartDistance = parameterAsDouble( parameters, u"START_DISTANCE"_s, context );
109 mDynamicStartDistance = QgsProcessingParameters::isDynamic( parameters, u"START_DISTANCE"_s );
110 if ( mDynamicStartDistance )
111 mStartDistanceProperty = parameters.value( u"START_DISTANCE"_s ).value<QgsProperty>();
112
113 mEndDistance = parameterAsDouble( parameters, u"END_DISTANCE"_s, context );
114 mDynamicEndDistance = QgsProcessingParameters::isDynamic( parameters, u"END_DISTANCE"_s );
115 if ( mDynamicEndDistance )
116 mEndDistanceProperty = parameters.value( u"END_DISTANCE"_s ).value<QgsProperty>();
117
118 return true;
119}
120
121QgsFeatureList QgsExtendLinesAlgorithm::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 QgsGeometry outGeometry = geometry.extendLine( startDistance, endDistance );
136 if ( outGeometry.isNull() )
137 throw QgsProcessingException( QObject::tr( "Error calculating extended line" ) ); // don't think this can actually happen!
138
139 f.setGeometry( outGeometry );
140 }
141 return QgsFeatureList() << f;
142}
143
ProcessingSourceType
Processing data source types.
Definition qgis.h:3645
@ VectorLine
Vector line layers.
Definition qgis.h:3649
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Definition qgis.h:3828
QFlags< ProcessingFeatureSourceFlag > ProcessingFeatureSourceFlags
Flags which control how QgsProcessingFeatureSource fetches features.
Definition qgis.h:3839
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: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