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