QGIS API Documentation  3.2.0-Bonn (bc43194)
qgsalgorithmmultiringconstantbuffer.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsalgorithmnultitingconstantbuffer.cpp
3  --------------------------
4  begin : February 2018
5  copyright : (C) 2018 by Alexander Bruy
6  email : alexander dot bruy 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 QgsMultiRingConstantBufferAlgorithm::name() const
23 {
24  return QStringLiteral( "multiringconstantbuffer" );
25 }
26 
27 QString QgsMultiRingConstantBufferAlgorithm::displayName() const
28 {
29  return QObject::tr( "Multi-ring buffer (constant distance)" );
30 }
31 
32 QStringList QgsMultiRingConstantBufferAlgorithm::tags() const
33 {
34  return QObject::tr( "buffer,grow,multiple,rings,distance,donut" ).split( ',' );
35 }
36 
37 QString QgsMultiRingConstantBufferAlgorithm::group() const
38 {
39  return QObject::tr( "Vector geometry" );
40 }
41 
42 QString QgsMultiRingConstantBufferAlgorithm::groupId() const
43 {
44  return QStringLiteral( "vectorgeometry" );
45 }
46 
47 QString QgsMultiRingConstantBufferAlgorithm::outputName() const
48 {
49  return QObject::tr( "Multi-ring buffer (constant distance)" );
50 }
51 
52 QString QgsMultiRingConstantBufferAlgorithm::shortHelpString() const
53 {
54  return QObject::tr( "This algorithm computes multi-ring ('donuts') buffer for all the features in an input layer, using a fixed or dynamic distance and rings number." );
55 }
56 
57 QgsMultiRingConstantBufferAlgorithm *QgsMultiRingConstantBufferAlgorithm::createInstance() const
58 {
59  return new QgsMultiRingConstantBufferAlgorithm();
60 }
61 
62 void QgsMultiRingConstantBufferAlgorithm::initParameters( const QVariantMap & )
63 {
64  std::unique_ptr< QgsProcessingParameterNumber> rings = qgis::make_unique< QgsProcessingParameterNumber >( QStringLiteral( "RINGS" ),
65  QObject::tr( "Number of rings" ), QgsProcessingParameterNumber::Integer,
66  1, false, 0 );
67  rings->setIsDynamic( true );
68  rings->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "RINGS" ), QObject::tr( "Number of rings" ), QgsPropertyDefinition::IntegerPositive ) );
69  rings->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
70  addParameter( rings.release() );
71 
72  std::unique_ptr< QgsProcessingParameterDistance > distance = qgis::make_unique< QgsProcessingParameterDistance >( QStringLiteral( "DISTANCE" ),
73  QObject::tr( "Distance between rings" ), 1, QStringLiteral( "INPUT" ), false, 0 );
74  distance->setIsDynamic( true );
75  distance->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "DISTANCE" ), QObject::tr( "Distance between rings" ), QgsPropertyDefinition::DoublePositive ) );
76  distance->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
77  addParameter( distance.release() );
78 }
79 
80 bool QgsMultiRingConstantBufferAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
81 {
82  mRingsNumber = parameterAsInt( parameters, QStringLiteral( "RINGS" ), context );
83  mDynamicRingsNumber = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "RINGS" ) );
84  if ( mDynamicRingsNumber )
85  mRingsNumberProperty = parameters.value( QStringLiteral( "RINGS" ) ).value< QgsProperty >();
86 
87  mDistance = parameterAsDouble( parameters, QStringLiteral( "DISTANCE" ), context );
88  mDynamicDistance = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "DISTANCE" ) );
89  if ( mDynamicDistance )
90  mDistanceProperty = parameters.value( QStringLiteral( "DISTANCE" ) ).value< QgsProperty >();
91 
92  return true;
93 }
94 
95 QgsFields QgsMultiRingConstantBufferAlgorithm::outputFields( const QgsFields &inputFields ) const
96 {
97  QgsFields fields = inputFields;
98  fields.append( QgsField( QStringLiteral( "ringId" ), QVariant::Int, QString(), 10, 0 ) );
99  fields.append( QgsField( QStringLiteral( "distance" ), QVariant::Double, QString(), 20, 6 ) );
100  return fields;
101 }
102 
103 QgsFeatureList QgsMultiRingConstantBufferAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
104 {
105  double currentDistance = 0;
106  QgsGeometry outputGeometry, previousGeometry;
107 
108  int rings = mRingsNumber;
109  if ( mDynamicRingsNumber )
110  rings = mRingsNumberProperty.valueAsInt( context.expressionContext(), rings );
111 
112  double distance = mDistance;
113  if ( mDynamicDistance )
114  distance = mDistanceProperty.valueAsDouble( context.expressionContext(), distance );
115 
116  QgsFeatureList outputs;
117 
118  for ( int i = 1; i <= rings; ++i )
119  {
120  QgsFeature out;
121  currentDistance = i * distance;
122  outputGeometry = feature.geometry().buffer( currentDistance, 40 );
123  if ( !outputGeometry )
124  {
125  feedback->reportError( QObject::tr( "Error calculating buffer for feature %1" ).arg( feature.id() ) );
126  continue;
127  }
128 
129  if ( i == 1 )
130  {
131  out.setGeometry( outputGeometry );
132  }
133  else
134  {
135  out.setGeometry( outputGeometry.symDifference( previousGeometry ) );
136  }
137  previousGeometry = outputGeometry;
138  QgsAttributes attrs = feature.attributes();
139  attrs << i << currentDistance;
140  out.setAttributes( attrs );
141  outputs.append( out );
142  }
143  return outputs;
144 }
145 
QgsFeatureId id
Definition: qgsfeature.h:71
Positive integer values (including 0)
Definition: qgsproperty.h:55
Base class for providing feedback from a processing algorithm.
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:549
Container of fields for a vector layer.
Definition: qgsfields.h:42
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:104
void setAttributes(const QgsAttributes &attrs)
Sets the feature&#39;s attributes.
Definition: qgsfeature.cpp:127
QgsGeometry buffer(double distance, int segments) const
Returns a buffer region around this geometry having the given width and with a specified number of se...
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:62
Positive double value (including 0)
Definition: qgsproperty.h:58
bool append(const QgsField &field, FieldOrigin origin=OriginProvider, int originIndex=-1)
Append a field. The field must have unique name, otherwise it is rejected (returns false) ...
Definition: qgsfields.cpp:59
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:48
QgsGeometry geometry() const
Returns the geometry associated with this feature.
Definition: qgsfeature.cpp:101
A store for object properties.
Definition: qgsproperty.h:229
QgsExpressionContext & expressionContext()
Returns the expression context.
Definition for a property.
Definition: qgsproperty.h:46
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.
void setGeometry(const QgsGeometry &geometry)
Set the feature&#39;s geometry.
Definition: qgsfeature.cpp:137
QgsGeometry symDifference(const QgsGeometry &geometry) const
Returns a geometry representing the points making up this geometry that do not make up other...
A vector of attributes.
Definition: qgsattributes.h:58
Contains information about the context in which a processing algorithm is executed.
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...
QgsAttributes attributes
Definition: qgsfeature.h:72