QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 
22 QString QgsTaperedBufferAlgorithm::name() const
23 {
24  return QStringLiteral( "taperedbuffer" );
25 }
26 
27 QString QgsTaperedBufferAlgorithm::displayName() const
28 {
29  return QObject::tr( "Tapered buffers" );
30 }
31 
32 QStringList QgsTaperedBufferAlgorithm::tags() const
33 {
34  return QObject::tr( "variable,distance,length,line,buffer" ).split( ',' );
35 }
36 
37 QString QgsTaperedBufferAlgorithm::group() const
38 {
39  return QObject::tr( "Vector geometry" );
40 }
41 
42 QString QgsTaperedBufferAlgorithm::groupId() const
43 {
44  return QStringLiteral( "vectorgeometry" );
45 }
46 
47 QString QgsTaperedBufferAlgorithm::outputName() const
48 {
49  return QObject::tr( "Buffered" );
50 }
51 
52 QgsProcessing::SourceType QgsTaperedBufferAlgorithm::outputLayerType() const
53 {
55 }
56 
57 QgsWkbTypes::Type QgsTaperedBufferAlgorithm::outputWkbType( QgsWkbTypes::Type ) const
58 {
60 }
61 
62 bool 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 
82 QString 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 
88 QList<int> QgsTaperedBufferAlgorithm::inputLayerTypes() const
89 {
90  return QList<int>() << QgsProcessing::TypeVectorLine;
91 }
92 
93 QgsTaperedBufferAlgorithm *QgsTaperedBufferAlgorithm::createInstance() const
94 {
95  return new QgsTaperedBufferAlgorithm();
96 }
97 
98 void QgsTaperedBufferAlgorithm::initParameters( const QVariantMap & )
99 {
100  std::unique_ptr< QgsProcessingParameterNumber > startWidth = qgis::make_unique< QgsProcessingParameterNumber >( QStringLiteral( "START_WIDTH" ),
101  QObject::tr( "Start width" ), QgsProcessingParameterNumber::Double,
102  0.0, false, 0.0 );
103  startWidth->setIsDynamic( true );
104  startWidth->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "START_WIDTH" ), QObject::tr( "Start width" ), QgsPropertyDefinition::DoublePositive ) );
105  startWidth->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
106  addParameter( startWidth.release() );
107 
108  std::unique_ptr< QgsProcessingParameterNumber > endWidth = qgis::make_unique< QgsProcessingParameterNumber >( QStringLiteral( "END_WIDTH" ),
109  QObject::tr( "End width" ), QgsProcessingParameterNumber::Double,
110  1, false, 0.0 );
111  endWidth->setIsDynamic( true );
112  endWidth->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "END_WIDTH" ), QObject::tr( "End width" ), QgsPropertyDefinition::DoublePositive ) );
113  endWidth->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
114  addParameter( endWidth.release() );
115 
116  std::unique_ptr< QgsProcessingParameterNumber > segments = qgis::make_unique< QgsProcessingParameterNumber >( QStringLiteral( "SEGMENTS" ),
117  QObject::tr( "Segments" ), QgsProcessingParameterNumber::Integer,
118  16, false, 1 );
119  segments->setIsDynamic( true );
120  segments->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "SEGMENTS" ), QObject::tr( "Segments" ), QgsPropertyDefinition::IntegerPositiveGreaterZero ) );
121  segments->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
122  addParameter( segments.release() );
123 }
124 
125 QgsFeatureList QgsTaperedBufferAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
126 {
127  if ( !feature.hasGeometry() )
128  return QgsFeatureList() << feature;
129 
130  QgsFeature f = feature;
131  int segments = mSegments;
132  if ( mDynamicSegments )
133  segments = mSegmentsProperty.valueAsInt( context.expressionContext(), segments );
134 
135  double startWidth = mStartWidth;
136  if ( mDynamicStartWidth )
137  startWidth = mStartWidthProperty.valueAsDouble( context.expressionContext(), startWidth );
138 
139  double endWidth = mEndWidth;
140  if ( mDynamicEndWidth )
141  endWidth = mEndWidthProperty.valueAsDouble( context.expressionContext(), endWidth );
142 
143  QgsGeometry outputGeometry = feature.geometry().taperedBuffer( startWidth, endWidth, segments );
144  if ( outputGeometry.isNull() )
145  {
146  feedback->reportError( QObject::tr( "Error buffering geometry %1: %2" ).arg( feature.id() ).arg( outputGeometry.lastError() ) );
147  }
148  f.setGeometry( outputGeometry );
149 
150  return QgsFeatureList() << f;
151 }
152 
153 
154 
155 
156 QString QgsVariableWidthBufferByMAlgorithm::name() const
157 {
158  return QStringLiteral( "bufferbym" );
159 }
160 
161 QString QgsVariableWidthBufferByMAlgorithm::displayName() const
162 {
163  return QObject::tr( "Variable width buffer (by M value)" );
164 }
165 
166 QStringList QgsVariableWidthBufferByMAlgorithm::tags() const
167 {
168  return QObject::tr( "variable,distance,length,line,buffer" ).split( ',' );
169 }
170 
171 QString QgsVariableWidthBufferByMAlgorithm::group() const
172 {
173  return QObject::tr( "Vector geometry" );
174 }
175 
176 QString QgsVariableWidthBufferByMAlgorithm::groupId() const
177 {
178  return QStringLiteral( "vectorgeometry" );
179 }
180 
181 QString QgsVariableWidthBufferByMAlgorithm::outputName() const
182 {
183  return QObject::tr( "Buffered" );
184 }
185 
186 QgsProcessing::SourceType QgsVariableWidthBufferByMAlgorithm::outputLayerType() const
187 {
189 }
190 
191 QgsWkbTypes::Type QgsVariableWidthBufferByMAlgorithm::outputWkbType( QgsWkbTypes::Type ) const
192 {
194 }
195 
196 bool QgsVariableWidthBufferByMAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
197 {
198  mSegments = parameterAsInt( parameters, QStringLiteral( "Segments" ), context );
199  mDynamicSegments = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "Segments" ) );
200  if ( mDynamicSegments )
201  mSegmentsProperty = parameters.value( QStringLiteral( "Segments" ) ).value< QgsProperty >();
202 
203  return true;
204 }
205 
206 QString QgsVariableWidthBufferByMAlgorithm::shortHelpString() const
207 {
208  return QObject::tr( "This algorithm creates variable width buffers along lines, using the M value of the line geometries "
209  "as the diameter of the buffer at each vertex." );
210 }
211 
212 QList<int> QgsVariableWidthBufferByMAlgorithm::inputLayerTypes() const
213 {
214  return QList<int>() << QgsProcessing::TypeVectorLine;
215 }
216 
217 QgsVariableWidthBufferByMAlgorithm *QgsVariableWidthBufferByMAlgorithm::createInstance() const
218 {
219  return new QgsVariableWidthBufferByMAlgorithm();
220 }
221 
222 void QgsVariableWidthBufferByMAlgorithm::initParameters( const QVariantMap & )
223 {
224  std::unique_ptr< QgsProcessingParameterNumber > segments = qgis::make_unique< QgsProcessingParameterNumber >( QStringLiteral( "SEGMENTS" ),
225  QObject::tr( "Segments" ), QgsProcessingParameterNumber::Integer,
226  16, false, 1 );
227  segments->setIsDynamic( true );
228  segments->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "SEGMENTS" ), QObject::tr( "Segments" ), QgsPropertyDefinition::IntegerPositiveGreaterZero ) );
229  segments->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
230  addParameter( segments.release() );
231 }
232 
233 QgsFeatureList QgsVariableWidthBufferByMAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
234 {
235  if ( !feature.hasGeometry() )
236  return QgsFeatureList() << feature;
237 
238  QgsFeature f = feature;
239  int segments = mSegments;
240  if ( mDynamicSegments )
241  segments = mSegmentsProperty.valueAsInt( context.expressionContext(), segments );
242 
243  QgsGeometry outputGeometry = feature.geometry().variableWidthBufferByM( segments );
244  if ( outputGeometry.isNull() )
245  {
246  feedback->reportError( QObject::tr( "Error buffering geometry %1: %2" ).arg( feature.id() ).arg( outputGeometry.lastError() ) );
247  }
248  f.setGeometry( outputGeometry );
249 
250  return QgsFeatureList() << f;
251 }
253 
254 
QgsProcessingParameterNumber::Double
@ Double
Double/float values.
Definition: qgsprocessingparameters.h:1846
QgsProperty
A store for object properties.
Definition: qgsproperty.h:231
QgsWkbTypes::MultiPolygon
@ MultiPolygon
Definition: qgswkbtypes.h:77
QgsProcessingParameters::isDynamic
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: qgsprocessingparameters.cpp:111
QgsProcessingFeedback
Definition: qgsprocessingfeedback.h:37
QgsProcessingFeedback::reportError
virtual void reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.
Definition: qgsprocessingfeedback.cpp:39
QgsProcessing::TypeVectorPolygon
@ TypeVectorPolygon
Vector polygon layers.
Definition: qgsprocessing.h:50
QgsPropertyDefinition::IntegerPositiveGreaterZero
@ IntegerPositiveGreaterZero
Non-zero positive integer values.
Definition: qgsproperty.h:57
QgsFeature::geometry
QgsGeometry geometry
Definition: qgsfeature.h:71
QgsProcessing::TypeVectorLine
@ TypeVectorLine
Vector line layers.
Definition: qgsprocessing.h:49
QgsWkbTypes::Type
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:68
QgsFeature::id
QgsFeatureId id
Definition: qgsfeature.h:68
QgsFeature::setGeometry
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
Definition: qgsfeature.cpp:137
QgsProcessingContext
Definition: qgsprocessingcontext.h:43
QgsGeometry::variableWidthBufferByM
QgsGeometry variableWidthBufferByM(int segments) const
Calculates a variable width buffer for a (multi)linestring geometry, where the width at each node is ...
Definition: qgsgeometry.cpp:2066
QgsGeometry::taperedBuffer
QgsGeometry taperedBuffer(double startWidth, double endWidth, int segments) const
Calculates a variable width buffer ("tapered buffer") for a (multi)curve geometry.
Definition: qgsgeometry.cpp:2059
QgsFeatureList
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:572
QgsPropertyDefinition
Definition for a property.
Definition: qgsproperty.h:47
QgsGeometry::isNull
bool isNull
Definition: qgsgeometry.h:125
QgsProcessingParameterNumber::Integer
@ Integer
Integer values.
Definition: qgsprocessingparameters.h:1845
QgsGeometry
Definition: qgsgeometry.h:122
QgsGeometry::lastError
QString lastError() const
Returns an error string referring to the last error encountered either when this geometry was created...
Definition: qgsgeometry.cpp:3018
QgsFeature::hasGeometry
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:197
QgsProcessingContext::expressionContext
QgsExpressionContext & expressionContext()
Returns the expression context.
Definition: qgsprocessingcontext.h:119
QgsProperty::valueAsDouble
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.
Definition: qgsproperty.cpp:620
QgsFeature
Definition: qgsfeature.h:55
QgsProperty::valueAsInt
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.
Definition: qgsproperty.cpp:643
QgsProcessing::SourceType
SourceType
Data source types enum.
Definition: qgsprocessing.h:44
qgsalgorithmtaperedbuffer.h
QgsPropertyDefinition::DoublePositive
@ DoublePositive
Positive double value (including 0)
Definition: qgsproperty.h:59