QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
qgsalgorithmoffsetlines.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmoffsetlines.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#include "qgsapplication.h"
20
22
23QString QgsOffsetLinesAlgorithm::name() const
24{
25 return QStringLiteral( "offsetline" );
26}
27
28QString QgsOffsetLinesAlgorithm::displayName() const
29{
30 return QObject::tr( "Offset lines" );
31}
32
33QStringList QgsOffsetLinesAlgorithm::tags() const
34{
35 return QObject::tr( "offset,linestring" ).split( ',' );
36}
37
38QString QgsOffsetLinesAlgorithm::group() const
39{
40 return QObject::tr( "Vector geometry" );
41}
42
43QString QgsOffsetLinesAlgorithm::groupId() const
44{
45 return QStringLiteral( "vectorgeometry" );
46}
47
48QString QgsOffsetLinesAlgorithm::outputName() const
49{
50 return QObject::tr( "Offset" );
51}
52
53QString QgsOffsetLinesAlgorithm::shortHelpString() const
54{
55 return QObject::tr( "This algorithm offsets lines by a specified distance. Positive distances will offset lines to the left, and negative distances "
56 "will offset to the right of lines.\n\n"
57 "The segments parameter controls the number of line segments to use to approximate a quarter circle when creating rounded offsets.\n\n"
58 "The join style parameter specifies whether round, miter or beveled joins should be used when offsetting corners in a line.\n\n"
59 "The miter limit parameter is only applicable for miter join styles, and controls the maximum distance from the offset curve to "
60 "use when creating a mitered join." );
61}
62
63QString QgsOffsetLinesAlgorithm::shortDescription() const
64{
65 return QObject::tr( "Offsets lines by a specified distance." );
66}
67
68QIcon QgsOffsetLinesAlgorithm::icon() const
69{
70 return QgsApplication::getThemeIcon( QStringLiteral( "/algorithms/mAlgorithmOffsetLines.svg" ) );
71}
72
73QString QgsOffsetLinesAlgorithm::svgIconPath() const
74{
75 return QgsApplication::iconPath( QStringLiteral( "/algorithms/mAlgorithmOffsetLines.svg" ) );
76}
77
78QgsOffsetLinesAlgorithm *QgsOffsetLinesAlgorithm::createInstance() const
79{
80 return new QgsOffsetLinesAlgorithm();
81}
82
83void QgsOffsetLinesAlgorithm::initParameters( const QVariantMap & )
84{
85 std::unique_ptr< QgsProcessingParameterDistance > offset = std::make_unique< QgsProcessingParameterDistance >( QStringLiteral( "DISTANCE" ),
86 QObject::tr( "Distance" ),
87 10.0, QStringLiteral( "INPUT" ) );
88 offset->setIsDynamic( true );
89 offset->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "DISTANCE" ), QObject::tr( "Distance" ), QgsPropertyDefinition::Double ) );
90 offset->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
91 addParameter( offset.release() );
92
93 auto segmentParam = std::make_unique< QgsProcessingParameterNumber >( QStringLiteral( "SEGMENTS" ), QObject::tr( "Segments" ), Qgis::ProcessingNumberParameterType::Integer, 8, false, 1 );
94 addParameter( segmentParam.release() );
95
96 auto joinStyleParam = std::make_unique< QgsProcessingParameterEnum>( QStringLiteral( "JOIN_STYLE" ), QObject::tr( "Join style" ), QStringList() << QObject::tr( "Round" ) << QObject::tr( "Miter" ) << QObject::tr( "Bevel" ), false, 0 );
97 addParameter( joinStyleParam.release() );
98
99 auto miterLimitParam = std::make_unique< QgsProcessingParameterNumber >( QStringLiteral( "MITER_LIMIT" ), QObject::tr( "Miter limit" ), Qgis::ProcessingNumberParameterType::Double, 2, false, 1 );
100 addParameter( miterLimitParam.release() );
101}
102
103QList<int> QgsOffsetLinesAlgorithm::inputLayerTypes() const
104{
105 return QList< int >() << static_cast< int >( Qgis::ProcessingSourceType::VectorLine );
106}
107
108Qgis::ProcessingSourceType QgsOffsetLinesAlgorithm::outputLayerType() const
109{
111}
112
113bool QgsOffsetLinesAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
114{
115 mOffset = parameterAsDouble( parameters, QStringLiteral( "DISTANCE" ), context );
116 mDynamicOffset = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "DISTANCE" ) );
117 if ( mDynamicOffset )
118 mOffsetProperty = parameters.value( QStringLiteral( "DISTANCE" ) ).value< QgsProperty >();
119
120 mSegments = parameterAsInt( parameters, QStringLiteral( "SEGMENTS" ), context );
121 mJoinStyle = static_cast< Qgis::JoinStyle>( 1 + parameterAsInt( parameters, QStringLiteral( "JOIN_STYLE" ), context ) );
122 mMiterLimit = parameterAsDouble( parameters, QStringLiteral( "MITER_LIMIT" ), context );
123
124 return true;
125}
126
127QgsFeatureList QgsOffsetLinesAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
128{
129 if ( feature.hasGeometry() )
130 {
131 QgsFeature f = feature;
132 const QgsGeometry geometry = feature.geometry();
133
134 double offset = mOffset;
135 if ( mDynamicOffset )
136 offset = mOffsetProperty.valueAsDouble( context.expressionContext(), offset );
137
138 const QgsGeometry offsetGeometry = geometry.offsetCurve( offset, mSegments, mJoinStyle, mMiterLimit );
139 f.setGeometry( offsetGeometry );
140 return QgsFeatureList() << f;
141 }
142 else
143 {
144 return QgsFeatureList() << feature;
145 }
146}
147
149
150
ProcessingSourceType
Processing data source types.
Definition: qgis.h:2858
@ VectorLine
Vector line layers.
JoinStyle
Join styles for buffers.
Definition: qgis.h:1694
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
static QString iconPath(const QString &iconFile)
Returns path to the desired icon file.
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.
Definition: qgsfeature.cpp:230
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
Definition: qgsfeature.cpp:167
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:162
QgsGeometry offsetCurve(double distance, int segments, Qgis::JoinStyle joinStyle, double miterLimit) const
Returns an offset line at a given distance and side from an input line.
Contains information about the context in which a processing algorithm is executed.
QgsExpressionContext & expressionContext()
Returns the expression context.
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:45
@ Double
Double value (including negative values)
Definition: qgsproperty.h:55
A store for object properties.
Definition: qgsproperty.h:228
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:917