QGIS API Documentation 3.41.0-Master (45a0abf3bec)
Loading...
Searching...
No Matches
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#include "qgsvectorlayer.h"
20
22
23QString QgsMultiRingConstantBufferAlgorithm::name() const
24{
25 return QStringLiteral( "multiringconstantbuffer" );
26}
27
28QString QgsMultiRingConstantBufferAlgorithm::displayName() const
29{
30 return QObject::tr( "Multi-ring buffer (constant distance)" );
31}
32
33QStringList QgsMultiRingConstantBufferAlgorithm::tags() const
34{
35 return QObject::tr( "buffer,grow,multiple,rings,distance,donut" ).split( ',' );
36}
37
38QString QgsMultiRingConstantBufferAlgorithm::group() const
39{
40 return QObject::tr( "Vector geometry" );
41}
42
43QString QgsMultiRingConstantBufferAlgorithm::groupId() const
44{
45 return QStringLiteral( "vectorgeometry" );
46}
47
48QString QgsMultiRingConstantBufferAlgorithm::outputName() const
49{
50 return QObject::tr( "Multi-ring buffer (constant distance)" );
51}
52
53QString QgsMultiRingConstantBufferAlgorithm::shortHelpString() const
54{
55 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." );
56}
57
58Qgis::ProcessingAlgorithmDocumentationFlags QgsMultiRingConstantBufferAlgorithm::documentationFlags() const
59{
61}
62
63QgsMultiRingConstantBufferAlgorithm *QgsMultiRingConstantBufferAlgorithm::createInstance() const
64{
65 return new QgsMultiRingConstantBufferAlgorithm();
66}
67
68void QgsMultiRingConstantBufferAlgorithm::initParameters( const QVariantMap & )
69{
70 std::unique_ptr< QgsProcessingParameterNumber> rings = std::make_unique< QgsProcessingParameterNumber >( QStringLiteral( "RINGS" ),
71 QObject::tr( "Number of rings" ), Qgis::ProcessingNumberParameterType::Integer,
72 1, false, 0 );
73 rings->setIsDynamic( true );
74 rings->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "RINGS" ), QObject::tr( "Number of rings" ), QgsPropertyDefinition::IntegerPositive ) );
75 rings->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
76 addParameter( rings.release() );
77
78 std::unique_ptr< QgsProcessingParameterDistance > distance = std::make_unique< QgsProcessingParameterDistance >( QStringLiteral( "DISTANCE" ),
79 QObject::tr( "Distance between rings" ), 1, QStringLiteral( "INPUT" ), false );
80 distance->setIsDynamic( true );
81 distance->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "DISTANCE" ), QObject::tr( "Distance between rings" ), QgsPropertyDefinition::DoublePositive ) );
82 distance->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
83 addParameter( distance.release() );
84}
85
86bool QgsMultiRingConstantBufferAlgorithm::supportInPlaceEdit( const QgsMapLayer *l ) const
87{
88 const QgsVectorLayer *layer = qobject_cast< const QgsVectorLayer * >( l );
89 if ( !layer )
90 return false;
91
93 return false;
94 // Polygons only
95 return layer->wkbType() == Qgis::WkbType::Polygon || layer->wkbType() == Qgis::WkbType::MultiPolygon;
96}
97
98bool QgsMultiRingConstantBufferAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
99{
100 mRingsNumber = parameterAsInt( parameters, QStringLiteral( "RINGS" ), context );
101 mDynamicRingsNumber = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "RINGS" ) );
102 if ( mDynamicRingsNumber )
103 mRingsNumberProperty = parameters.value( QStringLiteral( "RINGS" ) ).value< QgsProperty >();
104
105 mDistance = parameterAsDouble( parameters, QStringLiteral( "DISTANCE" ), context );
106 mDynamicDistance = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "DISTANCE" ) );
107 if ( mDynamicDistance )
108 mDistanceProperty = parameters.value( QStringLiteral( "DISTANCE" ) ).value< QgsProperty >();
109
110 return true;
111}
112
113QgsFields QgsMultiRingConstantBufferAlgorithm::outputFields( const QgsFields &inputFields ) const
114{
115 QgsFields fields = inputFields;
116 fields.append( QgsField( QStringLiteral( "ringId" ), QMetaType::Type::Int, QString(), 10, 0 ) );
117 fields.append( QgsField( QStringLiteral( "distance" ), QMetaType::Type::Double, QString(), 20, 6 ) );
118 return fields;
119}
120
121Qgis::ProcessingFeatureSourceFlags QgsMultiRingConstantBufferAlgorithm::sourceFlags() const
122{
124}
125
126QgsFeatureSink::SinkFlags QgsMultiRingConstantBufferAlgorithm::sinkFlags() const
127{
129}
130
131QgsFeatureList QgsMultiRingConstantBufferAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
132{
133 double currentDistance = 0;
134 QgsGeometry outputGeometry, previousGeometry;
135
136 int rings = mRingsNumber;
137 if ( mDynamicRingsNumber )
138 rings = mRingsNumberProperty.valueAsInt( context.expressionContext(), rings );
139
140 double distance = mDistance;
141 if ( mDynamicDistance )
142 distance = mDistanceProperty.valueAsDouble( context.expressionContext(), distance );
143
144 QgsFeatureList outputs;
145
146 // Set previous geometry to a zero-distance buffer of the original geometry,
147 // this is needed for negative distance values
148 previousGeometry = feature.geometry().buffer( 0.0, 40 );
149 previousGeometry.convertToMultiType();
150
151 for ( int i = 1; i <= rings; ++i )
152 {
153 QgsFeature out;
154 currentDistance = i * distance;
155 outputGeometry = feature.geometry().buffer( currentDistance, 40 );
156 outputGeometry.convertToMultiType();
157 if ( outputGeometry.isNull() )
158 {
159 feedback->reportError( QObject::tr( "Error calculating buffer for feature %1" ).arg( feature.id() ) );
160 continue;
161 }
162
163 if ( distance < 0.0 )
164 {
165 out.setGeometry( previousGeometry.symDifference( outputGeometry ) );
166 }
167 else if ( i == 1 )
168 {
169 out.setGeometry( outputGeometry );
170 }
171 else
172 {
173 out.setGeometry( outputGeometry.symDifference( previousGeometry ) );
174 }
175 previousGeometry = outputGeometry;
176 QgsAttributes attrs = feature.attributes();
177 attrs << i << currentDistance;
178 out.setAttributes( attrs );
179 outputs.append( out );
180 }
181
182 // For negative distance values, the last generated buffer geometry needs to be added
183 if ( distance < 0.0 )
184 {
185 QgsFeature out;
186 out.setGeometry( previousGeometry );
187 QgsAttributes attrs = feature.attributes();
188 attrs << 0 << 0.0;
189 out.setAttributes( attrs );
190 outputs.append( out );
191 }
192
193 return outputs;
194}
195
@ RegeneratesPrimaryKey
Algorithm always drops any existing primary keys or FID values and regenerates them in outputs.
QFlags< ProcessingAlgorithmDocumentationFlag > ProcessingAlgorithmDocumentationFlags
Flags describing algorithm behavior for documentation purposes.
Definition qgis.h:3367
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
@ Polygon
Polygon.
@ MultiPolygon
MultiPolygon.
QFlags< ProcessingFeatureSourceFlag > ProcessingFeatureSourceFlags
Flags which control how QgsProcessingFeatureSource fetches features.
Definition qgis.h:3444
A vector of attributes.
QFlags< SinkFlag > SinkFlags
@ RegeneratePrimaryKey
This flag indicates, that a primary key field cannot be guaranteed to be unique and the sink should i...
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
QgsAttributes attributes
Definition qgsfeature.h:67
QgsFeatureId id
Definition qgsfeature.h:66
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
QgsGeometry geometry
Definition qgsfeature.h:69
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:53
Container of fields for a vector layer.
Definition qgsfields.h:46
bool append(const QgsField &field, Qgis::FieldOrigin origin=Qgis::FieldOrigin::Provider, int originIndex=-1)
Appends a field.
Definition qgsfields.cpp:70
A geometry is the spatial representation of a feature.
QgsGeometry symDifference(const QgsGeometry &geometry, const QgsGeometryParameters &parameters=QgsGeometryParameters()) const
Returns a geometry representing the points making up this geometry that do not make up other.
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...
bool convertToMultiType()
Converts single type geometry into multitype geometry e.g.
Base class for all map layer types.
Definition qgsmaplayer.h:76
Contains information about the context in which a processing algorithm is executed.
QgsExpressionContext & expressionContext()
Returns the expression context.
bool supportInPlaceEdit(const QgsMapLayer *layer) const override
Checks whether this algorithm supports in-place editing on the given layer Default implementation for...
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
@ IntegerPositive
Positive integer values (including 0)
Definition qgsproperty.h:53
@ 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.
Represents a vector layer which manages a vector based data sets.
Q_INVOKABLE Qgis::WkbType wkbType() const FINAL
Returns the WKBType or WKBUnknown in case of error.
QList< QgsFeature > QgsFeatureList