QGIS API Documentation 3.40.0-Bratislava (b56115d8743)
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" ),
101 QObject::tr( "Start width" ), Qgis::ProcessingNumberParameterType::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 = std::make_unique< QgsProcessingParameterNumber >( QStringLiteral( "END_WIDTH" ),
109 QObject::tr( "End width" ), Qgis::ProcessingNumberParameterType::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 = std::make_unique< QgsProcessingParameterNumber >( QStringLiteral( "SEGMENTS" ),
117 QObject::tr( "Segments" ), Qgis::ProcessingNumberParameterType::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
125QgsFeatureList 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 const 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
156QString QgsVariableWidthBufferByMAlgorithm::name() const
157{
158 return QStringLiteral( "bufferbym" );
159}
160
161QString QgsVariableWidthBufferByMAlgorithm::displayName() const
162{
163 return QObject::tr( "Variable width buffer (by M value)" );
164}
165
166QStringList QgsVariableWidthBufferByMAlgorithm::tags() const
167{
168 return QObject::tr( "variable,distance,length,line,buffer" ).split( ',' );
169}
170
171QString QgsVariableWidthBufferByMAlgorithm::group() const
172{
173 return QObject::tr( "Vector geometry" );
174}
175
176QString QgsVariableWidthBufferByMAlgorithm::groupId() const
177{
178 return QStringLiteral( "vectorgeometry" );
179}
180
181QString QgsVariableWidthBufferByMAlgorithm::outputName() const
182{
183 return QObject::tr( "Buffered" );
184}
185
186Qgis::ProcessingSourceType QgsVariableWidthBufferByMAlgorithm::outputLayerType() const
187{
189}
190
191Qgis::WkbType QgsVariableWidthBufferByMAlgorithm::outputWkbType( Qgis::WkbType ) const
192{
194}
195
196bool 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
206QString 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
212QList<int> QgsVariableWidthBufferByMAlgorithm::inputLayerTypes() const
213{
214 return QList<int>() << static_cast< int >( Qgis::ProcessingSourceType::VectorLine );
215}
216
217QgsVariableWidthBufferByMAlgorithm *QgsVariableWidthBufferByMAlgorithm::createInstance() const
218{
219 return new QgsVariableWidthBufferByMAlgorithm();
220}
221
222void QgsVariableWidthBufferByMAlgorithm::initParameters( const QVariantMap & )
223{
224 std::unique_ptr< QgsProcessingParameterNumber > segments = std::make_unique< QgsProcessingParameterNumber >( QStringLiteral( "SEGMENTS" ),
225 QObject::tr( "Segments" ), Qgis::ProcessingNumberParameterType::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
233QgsFeatureList 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 const 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
ProcessingSourceType
Processing data source types.
Definition qgis.h:3262
@ 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