QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 
22 QString QgsExtendLinesAlgorithm::name() const
23 {
24  return QStringLiteral( "extendlines" );
25 }
26 
27 QString QgsExtendLinesAlgorithm::displayName() const
28 {
29  return QObject::tr( "Extend lines" );
30 }
31 
32 QStringList QgsExtendLinesAlgorithm::tags() const
33 {
34  return QObject::tr( "linestring,continue,grow,extrapolate" ).split( ',' );
35 }
36 
37 QString QgsExtendLinesAlgorithm::group() const
38 {
39  return QObject::tr( "Vector geometry" );
40 }
41 
42 QString QgsExtendLinesAlgorithm::groupId() const
43 {
44  return QStringLiteral( "vectorgeometry" );
45 }
46 
47 QString QgsExtendLinesAlgorithm::outputName() const
48 {
49  return QObject::tr( "Extended" );
50 }
51 
52 QString 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 
59 QString QgsExtendLinesAlgorithm::shortDescription() const
60 {
61  return QObject::tr( "Extends LineString geometries by extrapolating the start and end segments." );
62 }
63 
64 QList<int> QgsExtendLinesAlgorithm::inputLayerTypes() const
65 {
66  return QList<int>() << QgsProcessing::TypeVectorLine;
67 }
68 
69 QgsProcessing::SourceType QgsExtendLinesAlgorithm::outputLayerType() const
70 {
72 }
73 
74 QgsExtendLinesAlgorithm *QgsExtendLinesAlgorithm::createInstance() const
75 {
76  return new QgsExtendLinesAlgorithm();
77 }
78 
79 void QgsExtendLinesAlgorithm::initParameters( const QVariantMap & )
80 {
81  std::unique_ptr< QgsProcessingParameterDistance> startDistance = qgis::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 = qgis::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 
96 QgsProcessingFeatureSource::Flag QgsExtendLinesAlgorithm::sourceFlags() const
97 {
98  // skip geometry checks - this algorithm doesn't care about invalid geometries
100 }
101 
102 bool 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 
117 QgsFeatureList 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 
QgsProperty
A store for object properties.
Definition: qgsproperty.h:231
QgsProcessingParameters::isDynamic
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: qgsprocessingparameters.cpp:111
QgsProcessingFeedback
Definition: qgsprocessingfeedback.h:37
QgsFeature::geometry
QgsGeometry geometry
Definition: qgsfeature.h:71
QgsProcessing::TypeVectorLine
@ TypeVectorLine
Vector line layers.
Definition: qgsprocessing.h:49
QgsProcessingFeatureSource::FlagSkipGeometryValidityChecks
@ FlagSkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Definition: qgsprocessingutils.h:475
QgsFeature::setGeometry
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
Definition: qgsfeature.cpp:137
QgsGeometry::extendLine
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...
Definition: qgsgeometry.cpp:2073
QgsProcessingContext
Definition: qgsprocessingcontext.h:43
QgsFeatureList
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:572
QgsPropertyDefinition
Definition for a property.
Definition: qgsproperty.h:47
QgsGeometry::isNull
bool isNull
Definition: qgsgeometry.h:125
QgsProcessingFeatureSource::Flag
Flag
Flags controlling how QgsProcessingFeatureSource fetches features.
Definition: qgsprocessingutils.h:473
QgsGeometry
Definition: qgsgeometry.h:122
qgsalgorithmextendlines.h
QgsFeature::hasGeometry
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:197
QgsProcessingContext::expressionContext
QgsExpressionContext & expressionContext()
Returns the expression context.
Definition: qgsprocessingcontext.h:119
QgsProperty::valueAsDouble
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.
Definition: qgsproperty.cpp:620
QgsFeature
Definition: qgsfeature.h:55
QgsProcessing::SourceType
SourceType
Data source types enum.
Definition: qgsprocessing.h:44
QgsProcessingException
Definition: qgsexception.h:82
QgsPropertyDefinition::DoublePositive
@ DoublePositive
Positive double value (including 0)
Definition: qgsproperty.h:59