QGIS API Documentation 4.3.0-Master (d3b565c628d)
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 QGS_MARK_ALGORITHM_SOURCE
133
134 if ( !feature.hasGeometry() )
135 return QgsFeatureList() << feature;
136
137 QgsFeature f = feature;
138 int segments = mSegments;
139 if ( mDynamicSegments )
140 segments = mSegmentsProperty.valueAsInt( context.expressionContext(), segments );
141
142 double startWidth = mStartWidth;
143 if ( mDynamicStartWidth )
144 startWidth = mStartWidthProperty.valueAsDouble( context.expressionContext(), startWidth );
145
146 double endWidth = mEndWidth;
147 if ( mDynamicEndWidth )
148 endWidth = mEndWidthProperty.valueAsDouble( context.expressionContext(), endWidth );
149
150 const QgsGeometry outputGeometry = feature.geometry().taperedBuffer( startWidth, endWidth, segments );
151 if ( outputGeometry.isNull() )
152 {
153 feedback->reportError( QObject::tr( "Error buffering geometry %1: %2" ).arg( feature.id() ).arg( outputGeometry.lastError() ) );
154 }
155 f.setGeometry( outputGeometry );
156
157 return QgsFeatureList() << f;
158}
159
160
161QString QgsVariableWidthBufferByMAlgorithm::name() const
162{
163 return u"bufferbym"_s;
164}
165
166QString QgsVariableWidthBufferByMAlgorithm::displayName() const
167{
168 return QObject::tr( "Variable width buffer (by M value)" );
169}
170
171QStringList QgsVariableWidthBufferByMAlgorithm::tags() const
172{
173 return QObject::tr( "variable,distance,length,line,buffer" ).split( ',' );
174}
175
176QString QgsVariableWidthBufferByMAlgorithm::group() const
177{
178 return QObject::tr( "Vector geometry" );
179}
180
181QString QgsVariableWidthBufferByMAlgorithm::groupId() const
182{
183 return u"vectorgeometry"_s;
184}
185
186QString QgsVariableWidthBufferByMAlgorithm::outputName() const
187{
188 return QObject::tr( "Buffered" );
189}
190
191Qgis::ProcessingSourceType QgsVariableWidthBufferByMAlgorithm::outputLayerType() const
192{
194}
195
196Qgis::WkbType QgsVariableWidthBufferByMAlgorithm::outputWkbType( Qgis::WkbType ) const
197{
199}
200
201bool QgsVariableWidthBufferByMAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
202{
203 mSegments = parameterAsInt( parameters, u"Segments"_s, context );
204 mDynamicSegments = QgsProcessingParameters::isDynamic( parameters, u"Segments"_s );
205 if ( mDynamicSegments )
206 mSegmentsProperty = parameters.value( u"Segments"_s ).value<QgsProperty>();
207
208 return true;
209}
210
211QString QgsVariableWidthBufferByMAlgorithm::shortHelpString() const
212{
213 return QObject::tr(
214 "This algorithm creates variable width buffers along lines, using the M value of the line geometries "
215 "as the diameter of the buffer at each vertex."
216 );
217}
218
219QString QgsVariableWidthBufferByMAlgorithm::shortDescription() const
220{
221 return QObject::tr(
222 "Creates variable width buffers along lines, using the M value of the line geometries "
223 "as the diameter of the buffer at each vertex."
224 );
225}
226
227QList<int> QgsVariableWidthBufferByMAlgorithm::inputLayerTypes() const
228{
229 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorLine );
230}
231
232QgsVariableWidthBufferByMAlgorithm *QgsVariableWidthBufferByMAlgorithm::createInstance() const
233{
234 return new QgsVariableWidthBufferByMAlgorithm();
235}
236
237void QgsVariableWidthBufferByMAlgorithm::initParameters( const QVariantMap & )
238{
239 auto segments = std::make_unique<QgsProcessingParameterNumber>( u"SEGMENTS"_s, QObject::tr( "Segments" ), Qgis::ProcessingNumberParameterType::Integer, 16, false, 1 );
240 segments->setIsDynamic( true );
241 segments->setDynamicPropertyDefinition( QgsPropertyDefinition( u"SEGMENTS"_s, QObject::tr( "Segments" ), QgsPropertyDefinition::IntegerPositiveGreaterZero ) );
242 segments->setDynamicLayerParameterName( u"INPUT"_s );
243 addParameter( segments.release() );
244}
245
246QgsFeatureList QgsVariableWidthBufferByMAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
247{
248 QGS_MARK_ALGORITHM_SOURCE
249
250 if ( !feature.hasGeometry() )
251 return QgsFeatureList() << feature;
252
253 QgsFeature f = feature;
254 int segments = mSegments;
255 if ( mDynamicSegments )
256 segments = mSegmentsProperty.valueAsInt( context.expressionContext(), segments );
257
258 const QgsGeometry outputGeometry = feature.geometry().variableWidthBufferByM( segments );
259 if ( outputGeometry.isNull() )
260 {
261 feedback->reportError( QObject::tr( "Error buffering geometry %1: %2" ).arg( feature.id() ).arg( outputGeometry.lastError() ) );
262 }
263 f.setGeometry( outputGeometry );
264
265 return QgsFeatureList() << f;
266}
ProcessingSourceType
Processing data source types.
Definition qgis.h:3747
@ VectorPolygon
Vector polygon layers.
Definition qgis.h:3752
@ VectorLine
Vector line layers.
Definition qgis.h:3751
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:4023
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:63
QgsGeometry geometry
Definition qgsfeature.h:66
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