Skip to content

Commit 59ec2e0

Browse files
committedJul 23, 2018
[processing][FEATURE] Port extend lines algorithm to c++, allow dynamic start/end distance
Allows the start and end distance to be data defined
1 parent 8a1964f commit 59ec2e0

File tree

8 files changed

+218
-90
lines changed

8 files changed

+218
-90
lines changed
 

‎python/plugins/processing/algs/help/qgis.yaml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,6 @@ qgis:exportaddgeometrycolumns: >
157157

158158
Depending on the geometry type of the vector layer, the attributes added to the table will be different.
159159

160-
qgis:extendlines: >
161-
This algorithm extends line geometries by a specified amount at the start and end of the line. Lines are extended using the bearing of the first and last segment in the line.
162-
163160
qgis:extractbyattribute: >
164161
This algorithm creates a new vector layer that only contains matching features from an input layer. The criteria for adding features to the resulting layer is defined based on the values of an attribute from the input layer.
165162

‎python/plugins/processing/algs/qgis/ExtendLines.py

Lines changed: 0 additions & 83 deletions
This file was deleted.

‎python/plugins/processing/algs/qgis/QgisAlgorithmProvider.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@
6565
from .EliminateSelection import EliminateSelection
6666
from .ExecuteSQL import ExecuteSQL
6767
from .ExportGeometryInfo import ExportGeometryInfo
68-
from .ExtendLines import ExtendLines
6968
from .ExtentFromLayer import ExtentFromLayer
7069
from .ExtractSpecificVertices import ExtractSpecificVertices
7170
from .FieldPyculator import FieldsPyculator
@@ -182,7 +181,6 @@ def getAlgs(self):
182181
EliminateSelection(),
183182
ExecuteSQL(),
184183
ExportGeometryInfo(),
185-
ExtendLines(),
186184
ExtentFromLayer(),
187185
ExtractSpecificVertices(),
188186
FieldsCalculator(),

‎python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1835,7 +1835,7 @@ tests:
18351835
name: expected/extract_expression.gml
18361836
type: vector
18371837

1838-
- algorithm: qgis:extendlines
1838+
- algorithm: native:extendlines
18391839
name: Extend lines
18401840
params:
18411841
END_DISTANCE: 0.2
@@ -1850,7 +1850,7 @@ tests:
18501850
compare:
18511851
geometry:
18521852
precision: 7
1853-
- algorithm: qgis:extendlines
1853+
- algorithm: native:extendlines
18541854
name: Extend multilines
18551855
params:
18561856
END_DISTANCE: 0.4

‎src/analysis/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ SET(QGIS_ANALYSIS_SRCS
3333
processing/qgsalgorithmdropgeometry.cpp
3434
processing/qgsalgorithmdropmzvalues.cpp
3535
processing/qgsalgorithmexplode.cpp
36+
processing/qgsalgorithmextendlines.cpp
3637
processing/qgsalgorithmextenttolayer.cpp
3738
processing/qgsalgorithmextractbyattribute.cpp
3839
processing/qgsalgorithmextractbyexpression.cpp
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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+
18+
#include "qgsalgorithmextendlines.h"
19+
20+
///@cond PRIVATE
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+
{
71+
return QgsProcessing::TypeVectorLine;
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
99+
return QgsProcessingFeatureSource::FlagSkipGeometryValidityChecks;
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 )
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+
140+
///@endcond
141+
142+
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/***************************************************************************
2+
qgsalgorithmextendlines.h
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+
18+
#ifndef QGSEXTENDLINESALGORITHM_H
19+
#define QGSEXTENDLINESALGORITHM_H
20+
21+
#define SIP_NO_FILE
22+
23+
#include "qgis.h"
24+
#include "qgsprocessingalgorithm.h"
25+
26+
///@cond PRIVATE
27+
28+
/**
29+
* Native extend lines algorithm.
30+
*/
31+
class QgsExtendLinesAlgorithm : public QgsProcessingFeatureBasedAlgorithm
32+
{
33+
34+
public:
35+
36+
QgsExtendLinesAlgorithm() = default;
37+
QString name() const override;
38+
QString displayName() const override;
39+
QStringList tags() const override;
40+
QString group() const override;
41+
QString groupId() const override;
42+
QString shortHelpString() const override;
43+
QString shortDescription() const override;
44+
QList<int> inputLayerTypes() const override;
45+
QgsProcessing::SourceType outputLayerType() const override;
46+
QgsExtendLinesAlgorithm *createInstance() const override SIP_FACTORY;
47+
void initParameters( const QVariantMap &configuration = QVariantMap() ) override;
48+
49+
protected:
50+
QString outputName() const override;
51+
QgsProcessingFeatureSource::Flag sourceFlags() const override;
52+
bool prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;
53+
QgsFeatureList processFeature( const QgsFeature &feature, QgsProcessingContext &, QgsProcessingFeedback *feedback ) override;
54+
55+
private:
56+
57+
double mStartDistance = 0.0;
58+
bool mDynamicStartDistance = false;
59+
QgsProperty mStartDistanceProperty;
60+
61+
double mEndDistance = 0.0;
62+
bool mDynamicEndDistance = false;
63+
QgsProperty mEndDistanceProperty;
64+
};
65+
66+
67+
///@endcond PRIVATE
68+
69+
#endif // QGSEXTENDLINESALGORITHM_H
70+
71+

‎src/analysis/processing/qgsnativealgorithms.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "qgsalgorithmdropgeometry.h"
3131
#include "qgsalgorithmdropmzvalues.h"
3232
#include "qgsalgorithmexplode.h"
33+
#include "qgsalgorithmextendlines.h"
3334
#include "qgsalgorithmextenttolayer.h"
3435
#include "qgsalgorithmextractbyattribute.h"
3536
#include "qgsalgorithmextractbyexpression.h"
@@ -140,6 +141,7 @@ void QgsNativeAlgorithms::loadAlgorithms()
140141
addAlgorithm( new QgsDropGeometryAlgorithm() );
141142
addAlgorithm( new QgsDropMZValuesAlgorithm() );
142143
addAlgorithm( new QgsExplodeAlgorithm() );
144+
addAlgorithm( new QgsExtendLinesAlgorithm() );
143145
addAlgorithm( new QgsExtentToLayerAlgorithm() );
144146
addAlgorithm( new QgsExtractByAttributeAlgorithm() );
145147
addAlgorithm( new QgsExtractByExpressionAlgorithm() );

0 commit comments

Comments
 (0)
Please sign in to comment.