QGIS API Documentation 3.41.0-Master (57ec4277f5e)
Loading...
Searching...
No Matches
qgsalgorithmaffinetransform.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmaffinetransform.cpp
3 ---------------------
4 begin : December 2019
5 copyright : (C) 2019 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#include "qgsvectorlayer.h"
20
22
23QString QgsAffineTransformationAlgorithm::name() const
24{
25 return QStringLiteral( "affinetransform" );
26}
27
28QString QgsAffineTransformationAlgorithm::displayName() const
29{
30 return QObject::tr( "Affine transform" );
31}
32
33QStringList QgsAffineTransformationAlgorithm::tags() const
34{
35 return QObject::tr( "move,shift,transform,affine,scale,rotate,resize,matrix" ).split( ',' );
36}
37
38QString QgsAffineTransformationAlgorithm::group() const
39{
40 return QObject::tr( "Vector geometry" );
41}
42
43QString QgsAffineTransformationAlgorithm::groupId() const
44{
45 return QStringLiteral( "vectorgeometry" );
46}
47
48QString QgsAffineTransformationAlgorithm::outputName() const
49{
50 return QObject::tr( "Transformed" );
51}
52
53QString QgsAffineTransformationAlgorithm::shortHelpString() const
54{
55 return QObject::tr( "Applies an affine transformation to the geometries from a layer. Affine transformations can include "
56 "translation, scaling and rotation. The operations are performed in a scale, rotation, translation order." )
57 + QStringLiteral( "\n\n" )
58 + QObject::tr( "Z and M values present in the geometry can also be translated and scaled independently." );
59}
60
61QString QgsAffineTransformationAlgorithm::shortDescription() const
62{
63 return QObject::tr( "Applies an affine transformation to geometries." );
64}
65
66QgsAffineTransformationAlgorithm *QgsAffineTransformationAlgorithm::createInstance() const
67{
68 return new QgsAffineTransformationAlgorithm();
69}
70
71void QgsAffineTransformationAlgorithm::initParameters( const QVariantMap & )
72{
73 std::unique_ptr<QgsProcessingParameterDistance> xOffset = std::make_unique<QgsProcessingParameterDistance>( QStringLiteral( "DELTA_X" ), QObject::tr( "Translation (x-axis)" ), 0.0, QStringLiteral( "INPUT" ) );
74 xOffset->setIsDynamic( true );
75 xOffset->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "DELTA_X" ), QObject::tr( "Offset distance (x-axis)" ), QgsPropertyDefinition::Double ) );
76 xOffset->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
77 addParameter( xOffset.release() );
78
79 std::unique_ptr<QgsProcessingParameterDistance> yOffset = std::make_unique<QgsProcessingParameterDistance>( QStringLiteral( "DELTA_Y" ), QObject::tr( "Translation (y-axis)" ), 0.0, QStringLiteral( "INPUT" ) );
80 yOffset->setIsDynamic( true );
81 yOffset->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "DELTA_Y" ), QObject::tr( "Offset distance (y-axis)" ), QgsPropertyDefinition::Double ) );
82 yOffset->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
83 addParameter( yOffset.release() );
84
85 std::unique_ptr<QgsProcessingParameterNumber> zOffset = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral( "DELTA_Z" ), QObject::tr( "Translation (z-axis)" ), Qgis::ProcessingNumberParameterType::Double, 0.0 );
86 zOffset->setIsDynamic( true );
87 zOffset->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "DELTA_Z" ), QObject::tr( "Offset distance (z-axis)" ), QgsPropertyDefinition::Double ) );
88 zOffset->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
89 addParameter( zOffset.release() );
90
91 std::unique_ptr<QgsProcessingParameterNumber> mOffset = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral( "DELTA_M" ), QObject::tr( "Translation (m values)" ), Qgis::ProcessingNumberParameterType::Double, 0.0 );
92 mOffset->setIsDynamic( true );
93 mOffset->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "DELTA_M" ), QObject::tr( "Offset distance (m values)" ), QgsPropertyDefinition::Double ) );
94 mOffset->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
95 addParameter( mOffset.release() );
96
97 std::unique_ptr<QgsProcessingParameterNumber> xScale = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral( "SCALE_X" ), QObject::tr( "Scale factor (x-axis)" ), Qgis::ProcessingNumberParameterType::Double, 1.0 );
98 xScale->setIsDynamic( true );
99 xScale->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "SCALE_X" ), QObject::tr( "Scale factor (x-axis)" ), QgsPropertyDefinition::Double ) );
100 xScale->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
101 addParameter( xScale.release() );
102
103 std::unique_ptr<QgsProcessingParameterNumber> yScale = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral( "SCALE_Y" ), QObject::tr( "Scale factor (y-axis)" ), Qgis::ProcessingNumberParameterType::Double, 1.0 );
104 yScale->setIsDynamic( true );
105 yScale->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "SCALE_Y" ), QObject::tr( "Scale factor (y-axis)" ), QgsPropertyDefinition::Double ) );
106 yScale->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
107 addParameter( yScale.release() );
108
109 std::unique_ptr<QgsProcessingParameterNumber> zScale = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral( "SCALE_Z" ), QObject::tr( "Scale factor (z-axis)" ), Qgis::ProcessingNumberParameterType::Double, 1.0 );
110 zScale->setIsDynamic( true );
111 zScale->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "SCALE_Z" ), QObject::tr( "Scale factor (z-axis)" ), QgsPropertyDefinition::Double ) );
112 zScale->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
113 addParameter( zScale.release() );
114
115 std::unique_ptr<QgsProcessingParameterNumber> mScale = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral( "SCALE_M" ), QObject::tr( "Scale factor (m values)" ), Qgis::ProcessingNumberParameterType::Double, 1.0 );
116 mScale->setIsDynamic( true );
117 mScale->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "SCALE_M" ), QObject::tr( "Scale factor (m values)" ), QgsPropertyDefinition::Double ) );
118 mScale->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
119 addParameter( mScale.release() );
120
121 std::unique_ptr<QgsProcessingParameterNumber> rotation = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral( "ROTATION_Z" ), QObject::tr( "Rotation around z-axis (degrees counter-clockwise)" ), Qgis::ProcessingNumberParameterType::Double, 0.0 );
122 rotation->setIsDynamic( true );
123 rotation->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "ROTATION_Z" ), QObject::tr( "Rotation around z-axis (degrees counter-clockwise)" ), QgsPropertyDefinition::Double ) );
124 rotation->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
125 addParameter( rotation.release() );
126}
127
128bool QgsAffineTransformationAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
129{
130 mDeltaX = parameterAsDouble( parameters, QStringLiteral( "DELTA_X" ), context );
131 mDynamicDeltaX = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "DELTA_X" ) );
132 if ( mDynamicDeltaX )
133 mDeltaXProperty = parameters.value( QStringLiteral( "DELTA_X" ) ).value<QgsProperty>();
134
135 mDeltaY = parameterAsDouble( parameters, QStringLiteral( "DELTA_Y" ), context );
136 mDynamicDeltaY = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "DELTA_Y" ) );
137 if ( mDynamicDeltaY )
138 mDeltaYProperty = parameters.value( QStringLiteral( "DELTA_Y" ) ).value<QgsProperty>();
139
140 mDeltaZ = parameterAsDouble( parameters, QStringLiteral( "DELTA_Z" ), context );
141 mDynamicDeltaZ = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "DELTA_Z" ) );
142 if ( mDynamicDeltaZ )
143 mDeltaZProperty = parameters.value( QStringLiteral( "DELTA_Z" ) ).value<QgsProperty>();
144
145 mDeltaM = parameterAsDouble( parameters, QStringLiteral( "DELTA_M" ), context );
146 mDynamicDeltaM = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "DELTA_M" ) );
147 if ( mDynamicDeltaM )
148 mDeltaMProperty = parameters.value( QStringLiteral( "DELTA_M" ) ).value<QgsProperty>();
149
150 mScaleX = parameterAsDouble( parameters, QStringLiteral( "SCALE_X" ), context );
151 mDynamicScaleX = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "SCALE_X" ) );
152 if ( mDynamicScaleX )
153 mScaleXProperty = parameters.value( QStringLiteral( "SCALE_X" ) ).value<QgsProperty>();
154
155 mScaleY = parameterAsDouble( parameters, QStringLiteral( "SCALE_Y" ), context );
156 mDynamicScaleY = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "SCALE_Y" ) );
157 if ( mDynamicScaleY )
158 mScaleYProperty = parameters.value( QStringLiteral( "SCALE_Y" ) ).value<QgsProperty>();
159
160 mScaleZ = parameterAsDouble( parameters, QStringLiteral( "SCALE_Z" ), context );
161 mDynamicScaleZ = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "SCALE_Z" ) );
162 if ( mDynamicScaleZ )
163 mScaleZProperty = parameters.value( QStringLiteral( "SCALE_Z" ) ).value<QgsProperty>();
164
165 mScaleM = parameterAsDouble( parameters, QStringLiteral( "SCALE_M" ), context );
166 mDynamicScaleM = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "SCALE_M" ) );
167 if ( mDynamicScaleM )
168 mScaleMProperty = parameters.value( QStringLiteral( "SCALE_M" ) ).value<QgsProperty>();
169
170 mRotationZ = parameterAsDouble( parameters, QStringLiteral( "ROTATION_Z" ), context );
171 mDynamicRotationZ = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "ROTATION_Z" ) );
172 if ( mDynamicRotationZ )
173 mRotationZProperty = parameters.value( QStringLiteral( "ROTATION_Z" ) ).value<QgsProperty>();
174
175 return true;
176}
177
178QgsFeatureList QgsAffineTransformationAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
179{
180 QgsFeature f = feature;
181 if ( f.hasGeometry() )
182 {
183 QgsGeometry geometry = f.geometry();
184
185 double deltaX = mDeltaX;
186 if ( mDynamicDeltaX )
187 deltaX = mDeltaXProperty.valueAsDouble( context.expressionContext(), deltaX );
188 double deltaY = mDeltaY;
189 if ( mDynamicDeltaY )
190 deltaY = mDeltaYProperty.valueAsDouble( context.expressionContext(), deltaY );
191 double deltaZ = mDeltaZ;
192 if ( mDynamicDeltaZ )
193 deltaZ = mDeltaZProperty.valueAsDouble( context.expressionContext(), deltaZ );
194 double deltaM = mDeltaM;
195 if ( mDynamicDeltaM )
196 deltaM = mDeltaMProperty.valueAsDouble( context.expressionContext(), deltaM );
197
198 if ( deltaZ != 0.0 && !geometry.constGet()->is3D() )
199 geometry.get()->addZValue( 0 );
200 if ( deltaM != 0.0 && !geometry.constGet()->isMeasure() )
201 geometry.get()->addMValue( 0 );
202
203 double scaleX = mScaleX;
204 if ( mDynamicScaleX )
205 scaleX = mScaleXProperty.valueAsDouble( context.expressionContext(), scaleX );
206 double scaleY = mScaleY;
207 if ( mDynamicScaleY )
208 scaleY = mScaleYProperty.valueAsDouble( context.expressionContext(), scaleY );
209 double scaleZ = mScaleZ;
210 if ( mDynamicScaleZ )
211 scaleZ = mScaleZProperty.valueAsDouble( context.expressionContext(), scaleZ );
212 double scaleM = mScaleM;
213 if ( mDynamicScaleM )
214 scaleM = mScaleMProperty.valueAsDouble( context.expressionContext(), scaleM );
215
216 double rotationZ = mRotationZ;
217 if ( mDynamicRotationZ )
218 rotationZ = mRotationZProperty.valueAsDouble( context.expressionContext(), rotationZ );
219
220 QTransform transform;
221 transform.translate( deltaX, deltaY );
222 transform.rotate( rotationZ );
223 transform.scale( scaleX, scaleY );
224
225 geometry.transform( transform, deltaZ, scaleZ, deltaM, scaleM );
226 f.setGeometry( geometry );
227 }
228 return QgsFeatureList() << f;
229}
230
231Qgis::WkbType QgsAffineTransformationAlgorithm::outputWkbType( Qgis::WkbType inputWkbType ) const
232{
233 Qgis::WkbType wkb = inputWkbType;
234 if ( mDeltaZ != 0.0 )
235 wkb = QgsWkbTypes::addZ( wkb );
236 if ( mDeltaM != 0.0 )
237 wkb = QgsWkbTypes::addM( wkb );
238 return wkb;
239}
240
241
242bool QgsAffineTransformationAlgorithm::supportInPlaceEdit( const QgsMapLayer *l ) const
243{
244 const QgsVectorLayer *layer = qobject_cast<const QgsVectorLayer *>( l );
245 if ( !layer )
246 return false;
247
249 return false;
250
251 // If the type differs there is no sense in executing the algorithm and drop the result
252 const Qgis::WkbType inPlaceWkbType = layer->wkbType();
253 return inPlaceWkbType == outputWkbType( inPlaceWkbType );
254}
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.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
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.
A geometry is the spatial representation of a feature.
Qgis::GeometryOperationResult transform(const QgsCoordinateTransform &ct, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward, bool transformZ=false)
Transforms this geometry as described by the coordinate transform ct.
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.
Base class for all map layer types.
Definition qgsmaplayer.h:76
Contains information about the context in which a processing algorithm is executed.
QgsExpressionContext & expressionContext()
Returns the expression context.
bool supportInPlaceEdit(const QgsMapLayer *layer) const override
Checks whether this algorithm supports in-place editing on the given layer Default implementation for...
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
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.
Represents a vector layer which manages a vector based data sets.
Q_INVOKABLE Qgis::WkbType wkbType() const FINAL
Returns the WKBType or WKBUnknown in case of error.
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