QGIS API Documentation 3.43.0-Master (e01d6d7c4c0)
qgsalgorithmtaperedbuffer.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmtaperedbuffer.cpp
3 ---------------------
4 begin : March 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 QgsTaperedBufferAlgorithm::name() const
23{
24 return QStringLiteral( "taperedbuffer" );
25}
26
27QString QgsTaperedBufferAlgorithm::displayName() const
28{
29 return QObject::tr( "Tapered buffers" );
30}
31
32QStringList QgsTaperedBufferAlgorithm::tags() const
33{
34 return QObject::tr( "variable,distance,length,line,buffer" ).split( ',' );
35}
36
37QString QgsTaperedBufferAlgorithm::group() const
38{
39 return QObject::tr( "Vector geometry" );
40}
41
42QString QgsTaperedBufferAlgorithm::groupId() const
43{
44 return QStringLiteral( "vectorgeometry" );
45}
46
47QString QgsTaperedBufferAlgorithm::outputName() const
48{
49 return QObject::tr( "Buffered" );
50}
51
52Qgis::ProcessingSourceType QgsTaperedBufferAlgorithm::outputLayerType() const
53{
55}
56
57Qgis::WkbType QgsTaperedBufferAlgorithm::outputWkbType( Qgis::WkbType ) const
58{
60}
61
62bool QgsTaperedBufferAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
63{
64 mStartWidth = parameterAsDouble( parameters, QStringLiteral( "START_WIDTH" ), context );
65 mDynamicStartWidth = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "START_WIDTH" ) );
66 if ( mDynamicStartWidth )
67 mStartWidthProperty = parameters.value( QStringLiteral( "START_WIDTH" ) ).value<QgsProperty>();
68
69 mEndWidth = parameterAsDouble( parameters, QStringLiteral( "END_WIDTH" ), context );
70 mDynamicEndWidth = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "END_WIDTH" ) );
71 if ( mDynamicEndWidth )
72 mEndWidthProperty = parameters.value( QStringLiteral( "END_WIDTH" ) ).value<QgsProperty>();
73
74 mSegments = parameterAsInt( parameters, QStringLiteral( "Segments" ), context );
75 mDynamicSegments = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "Segments" ) );
76 if ( mDynamicSegments )
77 mSegmentsProperty = parameters.value( QStringLiteral( "Segments" ) ).value<QgsProperty>();
78
79 return true;
80}
81
82QString QgsTaperedBufferAlgorithm::shortHelpString() const
83{
84 return QObject::tr( "This algorithm creates tapered buffers along line geometries, using a specified start and "
85 "end buffer diameter corresponding to the buffer diameter at the start and end of the linestrings." );
86}
87
88QString QgsTaperedBufferAlgorithm::shortDescription() const
89{
90 return QObject::tr( "Creates tapered buffers along line geometries." );
91}
92
93QList<int> QgsTaperedBufferAlgorithm::inputLayerTypes() const
94{
95 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorLine );
96}
97
98QgsTaperedBufferAlgorithm *QgsTaperedBufferAlgorithm::createInstance() const
99{
100 return new QgsTaperedBufferAlgorithm();
101}
102
103void QgsTaperedBufferAlgorithm::initParameters( const QVariantMap & )
104{
105 auto startWidth = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral( "START_WIDTH" ), QObject::tr( "Start width" ), Qgis::ProcessingNumberParameterType::Double, 0.0, false, 0.0 );
106 startWidth->setIsDynamic( true );
107 startWidth->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "START_WIDTH" ), QObject::tr( "Start width" ), QgsPropertyDefinition::DoublePositive ) );
108 startWidth->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
109 addParameter( startWidth.release() );
110
111 auto endWidth = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral( "END_WIDTH" ), QObject::tr( "End width" ), Qgis::ProcessingNumberParameterType::Double, 1, false, 0.0 );
112 endWidth->setIsDynamic( true );
113 endWidth->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "END_WIDTH" ), QObject::tr( "End width" ), QgsPropertyDefinition::DoublePositive ) );
114 endWidth->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
115 addParameter( endWidth.release() );
116
117 auto segments = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral( "SEGMENTS" ), QObject::tr( "Segments" ), Qgis::ProcessingNumberParameterType::Integer, 16, false, 1 );
118 segments->setIsDynamic( true );
119 segments->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "SEGMENTS" ), QObject::tr( "Segments" ), QgsPropertyDefinition::IntegerPositiveGreaterZero ) );
120 segments->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
121 addParameter( segments.release() );
122}
123
124QgsFeatureList QgsTaperedBufferAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
125{
126 if ( !feature.hasGeometry() )
127 return QgsFeatureList() << feature;
128
129 QgsFeature f = feature;
130 int segments = mSegments;
131 if ( mDynamicSegments )
132 segments = mSegmentsProperty.valueAsInt( context.expressionContext(), segments );
133
134 double startWidth = mStartWidth;
135 if ( mDynamicStartWidth )
136 startWidth = mStartWidthProperty.valueAsDouble( context.expressionContext(), startWidth );
137
138 double endWidth = mEndWidth;
139 if ( mDynamicEndWidth )
140 endWidth = mEndWidthProperty.valueAsDouble( context.expressionContext(), endWidth );
141
142 const QgsGeometry outputGeometry = feature.geometry().taperedBuffer( startWidth, endWidth, segments );
143 if ( outputGeometry.isNull() )
144 {
145 feedback->reportError( QObject::tr( "Error buffering geometry %1: %2" ).arg( feature.id() ).arg( outputGeometry.lastError() ) );
146 }
147 f.setGeometry( outputGeometry );
148
149 return QgsFeatureList() << f;
150}
151
152
153QString QgsVariableWidthBufferByMAlgorithm::name() const
154{
155 return QStringLiteral( "bufferbym" );
156}
157
158QString QgsVariableWidthBufferByMAlgorithm::displayName() const
159{
160 return QObject::tr( "Variable width buffer (by M value)" );
161}
162
163QStringList QgsVariableWidthBufferByMAlgorithm::tags() const
164{
165 return QObject::tr( "variable,distance,length,line,buffer" ).split( ',' );
166}
167
168QString QgsVariableWidthBufferByMAlgorithm::group() const
169{
170 return QObject::tr( "Vector geometry" );
171}
172
173QString QgsVariableWidthBufferByMAlgorithm::groupId() const
174{
175 return QStringLiteral( "vectorgeometry" );
176}
177
178QString QgsVariableWidthBufferByMAlgorithm::outputName() const
179{
180 return QObject::tr( "Buffered" );
181}
182
183Qgis::ProcessingSourceType QgsVariableWidthBufferByMAlgorithm::outputLayerType() const
184{
186}
187
188Qgis::WkbType QgsVariableWidthBufferByMAlgorithm::outputWkbType( Qgis::WkbType ) const
189{
191}
192
193bool QgsVariableWidthBufferByMAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
194{
195 mSegments = parameterAsInt( parameters, QStringLiteral( "Segments" ), context );
196 mDynamicSegments = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "Segments" ) );
197 if ( mDynamicSegments )
198 mSegmentsProperty = parameters.value( QStringLiteral( "Segments" ) ).value<QgsProperty>();
199
200 return true;
201}
202
203QString QgsVariableWidthBufferByMAlgorithm::shortHelpString() const
204{
205 return QObject::tr( "This algorithm creates variable width buffers along lines, using the M value of the line geometries "
206 "as the diameter of the buffer at each vertex." );
207}
208
209QString QgsVariableWidthBufferByMAlgorithm::shortDescription() const
210{
211 return QObject::tr( "Creates variable width buffers along lines, using the M value of the line geometries "
212 "as the diameter of the buffer at each vertex." );
213}
214
215QList<int> QgsVariableWidthBufferByMAlgorithm::inputLayerTypes() const
216{
217 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorLine );
218}
219
220QgsVariableWidthBufferByMAlgorithm *QgsVariableWidthBufferByMAlgorithm::createInstance() const
221{
222 return new QgsVariableWidthBufferByMAlgorithm();
223}
224
225void QgsVariableWidthBufferByMAlgorithm::initParameters( const QVariantMap & )
226{
227 auto segments = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral( "SEGMENTS" ), QObject::tr( "Segments" ), Qgis::ProcessingNumberParameterType::Integer, 16, false, 1 );
228 segments->setIsDynamic( true );
229 segments->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "SEGMENTS" ), QObject::tr( "Segments" ), QgsPropertyDefinition::IntegerPositiveGreaterZero ) );
230 segments->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
231 addParameter( segments.release() );
232}
233
234QgsFeatureList QgsVariableWidthBufferByMAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
235{
236 if ( !feature.hasGeometry() )
237 return QgsFeatureList() << feature;
238
239 QgsFeature f = feature;
240 int segments = mSegments;
241 if ( mDynamicSegments )
242 segments = mSegmentsProperty.valueAsInt( context.expressionContext(), segments );
243
244 const QgsGeometry outputGeometry = feature.geometry().variableWidthBufferByM( segments );
245 if ( outputGeometry.isNull() )
246 {
247 feedback->reportError( QObject::tr( "Error buffering geometry %1: %2" ).arg( feature.id() ).arg( outputGeometry.lastError() ) );
248 }
249 f.setGeometry( outputGeometry );
250
251 return QgsFeatureList() << f;
252}
ProcessingSourceType
Processing data source types.
Definition qgis.h:3399
@ VectorPolygon
Vector polygon layers.
@ VectorLine
Vector line layers.
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:256
@ MultiPolygon
MultiPolygon.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
QgsFeatureId id
Definition qgsfeature.h:66
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.
QString lastError() const
Returns an error string referring to the last error encountered either when this geometry was created...
QgsGeometry variableWidthBufferByM(int segments) const
Calculates a variable width buffer for a (multi)linestring geometry, where the width at each node is ...
QgsGeometry taperedBuffer(double startWidth, double endWidth, int segments) const
Calculates a variable width buffer ("tapered buffer") for a (multi)curve geometry.
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.
virtual void reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.
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
@ IntegerPositiveGreaterZero
Non-zero positive integer values.
Definition qgsproperty.h:54
@ DoublePositive
Positive double value (including 0)
Definition qgsproperty.h:56
A store for object properties.
double valueAsDouble(const QgsExpressionContext &context, double defaultValue=0.0, bool *ok=nullptr) const
Calculates the current value of the property and interprets it as a double.
int valueAsInt(const QgsExpressionContext &context, int defaultValue=0, bool *ok=nullptr) const
Calculates the current value of the property and interprets it as an integer.
QList< QgsFeature > QgsFeatureList