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