QGIS API Documentation 3.99.0-Master (357b655ed83)
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
20#include "qgsprocessing.h"
21
22#include <QString>
23
24using namespace Qt::StringLiterals;
25
27
28QString QgsSingleSidedBufferAlgorithm::name() const
29{
30 return u"singlesidedbuffer"_s;
31}
32
33QString QgsSingleSidedBufferAlgorithm::displayName() const
34{
35 return QObject::tr( "Single sided buffer" );
36}
37
38QStringList QgsSingleSidedBufferAlgorithm::tags() const
39{
40 return QObject::tr( "rectangle,perpendicular,right,angles,square,quadrilateralise" ).split( ',' );
41}
42
43QString QgsSingleSidedBufferAlgorithm::group() const
44{
45 return QObject::tr( "Vector geometry" );
46}
47
48QString QgsSingleSidedBufferAlgorithm::groupId() const
49{
50 return u"vectorgeometry"_s;
51}
52
53QString QgsSingleSidedBufferAlgorithm::shortHelpString() const
54{
55 return QObject::tr( "This algorithm buffers lines by a specified distance on one "
56 "side of the line only.\n\nThe segments parameter controls "
57 "the number of line segments to use to approximate a quarter "
58 "circle when creating rounded buffers. The join style parameter "
59 "specifies whether round, miter or beveled joins should be used "
60 "when buffering corners in a line. The miter limit parameter is "
61 "only applicable for miter join styles, and controls the maximum "
62 "distance from the buffer to use when creating a mitered join." );
63}
64
65QString QgsSingleSidedBufferAlgorithm::shortDescription() const
66{
67 return QObject::tr( "Buffers lines by a specified distance on one side of the line only." );
68}
69
70QString QgsSingleSidedBufferAlgorithm::outputName() const
71{
72 return QObject::tr( "Buffered" );
73}
74
75QList<int> QgsSingleSidedBufferAlgorithm::inputLayerTypes() const
76{
77 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorLine );
78}
79
80Qgis::ProcessingSourceType QgsSingleSidedBufferAlgorithm::outputLayerType() const
81{
83}
84
85Qgis::WkbType QgsSingleSidedBufferAlgorithm::outputWkbType( Qgis::WkbType type ) const
86{
87 Q_UNUSED( type );
89}
90
91QgsSingleSidedBufferAlgorithm *QgsSingleSidedBufferAlgorithm::createInstance() const
92{
93 return new QgsSingleSidedBufferAlgorithm();
94}
95
96void QgsSingleSidedBufferAlgorithm::initParameters( const QVariantMap & )
97{
98 auto bufferParam = std::make_unique<QgsProcessingParameterDistance>( u"DISTANCE"_s, QObject::tr( "Distance" ), 10, u"INPUT"_s );
99 bufferParam->setIsDynamic( true );
100 bufferParam->setDynamicPropertyDefinition( QgsPropertyDefinition( u"Distance"_s, QObject::tr( "Buffer distance" ), QgsPropertyDefinition::Double ) );
101 bufferParam->setDynamicLayerParameterName( u"INPUT"_s );
102 addParameter( bufferParam.release() );
103
104 addParameter( new QgsProcessingParameterEnum( u"SIDE"_s, QObject::tr( "Side" ), QStringList() << QObject::tr( "Left" ) << QObject::tr( "Right" ), false, 0 ) );
105 addParameter( new QgsProcessingParameterNumber( u"SEGMENTS"_s, QObject::tr( "Segments" ), Qgis::ProcessingNumberParameterType::Integer, 8, false, 1 ) );
106 addParameter( new QgsProcessingParameterEnum( u"JOIN_STYLE"_s, QObject::tr( "Join style" ), QStringList() << QObject::tr( "Round" ) << QObject::tr( "Miter" ) << QObject::tr( "Bevel" ), false, 0 ) );
107 addParameter( new QgsProcessingParameterNumber( u"MITER_LIMIT"_s, QObject::tr( "Miter limit" ), Qgis::ProcessingNumberParameterType::Double, 2, false, 1 ) );
108}
109
110bool QgsSingleSidedBufferAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
111{
112 mDistance = parameterAsDouble( parameters, u"DISTANCE"_s, context );
113 mDynamicDistance = QgsProcessingParameters::isDynamic( parameters, u"DISTANCE"_s );
114 if ( mDynamicDistance )
115 mDistanceProperty = parameters.value( u"DISTANCE"_s ).value<QgsProperty>();
116
117 mSide = static_cast<Qgis::BufferSide>( parameterAsInt( parameters, u"SIDE"_s, context ) );
118 mSegments = parameterAsInt( parameters, u"SEGMENTS"_s, context );
119 mJoinStyle = static_cast<Qgis::JoinStyle>( 1 + parameterAsInt( parameters, u"JOIN_STYLE"_s, context ) );
120 mMiterLimit = parameterAsDouble( parameters, u"MITER_LIMIT"_s, context );
121
122 return true;
123}
124
125QgsFeatureList QgsSingleSidedBufferAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
126{
127 QgsFeature f = feature;
128
129 if ( f.hasGeometry() )
130 {
131 double distance = mDistance;
132 if ( mDynamicDistance )
133 distance = mDistanceProperty.valueAsDouble( context.expressionContext(), distance );
134
135 const QgsGeometry outputGeometry = f.geometry().singleSidedBuffer( distance, mSegments, mSide, mJoinStyle, mMiterLimit );
136 if ( outputGeometry.isNull() )
137 throw QgsProcessingException( QObject::tr( "Error calculating single sided buffer" ) );
138
139 f.setGeometry( outputGeometry );
140 }
141
142 return QgsFeatureList() << f;
143}
144
ProcessingSourceType
Processing data source types.
Definition qgis.h:3602
@ VectorPolygon
Vector polygon layers.
Definition qgis.h:3607
@ VectorLine
Vector line layers.
Definition qgis.h:3606
BufferSide
Side of line to buffer.
Definition qgis.h:2154
JoinStyle
Join styles for buffers.
Definition qgis.h:2179
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:280
@ Polygon
Polygon.
Definition qgis.h:284
@ Double
Double/float values.
Definition qgis.h:3875
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:60
QgsGeometry geometry
Definition qgsfeature.h:71
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:47
@ Double
Double value (including negative values).
Definition qgsproperty.h:57
A store for object properties.
QList< QgsFeature > QgsFeatureList