QGIS API Documentation  3.14.0-Pi (9f7028fd23)
qgsalgorithmarrayoffsetlines.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsalgorithmarrayoffsetlines.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 QgsCreateArrayOffsetLinesAlgorithm::name() const
23 {
24  return QStringLiteral( "arrayoffsetlines" );
25 }
26 
27 QString QgsCreateArrayOffsetLinesAlgorithm::displayName() const
28 {
29  return QObject::tr( "Array of offset (parallel) lines" );
30 }
31 
32 QStringList QgsCreateArrayOffsetLinesAlgorithm::tags() const
33 {
34  return QObject::tr( "offset,parallel,duplicate,create,spaced,copy,features,objects,step,repeat" ).split( ',' );
35 }
36 
37 QString QgsCreateArrayOffsetLinesAlgorithm::group() const
38 {
39  return QObject::tr( "Vector creation" );
40 }
41 
42 QString QgsCreateArrayOffsetLinesAlgorithm::groupId() const
43 {
44  return QStringLiteral( "vectorcreation" );
45 }
46 
47 QString QgsCreateArrayOffsetLinesAlgorithm::outputName() const
48 {
49  return QObject::tr( "Offset lines" );
50 }
51 
52 QString QgsCreateArrayOffsetLinesAlgorithm::shortHelpString() const
53 {
54  return QObject::tr( "This algorithm creates copies of line features in a layer, by creating multiple offset versions of each feature. "
55  "Each copy is offset by a preset distance." );
56 }
57 
58 QString QgsCreateArrayOffsetLinesAlgorithm::shortDescription() const
59 {
60  return QObject::tr( "Creates multiple offset copies of lines from a layer." );
61 }
62 
63 QgsCreateArrayOffsetLinesAlgorithm *QgsCreateArrayOffsetLinesAlgorithm::createInstance() const
64 {
65  return new QgsCreateArrayOffsetLinesAlgorithm();
66 }
67 
68 void QgsCreateArrayOffsetLinesAlgorithm::initParameters( const QVariantMap & )
69 {
70  std::unique_ptr< QgsProcessingParameterNumber > count = qgis::make_unique< QgsProcessingParameterNumber >( QStringLiteral( "COUNT" ),
71  QObject::tr( "Number of features to create" ), QgsProcessingParameterNumber::Integer,
72  10, false, 1 );
73  count->setIsDynamic( true );
74  count->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "COUNT" ), QObject::tr( "Number of features to create" ), QgsPropertyDefinition::IntegerPositiveGreaterZero ) );
75  count->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
76  addParameter( count.release() );
77 
78  std::unique_ptr< QgsProcessingParameterDistance > offset = qgis::make_unique< QgsProcessingParameterDistance >( QStringLiteral( "OFFSET" ),
79  QObject::tr( "Offset step distance" ),
80  1.0, QStringLiteral( "INPUT" ) );
81  offset->setIsDynamic( true );
82  offset->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "OFFSET" ), QObject::tr( "Step distance" ), QgsPropertyDefinition::Double ) );
83  offset->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
84  addParameter( offset.release() );
85 
86  auto segmentParam = qgis::make_unique< QgsProcessingParameterNumber >( QStringLiteral( "SEGMENTS" ), QObject::tr( "Segments" ), QgsProcessingParameterNumber::Integer, 8, false, 1 );
87  segmentParam->setFlags( segmentParam->flags() | QgsProcessingParameterDefinition::FlagAdvanced );
88  addParameter( segmentParam.release() );
89 
90  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 );
91  joinStyleParam->setFlags( joinStyleParam->flags() | QgsProcessingParameterDefinition::FlagAdvanced );
92  addParameter( joinStyleParam.release() );
93 
94  auto miterLimitParam = qgis::make_unique< QgsProcessingParameterNumber >( QStringLiteral( "MITER_LIMIT" ), QObject::tr( "Miter limit" ), QgsProcessingParameterNumber::Double, 2, false, 1 );
95  miterLimitParam->setFlags( miterLimitParam->flags() | QgsProcessingParameterDefinition::FlagAdvanced );
96  addParameter( miterLimitParam.release() );
97 }
98 
99 QList<int> QgsCreateArrayOffsetLinesAlgorithm::inputLayerTypes() const
100 {
101  return QList< int >() << QgsProcessing::TypeVectorLine;
102 }
103 
104 bool QgsCreateArrayOffsetLinesAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
105 {
106  mCount = parameterAsInt( parameters, QStringLiteral( "COUNT" ), context );
107  mDynamicCount = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "COUNT" ) );
108  if ( mDynamicCount )
109  mCountProperty = parameters.value( QStringLiteral( "COUNT" ) ).value< QgsProperty >();
110 
111  mOffsetStep = parameterAsDouble( parameters, QStringLiteral( "OFFSET" ), context );
112  mDynamicOffset = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "OFFSET" ) );
113  if ( mDynamicOffset )
114  mOffsetStepProperty = parameters.value( QStringLiteral( "OFFSET" ) ).value< QgsProperty >();
115 
116  mSegments = parameterAsInt( parameters, QStringLiteral( "SEGMENTS" ), context );
117  mJoinStyle = static_cast< QgsGeometry::JoinStyle>( 1 + parameterAsInt( parameters, QStringLiteral( "JOIN_STYLE" ), context ) );
118  mMiterLimit = parameterAsDouble( parameters, QStringLiteral( "MITER_LIMIT" ), context );
119 
120  return true;
121 }
122 
123 QgsFeatureList QgsCreateArrayOffsetLinesAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
124 {
125  QgsFeatureList result = QgsFeatureList();
126 
127  if ( feature.hasGeometry() )
128  {
129  const QgsGeometry geometry = feature.geometry();
130  const QgsAttributes attr = feature.attributes();
131 
132  int count = mCount;
133  if ( mDynamicCount )
134  count = mCountProperty.valueAsInt( context.expressionContext(), count );
135  result.reserve( count + 1 );
136 
137  double offsetStep = mOffsetStep;
138  if ( mDynamicOffset )
139  offsetStep = mOffsetStepProperty.valueAsDouble( context.expressionContext(), offsetStep );
140 
141  {
142  QgsFeature original = feature;
143  original.setGeometry( geometry );
144  QgsAttributes outAttrs = attr;
145  outAttrs << QVariant( 0 ) << QVariant( 0 );
146  original.setAttributes( outAttrs );
147  result << original;
148  }
149 
150  double offset = 0;
151  for ( int i = 0; i < count; ++i )
152  {
153  offset += offsetStep;
154  // FIXME: shouldn't we use QgsVectorLayerUtils::createFeature?
155  QgsFeature offsetFeature = feature;
156  const QgsGeometry offsetGeometry = geometry.offsetCurve( offset, mSegments, mJoinStyle, mMiterLimit );
157  offsetFeature.setGeometry( offsetGeometry );
158  QgsAttributes outAttrs = attr;
159  outAttrs << QVariant( i + 1 ) << QVariant( offset );
160  offsetFeature.setAttributes( outAttrs );
161  result << offsetFeature;
162  }
163  }
164  else
165  {
166  result << feature;
167  }
168 
169  return result;
170 }
171 
172 QgsFields QgsCreateArrayOffsetLinesAlgorithm::outputFields( const QgsFields &inputFields ) const
173 {
174  QgsFields output = inputFields;
175  output.append( QgsField( QStringLiteral( "instance" ), QVariant::Int ) );
176  output.append( QgsField( QStringLiteral( "offset" ), QVariant::Double ) );
177  return output;
178 }
179 
180 QgsFeatureSink::SinkFlags QgsCreateArrayOffsetLinesAlgorithm::sinkFlags() const
181 {
183 }
184 
186 
187 
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
QgsProcessingFeedback
Definition: qgsprocessingfeedback.h:37
QgsFields
Definition: qgsfields.h:44
QgsPropertyDefinition::IntegerPositiveGreaterZero
@ IntegerPositiveGreaterZero
Non-zero positive integer values.
Definition: qgsproperty.h:57
QgsFeature::geometry
QgsGeometry geometry
Definition: qgsfeature.h:71
QgsProcessingParameterDefinition::FlagAdvanced
@ FlagAdvanced
Parameter is an advanced parameter which should be hidden from users by default.
Definition: qgsprocessingparameters.h:419
QgsPropertyDefinition::Double
@ Double
Double value (including negative values)
Definition: qgsproperty.h:58
QgsProcessing::TypeVectorLine
@ TypeVectorLine
Vector line layers.
Definition: qgsprocessing.h:49
QgsFields::append
bool append(const QgsField &field, FieldOrigin origin=OriginProvider, int originIndex=-1)
Appends a field. The field must have unique name, otherwise it is rejected (returns false)
Definition: qgsfields.cpp:59
QgsFeature::setGeometry
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
Definition: qgsfeature.cpp:137
QgsProcessingContext
Definition: qgsprocessingcontext.h:43
QgsGeometry::offsetCurve
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.
Definition: qgsgeometry.cpp:1957
QgsFeatureList
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:572
qgsalgorithmarrayoffsetlines.h
QgsPropertyDefinition
Definition for a property.
Definition: qgsproperty.h:47
QgsFeatureSink::RegeneratePrimaryKey
@ RegeneratePrimaryKey
This flag indicates, that a primary key field cannot be guaranteed to be unique and the sink should i...
Definition: qgsfeaturesink.h:55
QgsFeature::attributes
QgsAttributes attributes
Definition: qgsfeature.h:69
QgsProcessingParameterNumber::Integer
@ Integer
Integer values.
Definition: qgsprocessingparameters.h:1845
QgsGeometry
Definition: qgsgeometry.h:122
QgsFeature::hasGeometry
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:197
QgsProcessingContext::expressionContext
QgsExpressionContext & expressionContext()
Returns the expression context.
Definition: qgsprocessingcontext.h:119
QgsProperty::valueAsDouble
double valueAsDouble(const QgsExpressionContext &context, double defaultValue=0.0, bool *ok=nullptr) const
Calculates the current value of the property and interprets it as a double.
Definition: qgsproperty.cpp:620
QgsAttributes
Definition: qgsattributes.h:57
QgsGeometry::JoinStyle
JoinStyle
Join styles for buffers.
Definition: qgsgeometry.h:1125
QgsFeature
Definition: qgsfeature.h:55
QgsFeature::setAttributes
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
Definition: qgsfeature.cpp:127
QgsField
Definition: qgsfield.h:49