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