QGIS API Documentation 3.38.0-Grenoble (exported)
Loading...
Searching...
No Matches
qgsalgorithmsinglesidedbuffer.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmsinglesidedbuffer.cpp
3 ---------------------
4 begin : November 2019
5 copyright : (C) 2019 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 "qgsprocessing.h"
20
22
23QString QgsSingleSidedBufferAlgorithm::name() const
24{
25 return QStringLiteral( "singlesidedbuffer" );
26}
27
28QString QgsSingleSidedBufferAlgorithm::displayName() const
29{
30 return QObject::tr( "Single sided buffer" );
31}
32
33QStringList QgsSingleSidedBufferAlgorithm::tags() const
34{
35 return QObject::tr( "rectangle,perpendicular,right,angles,square,quadrilateralise" ).split( ',' );
36}
37
38QString QgsSingleSidedBufferAlgorithm::group() const
39{
40 return QObject::tr( "Vector geometry" );
41}
42
43QString QgsSingleSidedBufferAlgorithm::groupId() const
44{
45 return QStringLiteral( "vectorgeometry" );
46}
47
48QString QgsSingleSidedBufferAlgorithm::shortHelpString() const
49{
50 return QObject::tr( "This algorithm buffers lines by a specified distance on one "
51 "side of the line only.\n\nThe segments parameter controls "
52 "the number of line segments to use to approximate a quarter "
53 "circle when creating rounded buffers. The join style parameter "
54 "specifies whether round, miter or beveled joins should be used "
55 "when buffering corners in a line. The miter limit parameter is "
56 "only applicable for miter join styles, and controls the maximum "
57 "distance from the buffer to use when creating a mitered join." );
58}
59
60QString QgsSingleSidedBufferAlgorithm::outputName() const
61{
62 return QObject::tr( "Buffered" );
63}
64
65QList<int> QgsSingleSidedBufferAlgorithm::inputLayerTypes() const
66{
67 return QList<int>() << static_cast< int >( Qgis::ProcessingSourceType::VectorLine );
68}
69
70Qgis::ProcessingSourceType QgsSingleSidedBufferAlgorithm::outputLayerType() const
71{
73}
74
75Qgis::WkbType QgsSingleSidedBufferAlgorithm::outputWkbType( Qgis::WkbType type ) const
76{
77 Q_UNUSED( type );
79}
80
81QgsSingleSidedBufferAlgorithm *QgsSingleSidedBufferAlgorithm::createInstance() const
82{
83 return new QgsSingleSidedBufferAlgorithm();
84}
85
86void QgsSingleSidedBufferAlgorithm::initParameters( const QVariantMap & )
87{
88 auto bufferParam = std::make_unique < QgsProcessingParameterDistance >( QStringLiteral( "DISTANCE" ), QObject::tr( "Distance" ), 10, QStringLiteral( "INPUT" ) );
89 bufferParam->setIsDynamic( true );
90 bufferParam->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "Distance" ), QObject::tr( "Buffer distance" ), QgsPropertyDefinition::Double ) );
91 bufferParam->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
92 addParameter( bufferParam.release() );
93
94 addParameter( new QgsProcessingParameterEnum( QStringLiteral( "SIDE" ), QObject::tr( "Side" ), QStringList() << QObject::tr( "Left" ) << QObject::tr( "Right" ), false, 0 ) );
95 addParameter( new QgsProcessingParameterNumber( QStringLiteral( "SEGMENTS" ), QObject::tr( "Segments" ), Qgis::ProcessingNumberParameterType::Integer, 8, false, 1 ) );
96 addParameter( new QgsProcessingParameterEnum( QStringLiteral( "JOIN_STYLE" ), QObject::tr( "Join style" ), QStringList() << QObject::tr( "Round" ) << QObject::tr( "Miter" ) << QObject::tr( "Bevel" ), false, 0 ) );
97 addParameter( new QgsProcessingParameterNumber( QStringLiteral( "MITER_LIMIT" ), QObject::tr( "Miter limit" ), Qgis::ProcessingNumberParameterType::Double, 2, false, 1 ) );
98}
99
100bool QgsSingleSidedBufferAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
101{
102 mDistance = parameterAsDouble( parameters, QStringLiteral( "DISTANCE" ), context );
103 mDynamicDistance = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "DISTANCE" ) );
104 if ( mDynamicDistance )
105 mDistanceProperty = parameters.value( QStringLiteral( "DISTANCE" ) ).value< QgsProperty >();
106
107 mSide = static_cast< Qgis::BufferSide>( parameterAsInt( parameters, QStringLiteral( "SIDE" ), context ) );
108 mSegments = parameterAsInt( parameters, QStringLiteral( "SEGMENTS" ), context );
109 mJoinStyle = static_cast< Qgis::JoinStyle>( 1 + parameterAsInt( parameters, QStringLiteral( "JOIN_STYLE" ), context ) );
110 mMiterLimit = parameterAsDouble( parameters, QStringLiteral( "MITER_LIMIT" ), context );
111
112 return true;
113}
114
115QgsFeatureList QgsSingleSidedBufferAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
116{
117 QgsFeature f = feature;
118
119 if ( f.hasGeometry() )
120 {
121 double distance = mDistance;
122 if ( mDynamicDistance )
123 distance = mDistanceProperty.valueAsDouble( context.expressionContext(), distance );
124
125 const QgsGeometry outputGeometry = f.geometry().singleSidedBuffer( distance, mSegments, mSide, mJoinStyle, mMiterLimit );
126 if ( outputGeometry.isNull() )
127 throw QgsProcessingException( QObject::tr( "Error calculating single sided buffer" ) );
128
129 f.setGeometry( outputGeometry );
130 }
131
132 return QgsFeatureList() << f;
133}
134
ProcessingSourceType
Processing data source types.
Definition qgis.h:2995
@ VectorPolygon
Vector polygon layers.
@ VectorLine
Vector line layers.
BufferSide
Side of line to buffer.
Definition qgis.h:1763
JoinStyle
Join styles for buffers.
Definition qgis.h:1788
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:201
@ Polygon
Polygon.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
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.
QgsGeometry singleSidedBuffer(double distance, int segments, Qgis::BufferSide side, Qgis::JoinStyle joinStyle=Qgis::JoinStyle::Round, double miterLimit=2.0) const
Returns a single sided buffer for a (multi)line geometry.
Contains information about the context in which a processing algorithm is executed.
QgsExpressionContext & expressionContext()
Returns the expression context.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
An enum based parameter for processing algorithms, allowing for selection from predefined values.
A numeric parameter for processing algorithms.
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
@ Double
Double value (including negative values)
Definition qgsproperty.h:55
A store for object properties.
QList< QgsFeature > QgsFeatureList