QGIS API Documentation  3.2.0-Bonn (bc43194)
qgsalgorithmtranslate.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsalgorithmtranslate.cpp
3  ---------------------
4  begin : November 2017
5  copyright : (C) 2017 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 
18 #include "qgsalgorithmtranslate.h"
19 
21 
22 QString QgsTranslateAlgorithm::name() const
23 {
24  return QStringLiteral( "translategeometry" );
25 }
26 
27 QString QgsTranslateAlgorithm::displayName() const
28 {
29  return QObject::tr( "Translate" );
30 }
31 
32 QStringList QgsTranslateAlgorithm::tags() const
33 {
34  return QObject::tr( "move,shift,transform,z,m,values,add" ).split( ',' );
35 }
36 
37 QString QgsTranslateAlgorithm::group() const
38 {
39  return QObject::tr( "Vector geometry" );
40 }
41 
42 QString QgsTranslateAlgorithm::groupId() const
43 {
44  return QStringLiteral( "vectorgeometry" );
45 }
46 
47 QString QgsTranslateAlgorithm::outputName() const
48 {
49  return QObject::tr( "Translated" );
50 }
51 
52 QString QgsTranslateAlgorithm::shortHelpString() const
53 {
54  return QObject::tr( "This algorithm moves the geometries within a layer, by offsetting them with a specified x and y displacement." )
55  + QStringLiteral( "\n\n" )
56  + QObject::tr( "Z and M values present in the geometry can also be translated." );
57 }
58 
59 QgsTranslateAlgorithm *QgsTranslateAlgorithm::createInstance() const
60 {
61  return new QgsTranslateAlgorithm();
62 }
63 
64 void QgsTranslateAlgorithm::initParameters( const QVariantMap & )
65 {
66  std::unique_ptr< QgsProcessingParameterDistance > xOffset = qgis::make_unique< QgsProcessingParameterDistance >( QStringLiteral( "DELTA_X" ),
67  QObject::tr( "Offset distance (x-axis)" ),
68  0.0, QStringLiteral( "INPUT" ) );
69  xOffset->setIsDynamic( true );
70  xOffset->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "DELTA_X" ), QObject::tr( "Offset distance (x-axis)" ), QgsPropertyDefinition::Double ) );
71  xOffset->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
72  addParameter( xOffset.release() );
73 
74  std::unique_ptr< QgsProcessingParameterDistance > yOffset = qgis::make_unique< QgsProcessingParameterDistance >( QStringLiteral( "DELTA_Y" ),
75  QObject::tr( "Offset distance (y-axis)" ),
76  0.0, QStringLiteral( "INPUT" ) );
77  yOffset->setIsDynamic( true );
78  yOffset->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "DELTA_Y" ), QObject::tr( "Offset distance (y-axis)" ), QgsPropertyDefinition::Double ) );
79  yOffset->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
80  addParameter( yOffset.release() );
81 
82  std::unique_ptr< QgsProcessingParameterNumber > zOffset = qgis::make_unique< QgsProcessingParameterNumber >( QStringLiteral( "DELTA_Z" ),
83  QObject::tr( "Offset distance (z-axis)" ), QgsProcessingParameterNumber::Double,
84  0.0 );
85  zOffset->setIsDynamic( true );
86  zOffset->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "DELTA_Z" ), QObject::tr( "Offset distance (z-axis)" ), QgsPropertyDefinition::Double ) );
87  zOffset->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
88  addParameter( zOffset.release() );
89 
90  std::unique_ptr< QgsProcessingParameterNumber > mOffset = qgis::make_unique< QgsProcessingParameterNumber >( QStringLiteral( "DELTA_M" ),
91  QObject::tr( "Offset distance (m values)" ), QgsProcessingParameterNumber::Double,
92  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 
99 bool QgsTranslateAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
100 {
101  mDeltaX = parameterAsDouble( parameters, QStringLiteral( "DELTA_X" ), context );
102  mDynamicDeltaX = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "DELTA_X" ) );
103  if ( mDynamicDeltaX )
104  mDeltaXProperty = parameters.value( QStringLiteral( "DELTA_X" ) ).value< QgsProperty >();
105 
106  mDeltaY = parameterAsDouble( parameters, QStringLiteral( "DELTA_Y" ), context );
107  mDynamicDeltaY = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "DELTA_Y" ) );
108  if ( mDynamicDeltaY )
109  mDeltaYProperty = parameters.value( QStringLiteral( "DELTA_Y" ) ).value< QgsProperty >();
110 
111  mDeltaZ = parameterAsDouble( parameters, QStringLiteral( "DELTA_Z" ), context );
112  mDynamicDeltaZ = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "DELTA_Z" ) );
113  if ( mDynamicDeltaZ )
114  mDeltaZProperty = parameters.value( QStringLiteral( "DELTA_Z" ) ).value< QgsProperty >();
115 
116  mDeltaM = parameterAsDouble( parameters, QStringLiteral( "DELTA_M" ), context );
117  mDynamicDeltaM = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "DELTA_M" ) );
118  if ( mDynamicDeltaM )
119  mDeltaMProperty = parameters.value( QStringLiteral( "DELTA_M" ) ).value< QgsProperty >();
120 
121  return true;
122 }
123 
124 QgsFeatureList QgsTranslateAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
125 {
126  QgsFeature f = feature;
127  if ( f.hasGeometry() )
128  {
129  QgsGeometry geometry = f.geometry();
130 
131  double deltaX = mDeltaX;
132  if ( mDynamicDeltaX )
133  deltaX = mDeltaXProperty.valueAsDouble( context.expressionContext(), deltaX );
134  double deltaY = mDeltaY;
135  if ( mDynamicDeltaY )
136  deltaY = mDeltaYProperty.valueAsDouble( context.expressionContext(), deltaY );
137  double deltaZ = mDeltaZ;
138  if ( mDynamicDeltaZ )
139  deltaZ = mDeltaZProperty.valueAsDouble( context.expressionContext(), deltaZ );
140  double deltaM = mDeltaM;
141  if ( mDynamicDeltaM )
142  deltaM = mDeltaMProperty.valueAsDouble( context.expressionContext(), deltaM );
143 
144  if ( deltaZ != 0 && !geometry.constGet()->is3D() )
145  geometry.get()->addZValue( 0 );
146  if ( deltaM != 0 && !geometry.constGet()->isMeasure() )
147  geometry.get()->addMValue( 0 );
148 
149  geometry.translate( deltaX, deltaY, deltaZ, deltaM );
150  f.setGeometry( geometry );
151  }
152  return QgsFeatureList() << f;
153 }
154 
155 QgsWkbTypes::Type QgsTranslateAlgorithm::outputWkbType( QgsWkbTypes::Type inputWkbType ) const
156 {
157  QgsWkbTypes::Type wkb = inputWkbType;
158  if ( mDeltaZ != 0 )
159  wkb = QgsWkbTypes::addZ( wkb );
160  if ( mDeltaM != 0 )
161  wkb = QgsWkbTypes::addM( wkb );
162  return wkb;
163 }
164 
166 
167 
bool isMeasure() const
Returns true if the geometry contains m values.
Base class for providing feedback from a processing algorithm.
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:549
virtual bool addMValue(double mValue=0)=0
Adds a measure to the geometry, initialized to a preset value.
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:104
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:62
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:190
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:67
static Type addM(Type type)
Adds the m dimension to a WKB type and returns the new type.
Definition: qgswkbtypes.h:889
static Type addZ(Type type)
Adds the z dimension to a WKB type and returns the new type.
Definition: qgswkbtypes.h:864
QgsGeometry geometry() const
Returns the geometry associated with this feature.
Definition: qgsfeature.cpp:101
A store for object properties.
Definition: qgsproperty.h:229
Double value (including negative values)
Definition: qgsproperty.h:57
QgsExpressionContext & expressionContext()
Returns the expression context.
Definition for a property.
Definition: qgsproperty.h:46
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.
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.
virtual bool addZValue(double zValue=0)=0
Adds a z-dimension to the geometry, initialized to a preset value.
void setGeometry(const QgsGeometry &geometry)
Set the feature&#39;s geometry.
Definition: qgsfeature.cpp:137
OperationResult 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.
bool is3D() const
Returns true if the geometry is 3D and contains a z-value.
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...