25using namespace Qt::StringLiterals;
29QString QgsBufferAlgorithm::name()
const
34QString QgsBufferAlgorithm::displayName()
const
36 return QObject::tr(
"Buffer" );
39QStringList QgsBufferAlgorithm::tags()
const
41 return QObject::tr(
"buffer,grow,fixed,variable,distance" ).split(
',' );
44QString QgsBufferAlgorithm::group()
const
46 return QObject::tr(
"Vector geometry" );
49QString QgsBufferAlgorithm::groupId()
const
51 return u
"vectorgeometry"_s;
54void QgsBufferAlgorithm::initAlgorithm(
const QVariantMap & )
58 auto bufferParam = std::make_unique<QgsProcessingParameterDistance>( u
"DISTANCE"_s, QObject::tr(
"Distance" ), 10, u
"INPUT"_s );
59 bufferParam->setIsDynamic(
true );
61 bufferParam->setDynamicLayerParameterName( u
"INPUT"_s );
62 addParameter( bufferParam.release() );
64 segmentParam->setHelp( QObject::tr(
"The segments parameter controls the number of line segments to use to approximate a quarter circle when creating rounded offsets." ) );
65 addParameter( segmentParam.release() );
66 addParameter(
new QgsProcessingParameterEnum( u
"END_CAP_STYLE"_s, QObject::tr(
"End cap style" ), QStringList() << QObject::tr(
"Round" ) << QObject::tr(
"Flat" ) << QObject::tr(
"Square" ),
false, 0 ) );
67 addParameter(
new QgsProcessingParameterEnum( u
"JOIN_STYLE"_s, QObject::tr(
"Join style" ), QStringList() << QObject::tr(
"Round" ) << QObject::tr(
"Miter" ) << QObject::tr(
"Bevel" ),
false, 0 ) );
72 auto keepDisjointParam = std::make_unique<QgsProcessingParameterBoolean>( u
"SEPARATE_DISJOINT"_s, QObject::tr(
"Keep disjoint results separate" ),
false );
74 keepDisjointParam->setHelp( QObject::tr(
"If checked, then any disjoint parts in the buffer results will be output as separate single-part features." ) );
75 addParameter( keepDisjointParam.release() );
80QString QgsBufferAlgorithm::shortHelpString()
const
82 return QObject::tr(
"This algorithm computes a buffer area for all the features in an input layer, using a fixed or dynamic distance.\n\n"
83 "The segments parameter controls the number of line segments to use to approximate a quarter circle when creating rounded offsets.\n\n"
84 "The end cap style parameter controls how line endings are handled in the buffer.\n\n"
85 "The join style parameter specifies whether round, miter or beveled joins should be used when offsetting corners in a line.\n\n"
86 "The miter limit parameter is only applicable for miter join styles, and controls the maximum distance from the offset curve to use when creating a mitered join." );
89QString QgsBufferAlgorithm::shortDescription()
const
91 return QObject::tr(
"Computes a buffer area for all the features in an input layer, using a fixed or dynamic distance." );
99QgsBufferAlgorithm *QgsBufferAlgorithm::createInstance()
const
101 return new QgsBufferAlgorithm();
106 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, u
"INPUT"_s, context ) );
111 const bool dissolve = parameterAsBoolean( parameters, u
"DISSOLVE"_s, context );
112 const bool keepDisjointSeparate = parameterAsBoolean( parameters, u
"SEPARATE_DISJOINT"_s, context );
113 const int segments = parameterAsInt( parameters, u
"SEGMENTS"_s, context );
116 const double miterLimit = parameterAsDouble( parameters, u
"MITER_LIMIT"_s, context );
117 const double bufferDistance = parameterAsDouble( parameters, u
"DISTANCE"_s, context );
125 QgsExpressionContext expressionContext = createExpressionContext( parameters, context, source.get() );
132 const long count = source->featureCount();
138 const double step = count > 0 ? 100.0 / count : 1;
141 QVector<QgsGeometry> bufferedGeometriesForDissolve;
150 if ( dissolveAttrs.isEmpty() )
156 double distance = bufferDistance;
160 distance = bufferProperty.
valueAsDouble( expressionContext, bufferDistance );
164 if ( outputGeometry.
isNull() )
170 bufferedGeometriesForDissolve << outputGeometry;
176 if ( !keepDisjointSeparate )
197 else if ( !dissolve )
207 if ( dissolve && !bufferedGeometriesForDissolve.isEmpty() )
214 if ( !keepDisjointSeparate )
237 outputs.insert( u
"OUTPUT"_s, dest );
250 const bool keepDisjointSeparate = parameterAsBoolean( parameters, u
"SEPARATE_DISJOINT"_s, context );
253 if ( sink ==
"OUTPUT"_L1 )
257 const VectorProperties inputProps = sourceProperties.value( u
"INPUT"_s );
258 result.
fields = inputProps.fields;
259 result.
crs = inputProps.crs;
266 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, u
"INPUT"_s, context ) );
269 result.
fields = source->fields();
270 result.
crs = source->sourceCrs();
280bool QgsBufferAlgorithm::supportInPlaceEdit(
const QgsMapLayer *layer )
const
282 const QgsVectorLayer *vlayer = qobject_cast<const QgsVectorLayer *>( layer );
@ VectorPolygon
Vector polygon layers.
@ Warning
Warning message.
@ Available
Properties are available.
JoinStyle
Join styles for buffers.
@ RegeneratesPrimaryKeyInSomeScenarios
Algorithm may drop the existing primary keys or FID values in some scenarios, depending on algorithm ...
QFlags< ProcessingAlgorithmFlag > ProcessingAlgorithmFlags
Flags indicating how and when an algorithm operates and should be exposed to users.
EndCapStyle
End cap styles for buffers.
QFlags< ProcessingAlgorithmDocumentationFlag > ProcessingAlgorithmDocumentationFlags
Flags describing algorithm behavior for documentation purposes.
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
@ MultiPolygon
MultiPolygon.
@ SupportsInPlaceEdits
Algorithm supports in-place editing.
@ Advanced
Parameter is an advanced parameter which should be hidden from users by default.
@ Double
Double/float values.
Abstract base class for all geometries.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
Fetch next feature and stores in f, returns true on success.
Wraps a request for features to a vector layer (or directly its vector data provider).
QFlags< SinkFlag > SinkFlags
@ FastInsert
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
@ 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...
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
bool hasGeometry() const
Returns true if the feature has an associated geometry.
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
bool isCanceled() const
Tells whether the operation has been canceled already.
void setProgress(double progress)
Sets the current progress for the feedback object.
A geometry is the spatial representation of a feature.
QgsAbstractGeometry::const_part_iterator const_parts_begin() const
Returns STL-style const iterator pointing to the first part of the geometry.
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...
QgsAbstractGeometry::const_part_iterator const_parts_end() const
Returns STL-style iterator pointing to the imaginary part after the last part of the geometry.
bool convertToMultiType()
Converts single type geometry into multitype geometry e.g.
static QgsGeometry unaryUnion(const QVector< QgsGeometry > &geometries, const QgsGeometryParameters ¶meters=QgsGeometryParameters())
Compute the unary union on a list of geometries.
Base class for all map layer types.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true, const char *file=__builtin_FILE(), const char *function=__builtin_FUNCTION(), int line=__builtin_LINE())
Adds a message to the log instance (and creates it if necessary).
virtual Qgis::ProcessingAlgorithmFlags flags() const
Returns the flags indicating how and when the algorithm operates and should be exposed to users.
Contains information about the context in which a processing algorithm is executed.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
A boolean parameter for processing algorithms.
An enum based parameter for processing algorithms, allowing for selection from predefined values.
A feature sink output for processing algorithms.
An input feature source (such as vector layers) parameter for processing algorithms.
A numeric parameter for processing algorithms.
static bool isDynamic(const QVariantMap ¶meters, 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.
@ Double
Double value (including negative values).
A store for object properties.
QVariant value(const QgsExpressionContext &context, const QVariant &defaultValue=QVariant(), bool *ok=nullptr) const
Calculates the current value of the property, including any transforms which are set for the property...
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 dataset.
Q_INVOKABLE Qgis::WkbType wkbType() const final
Returns the WKBType or WKBUnknown in case of error.
Properties of a vector source or sink used in an algorithm.
Qgis::WkbType wkbType
Geometry (WKB) type.
QgsCoordinateReferenceSystem crs
Coordinate Reference System.
Qgis::ProcessingPropertyAvailability availability
Availability of the properties. By default properties are not available.