QGIS API Documentation 3.41.0-Master (cea29feecf2)
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#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" ), QObject::tr( "Distance" ), 10.0, QStringLiteral( "INPUT" ) );
86 offset->setIsDynamic( true );
87 offset->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "DISTANCE" ), QObject::tr( "Distance" ), QgsPropertyDefinition::Double ) );
88 offset->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
89 addParameter( offset.release() );
90
91 auto segmentParam = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral( "SEGMENTS" ), QObject::tr( "Segments" ), Qgis::ProcessingNumberParameterType::Integer, 8, false, 1 );
92 addParameter( segmentParam.release() );
93
94 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 );
95 addParameter( joinStyleParam.release() );
96
97 auto miterLimitParam = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral( "MITER_LIMIT" ), QObject::tr( "Miter limit" ), Qgis::ProcessingNumberParameterType::Double, 2, false, 1 );
98 addParameter( miterLimitParam.release() );
99}
100
101QList<int> QgsOffsetLinesAlgorithm::inputLayerTypes() const
102{
103 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorLine );
104}
105
106Qgis::ProcessingSourceType QgsOffsetLinesAlgorithm::outputLayerType() const
107{
109}
110
111bool QgsOffsetLinesAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
112{
113 mOffset = parameterAsDouble( parameters, QStringLiteral( "DISTANCE" ), context );
114 mDynamicOffset = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "DISTANCE" ) );
115 if ( mDynamicOffset )
116 mOffsetProperty = parameters.value( QStringLiteral( "DISTANCE" ) ).value<QgsProperty>();
117
118 mSegments = parameterAsInt( parameters, QStringLiteral( "SEGMENTS" ), context );
119 mJoinStyle = static_cast<Qgis::JoinStyle>( 1 + parameterAsInt( parameters, QStringLiteral( "JOIN_STYLE" ), context ) );
120 mMiterLimit = parameterAsDouble( parameters, QStringLiteral( "MITER_LIMIT" ), context );
121
122 return true;
123}
124
125QgsFeatureList QgsOffsetLinesAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
126{
127 if ( feature.hasGeometry() )
128 {
129 QgsFeature f = feature;
130 const QgsGeometry geometry = feature.geometry();
131
132 double offset = mOffset;
133 if ( mDynamicOffset )
134 offset = mOffsetProperty.valueAsDouble( context.expressionContext(), offset );
135
136 const QgsGeometry offsetGeometry = geometry.offsetCurve( offset, mSegments, mJoinStyle, mMiterLimit );
137 f.setGeometry( offsetGeometry );
138 return QgsFeatureList() << f;
139 }
140 else
141 {
142 return QgsFeatureList() << feature;
143 }
144}
145
ProcessingSourceType
Processing data source types.
Definition qgis.h:3315
@ VectorLine
Vector line layers.
JoinStyle
Join styles for buffers.
Definition qgis.h:2013
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