QGIS API Documentation 3.41.0-Master (af5edcb665c)
Loading...
Searching...
No Matches
qgsalgorithmarraytranslatedfeatures.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmarraytranslatedfeatures.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 QgsArrayTranslatedFeaturesAlgorithm::name() const
23{
24 return QStringLiteral( "arraytranslatedfeatures" );
25}
26
27QString QgsArrayTranslatedFeaturesAlgorithm::displayName() const
28{
29 return QObject::tr( "Array of translated features" );
30}
31
32QStringList QgsArrayTranslatedFeaturesAlgorithm::tags() const
33{
34 return QObject::tr( "translate,parallel,offset,duplicate,grid,spaced,moved,copy,features,objects,step,repeat" ).split( ',' );
35}
36
37QString QgsArrayTranslatedFeaturesAlgorithm::group() const
38{
39 return QObject::tr( "Vector creation" );
40}
41
42QString QgsArrayTranslatedFeaturesAlgorithm::groupId() const
43{
44 return QStringLiteral( "vectorcreation" );
45}
46
47QString QgsArrayTranslatedFeaturesAlgorithm::outputName() const
48{
49 return QObject::tr( "Translated" );
50}
51
52QString QgsArrayTranslatedFeaturesAlgorithm::shortHelpString() const
53{
54 return QObject::tr( "This algorithm creates copies of features in a layer, by creating multiple translated versions of each feature. "
55 "Each copy is incrementally displaced by a preset amount in the x/y/z/m axis." );
56}
57
58QString QgsArrayTranslatedFeaturesAlgorithm::shortDescription() const
59{
60 return QObject::tr( "Creates multiple translated copies of features in a layer." );
61}
62
63Qgis::ProcessingAlgorithmDocumentationFlags QgsArrayTranslatedFeaturesAlgorithm::documentationFlags() const
64{
66}
67
68QgsArrayTranslatedFeaturesAlgorithm *QgsArrayTranslatedFeaturesAlgorithm::createInstance() const
69{
70 return new QgsArrayTranslatedFeaturesAlgorithm();
71}
72
73void QgsArrayTranslatedFeaturesAlgorithm::initParameters( const QVariantMap & )
74{
75 std::unique_ptr<QgsProcessingParameterNumber> count = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral( "COUNT" ), QObject::tr( "Number of features to create" ), Qgis::ProcessingNumberParameterType::Integer, 10, false, 1 );
76 count->setIsDynamic( true );
77 count->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "COUNT" ), QObject::tr( "Number of features to create" ), QgsPropertyDefinition::IntegerPositiveGreaterZero ) );
78 count->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
79 addParameter( count.release() );
80
81 std::unique_ptr<QgsProcessingParameterDistance> xOffset = std::make_unique<QgsProcessingParameterDistance>( QStringLiteral( "DELTA_X" ), QObject::tr( "Step distance (x-axis)" ), 0.0, QStringLiteral( "INPUT" ) );
82 xOffset->setIsDynamic( true );
83 xOffset->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "DELTA_X" ), QObject::tr( "Offset distance (x-axis)" ), QgsPropertyDefinition::Double ) );
84 xOffset->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
85 addParameter( xOffset.release() );
86
87 std::unique_ptr<QgsProcessingParameterDistance> yOffset = std::make_unique<QgsProcessingParameterDistance>( QStringLiteral( "DELTA_Y" ), QObject::tr( "Step distance (y-axis)" ), 0.0, QStringLiteral( "INPUT" ) );
88 yOffset->setIsDynamic( true );
89 yOffset->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "DELTA_Y" ), QObject::tr( "Offset distance (y-axis)" ), QgsPropertyDefinition::Double ) );
90 yOffset->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
91 addParameter( yOffset.release() );
92
93 std::unique_ptr<QgsProcessingParameterNumber> zOffset = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral( "DELTA_Z" ), QObject::tr( "Step distance (z-axis)" ), Qgis::ProcessingNumberParameterType::Double, 0.0 );
94 zOffset->setIsDynamic( true );
95 zOffset->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "DELTA_Z" ), QObject::tr( "Offset distance (z-axis)" ), QgsPropertyDefinition::Double ) );
96 zOffset->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
97 addParameter( zOffset.release() );
98
99 std::unique_ptr<QgsProcessingParameterNumber> mOffset = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral( "DELTA_M" ), QObject::tr( "Step distance (m values)" ), Qgis::ProcessingNumberParameterType::Double, 0.0 );
100 mOffset->setIsDynamic( true );
101 mOffset->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "DELTA_M" ), QObject::tr( "Offset distance (m values)" ), QgsPropertyDefinition::Double ) );
102 mOffset->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
103 addParameter( mOffset.release() );
104}
105
106bool QgsArrayTranslatedFeaturesAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
107{
108 mCount = parameterAsInt( parameters, QStringLiteral( "COUNT" ), context );
109 mDynamicCount = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "COUNT" ) );
110 if ( mDynamicCount )
111 mCountProperty = parameters.value( QStringLiteral( "COUNT" ) ).value<QgsProperty>();
112
113 mDeltaX = parameterAsDouble( parameters, QStringLiteral( "DELTA_X" ), context );
114 mDynamicDeltaX = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "DELTA_X" ) );
115 if ( mDynamicDeltaX )
116 mDeltaXProperty = parameters.value( QStringLiteral( "DELTA_X" ) ).value<QgsProperty>();
117
118 mDeltaY = parameterAsDouble( parameters, QStringLiteral( "DELTA_Y" ), context );
119 mDynamicDeltaY = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "DELTA_Y" ) );
120 if ( mDynamicDeltaY )
121 mDeltaYProperty = parameters.value( QStringLiteral( "DELTA_Y" ) ).value<QgsProperty>();
122
123 mDeltaZ = parameterAsDouble( parameters, QStringLiteral( "DELTA_Z" ), context );
124 mDynamicDeltaZ = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "DELTA_Z" ) );
125 if ( mDynamicDeltaZ )
126 mDeltaZProperty = parameters.value( QStringLiteral( "DELTA_Z" ) ).value<QgsProperty>();
127
128 mDeltaM = parameterAsDouble( parameters, QStringLiteral( "DELTA_M" ), context );
129 mDynamicDeltaM = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "DELTA_M" ) );
130 if ( mDynamicDeltaM )
131 mDeltaMProperty = parameters.value( QStringLiteral( "DELTA_M" ) ).value<QgsProperty>();
132
133 return true;
134}
135
136QgsFeatureList QgsArrayTranslatedFeaturesAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
137{
139
140 if ( feature.hasGeometry() )
141 {
142 QgsGeometry geometry = feature.geometry();
143 const QgsAttributes attr = feature.attributes();
144
145 int count = mCount;
146 if ( mDynamicCount )
147 count = mCountProperty.valueAsInt( context.expressionContext(), count );
148 result.reserve( count + 1 );
149
150 double deltaX = mDeltaX;
151 if ( mDynamicDeltaX )
152 deltaX = mDeltaXProperty.valueAsDouble( context.expressionContext(), deltaX );
153 double deltaY = mDeltaY;
154 if ( mDynamicDeltaY )
155 deltaY = mDeltaYProperty.valueAsDouble( context.expressionContext(), deltaY );
156 double deltaZ = mDeltaZ;
157 if ( mDynamicDeltaZ )
158 deltaZ = mDeltaZProperty.valueAsDouble( context.expressionContext(), deltaZ );
159 double deltaM = mDeltaM;
160 if ( mDynamicDeltaM )
161 deltaM = mDeltaMProperty.valueAsDouble( context.expressionContext(), deltaM );
162
163 // we add the original feature only after adding initial z/m values if needed
164 if ( deltaZ != 0 && !geometry.constGet()->is3D() )
165 geometry.get()->addZValue( 0 );
166 if ( deltaM != 0 && !geometry.constGet()->isMeasure() )
167 geometry.get()->addMValue( 0 );
168
169 {
170 QgsFeature original = feature;
171 original.setGeometry( geometry );
172 QgsAttributes outAttrs = attr;
173 outAttrs << QVariant( 0 );
174 original.setAttributes( outAttrs );
175 result << original;
176 }
177
178 for ( int i = 0; i < count; ++i )
179 {
180 QgsFeature offsetFeature = feature;
181 geometry.translate( deltaX, deltaY, deltaZ, deltaM );
182 offsetFeature.setGeometry( geometry );
183 QgsAttributes outAttrs = attr;
184 outAttrs << QVariant( i + 1 );
185 offsetFeature.setAttributes( outAttrs );
186 result << offsetFeature;
187 }
188 }
189 else
190 {
191 result << feature;
192 }
193
194 return result;
195}
196
197Qgis::WkbType QgsArrayTranslatedFeaturesAlgorithm::outputWkbType( Qgis::WkbType inputWkbType ) const
198{
199 Qgis::WkbType wkb = inputWkbType;
200 if ( mDeltaZ != 0 )
201 wkb = QgsWkbTypes::addZ( wkb );
202 if ( mDeltaM != 0 )
203 wkb = QgsWkbTypes::addM( wkb );
204 return wkb;
205}
206
207QgsFields QgsArrayTranslatedFeaturesAlgorithm::outputFields( const QgsFields &inputFields ) const
208{
209 QgsFields output;
210 output.append( QgsField( QStringLiteral( "instance" ), QMetaType::Type::Int ) );
211 return QgsProcessingUtils::combineFields( inputFields, output );
212}
213
214QgsFeatureSink::SinkFlags QgsArrayTranslatedFeaturesAlgorithm::sinkFlags() const
215{
217}
218
@ 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:3430
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:256
virtual bool addZValue(double zValue=0)=0
Adds a z-dimension to the geometry, initialized to a preset value.
bool isMeasure() const
Returns true if the geometry contains m values.
bool is3D() const
Returns true if the geometry is 3D and contains a z-value.
virtual bool addMValue(double mValue=0)=0
Adds a measure to the geometry, initialized to a preset value.
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.
QgsAbstractGeometry * get()
Returns a modifiable (non-const) reference to the underlying abstract geometry primitive.
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
Qgis::GeometryOperationResult translate(double dx, double dy, double dz=0.0, double dm=0.0)
Translates this geometry by dx, dy, dz and dm.
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...
static QgsFields combineFields(const QgsFields &fieldsA, const QgsFields &fieldsB, const QString &fieldsBPrefix=QString())
Combines two field lists, avoiding duplicate field names (in a case-insensitive manner).
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.
static Qgis::WkbType addM(Qgis::WkbType type)
Adds the m dimension to a WKB type and returns the new type.
static Qgis::WkbType addZ(Qgis::WkbType type)
Adds the z dimension to a WKB type and returns the new type.
QList< QgsFeature > QgsFeatureList