QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 
23 QString QgsSingleSidedBufferAlgorithm::name() const
24 {
25  return QStringLiteral( "singlesidedbuffer" );
26 }
27 
28 QString QgsSingleSidedBufferAlgorithm::displayName() const
29 {
30  return QObject::tr( "Single sided buffer" );
31 }
32 
33 QStringList QgsSingleSidedBufferAlgorithm::tags() const
34 {
35  return QObject::tr( "rectangle,perpendicular,right,angles,square,quadrilateralise" ).split( ',' );
36 }
37 
38 QString QgsSingleSidedBufferAlgorithm::group() const
39 {
40  return QObject::tr( "Vector geometry" );
41 }
42 
43 QString QgsSingleSidedBufferAlgorithm::groupId() const
44 {
45  return QStringLiteral( "vectorgeometry" );
46 }
47 
48 QString 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 
60 QString QgsSingleSidedBufferAlgorithm::outputName() const
61 {
62  return QObject::tr( "Buffered" );
63 }
64 
65 QList<int> QgsSingleSidedBufferAlgorithm::inputLayerTypes() const
66 {
67  return QList<int>() << QgsProcessing::TypeVectorLine;
68 }
69 
70 QgsProcessing::SourceType QgsSingleSidedBufferAlgorithm::outputLayerType() const
71 {
73 }
74 
75 QgsWkbTypes::Type QgsSingleSidedBufferAlgorithm::outputWkbType( QgsWkbTypes::Type type ) const
76 {
77  Q_UNUSED( type );
78  return QgsWkbTypes::Polygon;
79 }
80 
81 QgsSingleSidedBufferAlgorithm *QgsSingleSidedBufferAlgorithm::createInstance() const
82 {
83  return new QgsSingleSidedBufferAlgorithm();
84 }
85 
86 void QgsSingleSidedBufferAlgorithm::initParameters( const QVariantMap & )
87 {
88  auto bufferParam = qgis::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" ), QgsProcessingParameterNumber::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" ), QgsProcessingParameterNumber::Double, 2, false, 1 ) );
98 }
99 
100 bool 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< QgsGeometry::BufferSide>( parameterAsInt( parameters, QStringLiteral( "SIDE" ), context ) );
108  mSegments = parameterAsInt( parameters, QStringLiteral( "SEGMENTS" ), context );
109  mJoinStyle = static_cast< QgsGeometry::JoinStyle>( 1 + parameterAsInt( parameters, QStringLiteral( "JOIN_STYLE" ), context ) );
110  mMiterLimit = parameterAsDouble( parameters, QStringLiteral( "MITER_LIMIT" ), context );
111 
112  return true;
113 }
114 
115 QgsFeatureList 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  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 
QgsProcessingParameterNumber::Double
@ Double
Double/float values.
Definition: qgsprocessingparameters.h:1846
QgsProperty
A store for object properties.
Definition: qgsproperty.h:231
QgsProcessingParameters::isDynamic
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: qgsprocessingparameters.cpp:111
QgsProcessingParameterNumber
Definition: qgsprocessingparameters.h:1838
QgsProcessingFeedback
Definition: qgsprocessingfeedback.h:37
QgsProcessing::TypeVectorPolygon
@ TypeVectorPolygon
Vector polygon layers.
Definition: qgsprocessing.h:50
QgsFeature::geometry
QgsGeometry geometry
Definition: qgsfeature.h:71
QgsPropertyDefinition::Double
@ Double
Double value (including negative values)
Definition: qgsproperty.h:58
QgsProcessing::TypeVectorLine
@ TypeVectorLine
Vector line layers.
Definition: qgsprocessing.h:49
QgsWkbTypes::Type
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:68
qgsalgorithmsinglesidedbuffer.h
QgsFeature::setGeometry
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
Definition: qgsfeature.cpp:137
QgsProcessingContext
Definition: qgsprocessingcontext.h:43
QgsFeatureList
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:572
QgsPropertyDefinition
Definition for a property.
Definition: qgsproperty.h:47
QgsGeometry::isNull
bool isNull
Definition: qgsgeometry.h:125
QgsProcessingParameterNumber::Integer
@ Integer
Integer values.
Definition: qgsprocessingparameters.h:1845
QgsGeometry::BufferSide
BufferSide
Side of line to buffer.
Definition: qgsgeometry.h:1108
qgsprocessing.h
QgsGeometry
Definition: qgsgeometry.h:122
QgsFeature::hasGeometry
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:197
QgsGeometry::singleSidedBuffer
QgsGeometry singleSidedBuffer(double distance, int segments, BufferSide side, JoinStyle joinStyle=JoinStyleRound, double miterLimit=2.0) const
Returns a single sided buffer for a (multi)line geometry.
Definition: qgsgeometry.cpp:2015
QgsWkbTypes::Polygon
@ Polygon
Definition: qgswkbtypes.h:73
QgsProcessingContext::expressionContext
QgsExpressionContext & expressionContext()
Returns the expression context.
Definition: qgsprocessingcontext.h:119
QgsGeometry::JoinStyle
JoinStyle
Join styles for buffers.
Definition: qgsgeometry.h:1125
QgsFeature
Definition: qgsfeature.h:55
QgsProcessingParameterEnum
Definition: qgsprocessingparameters.h:2134
QgsProcessing::SourceType
SourceType
Data source types enum.
Definition: qgsprocessing.h:44
QgsProcessingException
Definition: qgsexception.h:82