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