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