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