QGIS API Documentation 3.41.0-Master (af5edcb665c)
Loading...
Searching...
No Matches
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
88QList<int> QgsTaperedBufferAlgorithm::inputLayerTypes() const
89{
90 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorLine );
91}
92
93QgsTaperedBufferAlgorithm *QgsTaperedBufferAlgorithm::createInstance() const
94{
95 return new QgsTaperedBufferAlgorithm();
96}
97
98void QgsTaperedBufferAlgorithm::initParameters( const QVariantMap & )
99{
100 std::unique_ptr<QgsProcessingParameterNumber> startWidth = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral( "START_WIDTH" ), QObject::tr( "Start width" ), Qgis::ProcessingNumberParameterType::Double, 0.0, false, 0.0 );
101 startWidth->setIsDynamic( true );
102 startWidth->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "START_WIDTH" ), QObject::tr( "Start width" ), QgsPropertyDefinition::DoublePositive ) );
103 startWidth->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
104 addParameter( startWidth.release() );
105
106 std::unique_ptr<QgsProcessingParameterNumber> endWidth = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral( "END_WIDTH" ), QObject::tr( "End width" ), Qgis::ProcessingNumberParameterType::Double, 1, false, 0.0 );
107 endWidth->setIsDynamic( true );
108 endWidth->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "END_WIDTH" ), QObject::tr( "End width" ), QgsPropertyDefinition::DoublePositive ) );
109 endWidth->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
110 addParameter( endWidth.release() );
111
112 std::unique_ptr<QgsProcessingParameterNumber> segments = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral( "SEGMENTS" ), QObject::tr( "Segments" ), Qgis::ProcessingNumberParameterType::Integer, 16, false, 1 );
113 segments->setIsDynamic( true );
114 segments->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "SEGMENTS" ), QObject::tr( "Segments" ), QgsPropertyDefinition::IntegerPositiveGreaterZero ) );
115 segments->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
116 addParameter( segments.release() );
117}
118
119QgsFeatureList QgsTaperedBufferAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
120{
121 if ( !feature.hasGeometry() )
122 return QgsFeatureList() << feature;
123
124 QgsFeature f = feature;
125 int segments = mSegments;
126 if ( mDynamicSegments )
127 segments = mSegmentsProperty.valueAsInt( context.expressionContext(), segments );
128
129 double startWidth = mStartWidth;
130 if ( mDynamicStartWidth )
131 startWidth = mStartWidthProperty.valueAsDouble( context.expressionContext(), startWidth );
132
133 double endWidth = mEndWidth;
134 if ( mDynamicEndWidth )
135 endWidth = mEndWidthProperty.valueAsDouble( context.expressionContext(), endWidth );
136
137 const QgsGeometry outputGeometry = feature.geometry().taperedBuffer( startWidth, endWidth, segments );
138 if ( outputGeometry.isNull() )
139 {
140 feedback->reportError( QObject::tr( "Error buffering geometry %1: %2" ).arg( feature.id() ).arg( outputGeometry.lastError() ) );
141 }
142 f.setGeometry( outputGeometry );
143
144 return QgsFeatureList() << f;
145}
146
147
148QString QgsVariableWidthBufferByMAlgorithm::name() const
149{
150 return QStringLiteral( "bufferbym" );
151}
152
153QString QgsVariableWidthBufferByMAlgorithm::displayName() const
154{
155 return QObject::tr( "Variable width buffer (by M value)" );
156}
157
158QStringList QgsVariableWidthBufferByMAlgorithm::tags() const
159{
160 return QObject::tr( "variable,distance,length,line,buffer" ).split( ',' );
161}
162
163QString QgsVariableWidthBufferByMAlgorithm::group() const
164{
165 return QObject::tr( "Vector geometry" );
166}
167
168QString QgsVariableWidthBufferByMAlgorithm::groupId() const
169{
170 return QStringLiteral( "vectorgeometry" );
171}
172
173QString QgsVariableWidthBufferByMAlgorithm::outputName() const
174{
175 return QObject::tr( "Buffered" );
176}
177
178Qgis::ProcessingSourceType QgsVariableWidthBufferByMAlgorithm::outputLayerType() const
179{
181}
182
183Qgis::WkbType QgsVariableWidthBufferByMAlgorithm::outputWkbType( Qgis::WkbType ) const
184{
186}
187
188bool QgsVariableWidthBufferByMAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
189{
190 mSegments = parameterAsInt( parameters, QStringLiteral( "Segments" ), context );
191 mDynamicSegments = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "Segments" ) );
192 if ( mDynamicSegments )
193 mSegmentsProperty = parameters.value( QStringLiteral( "Segments" ) ).value<QgsProperty>();
194
195 return true;
196}
197
198QString QgsVariableWidthBufferByMAlgorithm::shortHelpString() const
199{
200 return QObject::tr( "This algorithm creates variable width buffers along lines, using the M value of the line geometries "
201 "as the diameter of the buffer at each vertex." );
202}
203
204QList<int> QgsVariableWidthBufferByMAlgorithm::inputLayerTypes() const
205{
206 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorLine );
207}
208
209QgsVariableWidthBufferByMAlgorithm *QgsVariableWidthBufferByMAlgorithm::createInstance() const
210{
211 return new QgsVariableWidthBufferByMAlgorithm();
212}
213
214void QgsVariableWidthBufferByMAlgorithm::initParameters( const QVariantMap & )
215{
216 std::unique_ptr<QgsProcessingParameterNumber> segments = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral( "SEGMENTS" ), QObject::tr( "Segments" ), Qgis::ProcessingNumberParameterType::Integer, 16, false, 1 );
217 segments->setIsDynamic( true );
218 segments->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "SEGMENTS" ), QObject::tr( "Segments" ), QgsPropertyDefinition::IntegerPositiveGreaterZero ) );
219 segments->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
220 addParameter( segments.release() );
221}
222
223QgsFeatureList QgsVariableWidthBufferByMAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
224{
225 if ( !feature.hasGeometry() )
226 return QgsFeatureList() << feature;
227
228 QgsFeature f = feature;
229 int segments = mSegments;
230 if ( mDynamicSegments )
231 segments = mSegmentsProperty.valueAsInt( context.expressionContext(), segments );
232
233 const QgsGeometry outputGeometry = feature.geometry().variableWidthBufferByM( segments );
234 if ( outputGeometry.isNull() )
235 {
236 feedback->reportError( QObject::tr( "Error buffering geometry %1: %2" ).arg( feature.id() ).arg( outputGeometry.lastError() ) );
237 }
238 f.setGeometry( outputGeometry );
239
240 return QgsFeatureList() << f;
241}
ProcessingSourceType
Processing data source types.
Definition qgis.h:3333
@ 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