QGIS API Documentation 3.41.0-Master (3440c17df1d)
Loading...
Searching...
No Matches
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
22QString QgsCreateArrayOffsetLinesAlgorithm::name() const
23{
24 return QStringLiteral( "arrayoffsetlines" );
25}
26
27QString QgsCreateArrayOffsetLinesAlgorithm::displayName() const
28{
29 return QObject::tr( "Array of offset (parallel) lines" );
30}
31
32QStringList QgsCreateArrayOffsetLinesAlgorithm::tags() const
33{
34 return QObject::tr( "offset,parallel,duplicate,create,spaced,copy,features,objects,step,repeat" ).split( ',' );
35}
36
37QString QgsCreateArrayOffsetLinesAlgorithm::group() const
38{
39 return QObject::tr( "Vector creation" );
40}
41
42QString QgsCreateArrayOffsetLinesAlgorithm::groupId() const
43{
44 return QStringLiteral( "vectorcreation" );
45}
46
47QString QgsCreateArrayOffsetLinesAlgorithm::outputName() const
48{
49 return QObject::tr( "Offset lines" );
50}
51
52QString 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
58QString QgsCreateArrayOffsetLinesAlgorithm::shortDescription() const
59{
60 return QObject::tr( "Creates multiple offset copies of lines from a layer." );
61}
62
63Qgis::ProcessingAlgorithmDocumentationFlags QgsCreateArrayOffsetLinesAlgorithm::documentationFlags() const
64{
66}
67
68QgsCreateArrayOffsetLinesAlgorithm *QgsCreateArrayOffsetLinesAlgorithm::createInstance() const
69{
70 return new QgsCreateArrayOffsetLinesAlgorithm();
71}
72
73void QgsCreateArrayOffsetLinesAlgorithm::initParameters( const QVariantMap & )
74{
75 std::unique_ptr< QgsProcessingParameterNumber > count = std::make_unique< QgsProcessingParameterNumber >( QStringLiteral( "COUNT" ),
76 QObject::tr( "Number of features to create" ), Qgis::ProcessingNumberParameterType::Integer,
77 10, false, 1 );
78 count->setIsDynamic( true );
79 count->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "COUNT" ), QObject::tr( "Number of features to create" ), QgsPropertyDefinition::IntegerPositiveGreaterZero ) );
80 count->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
81 addParameter( count.release() );
82
83 std::unique_ptr< QgsProcessingParameterDistance > offset = std::make_unique< QgsProcessingParameterDistance >( QStringLiteral( "OFFSET" ),
84 QObject::tr( "Offset step distance" ),
85 1.0, QStringLiteral( "INPUT" ) );
86 offset->setIsDynamic( true );
87 offset->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "OFFSET" ), QObject::tr( "Step distance" ), QgsPropertyDefinition::Double ) );
88 offset->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
89 addParameter( offset.release() );
90
91 auto segmentParam = std::make_unique< QgsProcessingParameterNumber >( QStringLiteral( "SEGMENTS" ), QObject::tr( "Segments" ), Qgis::ProcessingNumberParameterType::Integer, 8, false, 1 );
92 segmentParam->setFlags( segmentParam->flags() | Qgis::ProcessingParameterFlag::Advanced );
93 addParameter( segmentParam.release() );
94
95 auto joinStyleParam = std::make_unique< QgsProcessingParameterEnum>( QStringLiteral( "JOIN_STYLE" ), QObject::tr( "Join style" ), QStringList() << QObject::tr( "Round" ) << QObject::tr( "Miter" ) << QObject::tr( "Bevel" ), false, 0 );
96 joinStyleParam->setFlags( joinStyleParam->flags() | Qgis::ProcessingParameterFlag::Advanced );
97 addParameter( joinStyleParam.release() );
98
99 auto miterLimitParam = std::make_unique< QgsProcessingParameterNumber >( QStringLiteral( "MITER_LIMIT" ), QObject::tr( "Miter limit" ), Qgis::ProcessingNumberParameterType::Double, 2, false, 1 );
100 miterLimitParam->setFlags( miterLimitParam->flags() | Qgis::ProcessingParameterFlag::Advanced );
101 addParameter( miterLimitParam.release() );
102}
103
104QList<int> QgsCreateArrayOffsetLinesAlgorithm::inputLayerTypes() const
105{
106 return QList< int >() << static_cast< int >( Qgis::ProcessingSourceType::VectorLine );
107}
108
109bool QgsCreateArrayOffsetLinesAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
110{
111 mCount = parameterAsInt( parameters, QStringLiteral( "COUNT" ), context );
112 mDynamicCount = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "COUNT" ) );
113 if ( mDynamicCount )
114 mCountProperty = parameters.value( QStringLiteral( "COUNT" ) ).value< QgsProperty >();
115
116 mOffsetStep = parameterAsDouble( parameters, QStringLiteral( "OFFSET" ), context );
117 mDynamicOffset = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "OFFSET" ) );
118 if ( mDynamicOffset )
119 mOffsetStepProperty = parameters.value( QStringLiteral( "OFFSET" ) ).value< QgsProperty >();
120
121 mSegments = parameterAsInt( parameters, QStringLiteral( "SEGMENTS" ), context );
122 mJoinStyle = static_cast< Qgis::JoinStyle>( 1 + parameterAsInt( parameters, QStringLiteral( "JOIN_STYLE" ), context ) );
123 mMiterLimit = parameterAsDouble( parameters, QStringLiteral( "MITER_LIMIT" ), context );
124
125 return true;
126}
127
128QgsFeatureList QgsCreateArrayOffsetLinesAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
129{
131
132 if ( feature.hasGeometry() )
133 {
134 const QgsGeometry geometry = feature.geometry();
135 const QgsAttributes attr = feature.attributes();
136
137 int count = mCount;
138 if ( mDynamicCount )
139 count = mCountProperty.valueAsInt( context.expressionContext(), count );
140 result.reserve( count + 1 );
141
142 double offsetStep = mOffsetStep;
143 if ( mDynamicOffset )
144 offsetStep = mOffsetStepProperty.valueAsDouble( context.expressionContext(), offsetStep );
145
146 {
147 QgsFeature original = feature;
148 original.setGeometry( geometry );
149 QgsAttributes outAttrs = attr;
150 outAttrs << QVariant( 0 ) << QVariant( 0 );
151 original.setAttributes( outAttrs );
152 result << original;
153 }
154
155 double offset = 0;
156 for ( int i = 0; i < count; ++i )
157 {
158 offset += offsetStep;
159 // FIXME: shouldn't we use QgsVectorLayerUtils::createFeature?
160 QgsFeature offsetFeature = feature;
161 const QgsGeometry offsetGeometry = geometry.offsetCurve( offset, mSegments, mJoinStyle, mMiterLimit );
162 offsetFeature.setGeometry( offsetGeometry );
163 QgsAttributes outAttrs = attr;
164 outAttrs << QVariant( i + 1 ) << QVariant( offset );
165 offsetFeature.setAttributes( outAttrs );
166 result << offsetFeature;
167 }
168 }
169 else
170 {
171 result << feature;
172 }
173
174 return result;
175}
176
177QgsFields QgsCreateArrayOffsetLinesAlgorithm::outputFields( const QgsFields &inputFields ) const
178{
179 QgsFields output = inputFields;
180 output.append( QgsField( QStringLiteral( "instance" ), QMetaType::Type::Int ) );
181 output.append( QgsField( QStringLiteral( "offset" ), QMetaType::Type::Double ) );
182 return output;
183}
184
185QgsFeatureSink::SinkFlags QgsCreateArrayOffsetLinesAlgorithm::sinkFlags() const
186{
188}
189
191
192
@ VectorLine
Vector line layers.
JoinStyle
Join styles for buffers.
Definition qgis.h:1968
@ RegeneratesPrimaryKey
Algorithm always drops any existing primary keys or FID values and regenerates them in outputs.
QFlags< ProcessingAlgorithmDocumentationFlag > ProcessingAlgorithmDocumentationFlags
Flags describing algorithm behavior for documentation purposes.
Definition qgis.h:3367
@ Advanced
Parameter is an advanced parameter which should be hidden from users by default.
A vector of attributes.
QFlags< SinkFlag > SinkFlags
@ RegeneratePrimaryKey
This flag indicates, that a primary key field cannot be guaranteed to be unique and the sink should i...
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
QgsAttributes attributes
Definition qgsfeature.h:67
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
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.
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:53
Container of fields for a vector layer.
Definition qgsfields.h:46
bool append(const QgsField &field, Qgis::FieldOrigin origin=Qgis::FieldOrigin::Provider, int originIndex=-1)
Appends a field.
Definition qgsfields.cpp:70
A geometry is the spatial representation of a feature.
QgsGeometry offsetCurve(double distance, int segments, Qgis::JoinStyle joinStyle, double miterLimit) const
Returns an offset line at a given distance and side from an input line.
Contains information about the context in which a processing algorithm is executed.
QgsExpressionContext & expressionContext()
Returns the expression context.
Base class for providing feedback from a processing algorithm.
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
@ IntegerPositiveGreaterZero
Non-zero positive integer values.
Definition qgsproperty.h:54
A store for object properties.
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.
QList< QgsFeature > QgsFeatureList