QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
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" ),
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 > xOffset = std::make_unique< QgsProcessingParameterDistance >( QStringLiteral( "DELTA_X" ),
84 QObject::tr( "Step distance (x-axis)" ),
85 0.0, QStringLiteral( "INPUT" ) );
86 xOffset->setIsDynamic( true );
87 xOffset->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "DELTA_X" ), QObject::tr( "Offset distance (x-axis)" ), QgsPropertyDefinition::Double ) );
88 xOffset->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
89 addParameter( xOffset.release() );
90
91 std::unique_ptr< QgsProcessingParameterDistance > yOffset = std::make_unique< QgsProcessingParameterDistance >( QStringLiteral( "DELTA_Y" ),
92 QObject::tr( "Step distance (y-axis)" ),
93 0.0, QStringLiteral( "INPUT" ) );
94 yOffset->setIsDynamic( true );
95 yOffset->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "DELTA_Y" ), QObject::tr( "Offset distance (y-axis)" ), QgsPropertyDefinition::Double ) );
96 yOffset->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
97 addParameter( yOffset.release() );
98
99 std::unique_ptr< QgsProcessingParameterNumber > zOffset = std::make_unique< QgsProcessingParameterNumber >( QStringLiteral( "DELTA_Z" ),
100 QObject::tr( "Step distance (z-axis)" ), Qgis::ProcessingNumberParameterType::Double,
101 0.0 );
102 zOffset->setIsDynamic( true );
103 zOffset->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "DELTA_Z" ), QObject::tr( "Offset distance (z-axis)" ), QgsPropertyDefinition::Double ) );
104 zOffset->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
105 addParameter( zOffset.release() );
106
107 std::unique_ptr< QgsProcessingParameterNumber > mOffset = std::make_unique< QgsProcessingParameterNumber >( QStringLiteral( "DELTA_M" ),
108 QObject::tr( "Step distance (m values)" ), Qgis::ProcessingNumberParameterType::Double,
109 0.0 );
110 mOffset->setIsDynamic( true );
111 mOffset->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "DELTA_M" ), QObject::tr( "Offset distance (m values)" ), QgsPropertyDefinition::Double ) );
112 mOffset->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
113 addParameter( mOffset.release() );
114}
115
116bool QgsArrayTranslatedFeaturesAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
117{
118 mCount = parameterAsInt( parameters, QStringLiteral( "COUNT" ), context );
119 mDynamicCount = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "COUNT" ) );
120 if ( mDynamicCount )
121 mCountProperty = parameters.value( QStringLiteral( "COUNT" ) ).value< QgsProperty >();
122
123 mDeltaX = parameterAsDouble( parameters, QStringLiteral( "DELTA_X" ), context );
124 mDynamicDeltaX = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "DELTA_X" ) );
125 if ( mDynamicDeltaX )
126 mDeltaXProperty = parameters.value( QStringLiteral( "DELTA_X" ) ).value< QgsProperty >();
127
128 mDeltaY = parameterAsDouble( parameters, QStringLiteral( "DELTA_Y" ), context );
129 mDynamicDeltaY = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "DELTA_Y" ) );
130 if ( mDynamicDeltaY )
131 mDeltaYProperty = parameters.value( QStringLiteral( "DELTA_Y" ) ).value< QgsProperty >();
132
133 mDeltaZ = parameterAsDouble( parameters, QStringLiteral( "DELTA_Z" ), context );
134 mDynamicDeltaZ = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "DELTA_Z" ) );
135 if ( mDynamicDeltaZ )
136 mDeltaZProperty = parameters.value( QStringLiteral( "DELTA_Z" ) ).value< QgsProperty >();
137
138 mDeltaM = parameterAsDouble( parameters, QStringLiteral( "DELTA_M" ), context );
139 mDynamicDeltaM = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "DELTA_M" ) );
140 if ( mDynamicDeltaM )
141 mDeltaMProperty = parameters.value( QStringLiteral( "DELTA_M" ) ).value< QgsProperty >();
142
143 return true;
144}
145
146QgsFeatureList QgsArrayTranslatedFeaturesAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
147{
149
150 if ( feature.hasGeometry() )
151 {
152 QgsGeometry geometry = feature.geometry();
153 const QgsAttributes attr = feature.attributes();
154
155 int count = mCount;
156 if ( mDynamicCount )
157 count = mCountProperty.valueAsInt( context.expressionContext(), count );
158 result.reserve( count + 1 );
159
160 double deltaX = mDeltaX;
161 if ( mDynamicDeltaX )
162 deltaX = mDeltaXProperty.valueAsDouble( context.expressionContext(), deltaX );
163 double deltaY = mDeltaY;
164 if ( mDynamicDeltaY )
165 deltaY = mDeltaYProperty.valueAsDouble( context.expressionContext(), deltaY );
166 double deltaZ = mDeltaZ;
167 if ( mDynamicDeltaZ )
168 deltaZ = mDeltaZProperty.valueAsDouble( context.expressionContext(), deltaZ );
169 double deltaM = mDeltaM;
170 if ( mDynamicDeltaM )
171 deltaM = mDeltaMProperty.valueAsDouble( context.expressionContext(), deltaM );
172
173 // we add the original feature only after adding initial z/m values if needed
174 if ( deltaZ != 0 && !geometry.constGet()->is3D() )
175 geometry.get()->addZValue( 0 );
176 if ( deltaM != 0 && !geometry.constGet()->isMeasure() )
177 geometry.get()->addMValue( 0 );
178
179 {
180 QgsFeature original = feature;
181 original.setGeometry( geometry );
182 QgsAttributes outAttrs = attr;
183 outAttrs << QVariant( 0 );
184 original.setAttributes( outAttrs );
185 result << original;
186 }
187
188 for ( int i = 0; i < count; ++i )
189 {
190 QgsFeature offsetFeature = feature;
191 geometry.translate( deltaX, deltaY, deltaZ, deltaM );
192 offsetFeature.setGeometry( geometry );
193 QgsAttributes outAttrs = attr;
194 outAttrs << QVariant( i + 1 );
195 offsetFeature.setAttributes( outAttrs );
196 result << offsetFeature;
197 }
198 }
199 else
200 {
201 result << feature;
202 }
203
204 return result;
205}
206
207Qgis::WkbType QgsArrayTranslatedFeaturesAlgorithm::outputWkbType( Qgis::WkbType inputWkbType ) const
208{
209 Qgis::WkbType wkb = inputWkbType;
210 if ( mDeltaZ != 0 )
211 wkb = QgsWkbTypes::addZ( wkb );
212 if ( mDeltaM != 0 )
213 wkb = QgsWkbTypes::addM( wkb );
214 return wkb;
215}
216
217QgsFields QgsArrayTranslatedFeaturesAlgorithm::outputFields( const QgsFields &inputFields ) const
218{
219 QgsFields output;
220 output.append( QgsField( QStringLiteral( "instance" ), QMetaType::Type::Int ) );
221 return QgsProcessingUtils::combineFields( inputFields, output );
222}
223
224QgsFeatureSink::SinkFlags QgsArrayTranslatedFeaturesAlgorithm::sinkFlags() const
225{
227}
228
230
231
@ 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:3359
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