QGIS API Documentation  3.8.0-Zanzibar (11aff65)
qgsalgorithmoffsetlines.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsalgorithmoffsetlines.cpp
3  ---------------------
4  begin : July 2018
5  copyright : (C) 2018 by Nyall Dawson
6  email : nyall dot dawson 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 
21 
22 QString QgsOffsetLinesAlgorithm::name() const
23 {
24  return QStringLiteral( "offsetline" );
25 }
26 
27 QString QgsOffsetLinesAlgorithm::displayName() const
28 {
29  return QObject::tr( "Offset lines" );
30 }
31 
32 QStringList QgsOffsetLinesAlgorithm::tags() const
33 {
34  return QObject::tr( "offset,linestring" ).split( ',' );
35 }
36 
37 QString QgsOffsetLinesAlgorithm::group() const
38 {
39  return QObject::tr( "Vector geometry" );
40 }
41 
42 QString QgsOffsetLinesAlgorithm::groupId() const
43 {
44  return QStringLiteral( "vectorgeometry" );
45 }
46 
47 QString QgsOffsetLinesAlgorithm::outputName() const
48 {
49  return QObject::tr( "Offset" );
50 }
51 
52 QString QgsOffsetLinesAlgorithm::shortHelpString() const
53 {
54  return QObject::tr( "This algorithm offsets lines by a specified distance. Positive distances will offset lines to the left, and negative distances "
55  "will offset to the right of lines.\n\n"
56  "The segments parameter controls the number of line segments to use to approximate a quarter circle when creating rounded offsets.\n\n"
57  "The join style parameter specifies whether round, miter or beveled joins should be used when offsetting corners in a line.\n\n"
58  "The miter limit parameter is only applicable for miter join styles, and controls the maximum distance from the offset curve to "
59  "use when creating a mitered join." );
60 }
61 
62 QString QgsOffsetLinesAlgorithm::shortDescription() const
63 {
64  return QObject::tr( "Offsets lines by a specified distance." );
65 }
66 
67 QgsOffsetLinesAlgorithm *QgsOffsetLinesAlgorithm::createInstance() const
68 {
69  return new QgsOffsetLinesAlgorithm();
70 }
71 
72 void QgsOffsetLinesAlgorithm::initParameters( const QVariantMap & )
73 {
74  std::unique_ptr< QgsProcessingParameterDistance > offset = qgis::make_unique< QgsProcessingParameterDistance >( QStringLiteral( "DISTANCE" ),
75  QObject::tr( "Distance" ),
76  10.0, QStringLiteral( "INPUT" ) );
77  offset->setIsDynamic( true );
78  offset->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "DISTANCE" ), QObject::tr( "Distance" ), QgsPropertyDefinition::Double ) );
79  offset->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
80  addParameter( offset.release() );
81 
82  auto segmentParam = qgis::make_unique< QgsProcessingParameterNumber >( QStringLiteral( "SEGMENTS" ), QObject::tr( "Segments" ), QgsProcessingParameterNumber::Integer, 8, false, 1 );
83  addParameter( segmentParam.release() );
84 
85  auto joinStyleParam = qgis::make_unique< QgsProcessingParameterEnum>( QStringLiteral( "JOIN_STYLE" ), QObject::tr( "Join style" ), QStringList() << QObject::tr( "Round" ) << QObject::tr( "Miter" ) << QObject::tr( "Bevel" ), false, 0 );
86  addParameter( joinStyleParam.release() );
87 
88  auto miterLimitParam = qgis::make_unique< QgsProcessingParameterNumber >( QStringLiteral( "MITER_LIMIT" ), QObject::tr( "Miter limit" ), QgsProcessingParameterNumber::Double, 2, false, 1 );
89  addParameter( miterLimitParam.release() );
90 }
91 
92 QList<int> QgsOffsetLinesAlgorithm::inputLayerTypes() const
93 {
94  return QList< int >() << QgsProcessing::TypeVectorLine;
95 }
96 
97 QgsProcessing::SourceType QgsOffsetLinesAlgorithm::outputLayerType() const
98 {
100 }
101 
102 bool QgsOffsetLinesAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
103 {
104  mOffset = parameterAsDouble( parameters, QStringLiteral( "DISTANCE" ), context );
105  mDynamicOffset = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "DISTANCE" ) );
106  if ( mDynamicOffset )
107  mOffsetProperty = parameters.value( QStringLiteral( "DISTANCE" ) ).value< QgsProperty >();
108 
109  mSegments = parameterAsInt( parameters, QStringLiteral( "SEGMENTS" ), context );
110  mJoinStyle = static_cast< QgsGeometry::JoinStyle>( 1 + parameterAsInt( parameters, QStringLiteral( "JOIN_STYLE" ), context ) );
111  mMiterLimit = parameterAsDouble( parameters, QStringLiteral( "MITER_LIMIT" ), context );
112 
113  return true;
114 }
115 
116 QgsFeatureList QgsOffsetLinesAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
117 {
118  if ( feature.hasGeometry() )
119  {
120  QgsFeature f = feature;
121  const QgsGeometry geometry = feature.geometry();
122 
123  double offset = mOffset;
124  if ( mDynamicOffset )
125  offset = mOffsetProperty.valueAsDouble( context.expressionContext(), offset );
126 
127  const QgsGeometry offsetGeometry = geometry.offsetCurve( offset, mSegments, mJoinStyle, mMiterLimit );
128  f.setGeometry( offsetGeometry );
129  return QgsFeatureList() << f;
130  }
131  else
132  {
133  return QgsFeatureList() << feature;
134  }
135 }
136 
138 
139 
Base class for providing feedback from a processing algorithm.
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:571
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:111
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:55
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:197
QgsGeometry offsetCurve(double distance, int segments, JoinStyle joinStyle, double miterLimit) const
Returns an offset line at a given distance and side from an input line.
A store for object properties.
Definition: qgsproperty.h:229
Double value (including negative values)
Definition: qgsproperty.h:57
QgsExpressionContext & expressionContext()
Returns the expression context.
Definition for a property.
Definition: qgsproperty.h:46
Vector line layers.
Definition: qgsprocessing.h:49
void setGeometry(const QgsGeometry &geometry)
Set the feature&#39;s geometry.
Definition: qgsfeature.cpp:137
SourceType
Data source types enum.
Definition: qgsprocessing.h:44
QgsGeometry geometry
Definition: qgsfeature.h:67
JoinStyle
Join styles for buffers.
Definition: qgsgeometry.h:1060
Contains information about the context in which a processing algorithm is executed.
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...