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