QGIS API Documentation  3.0.2-Girona (307d082)
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  addParameter( new QgsProcessingParameterNumber( QStringLiteral( "DELTA_X" ),
67  QObject::tr( "Offset distance (x-axis)" ), QgsProcessingParameterNumber::Double,
68  0.0 ) );
69  addParameter( new QgsProcessingParameterNumber( QStringLiteral( "DELTA_Y" ),
70  QObject::tr( "Offset distance (y-axis)" ), QgsProcessingParameterNumber::Double,
71  0.0 ) );
72  addParameter( new QgsProcessingParameterNumber( QStringLiteral( "DELTA_Z" ),
73  QObject::tr( "Offset distance (z-axis)" ), QgsProcessingParameterNumber::Double,
74  0.0 ) );
75  addParameter( new QgsProcessingParameterNumber( QStringLiteral( "DELTA_M" ),
76  QObject::tr( "Offset distance (m values)" ), QgsProcessingParameterNumber::Double,
77  0.0 ) );
78 }
79 
80 bool QgsTranslateAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
81 {
82  mDeltaX = parameterAsDouble( parameters, QStringLiteral( "DELTA_X" ), context );
83  mDeltaY = parameterAsDouble( parameters, QStringLiteral( "DELTA_Y" ), context );
84  mDeltaZ = parameterAsDouble( parameters, QStringLiteral( "DELTA_Z" ), context );
85  mDeltaM = parameterAsDouble( parameters, QStringLiteral( "DELTA_M" ), context );
86 
87  return true;
88 }
89 
90 QgsFeatureList QgsTranslateAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &, QgsProcessingFeedback * )
91 {
92  QgsFeature f = feature;
93  if ( f.hasGeometry() )
94  {
95  QgsGeometry geometry = f.geometry();
96  if ( mDeltaZ != 0 && !geometry.constGet()->is3D() )
97  geometry.get()->addZValue( 0 );
98  if ( mDeltaM != 0 && !geometry.constGet()->isMeasure() )
99  geometry.get()->addMValue( 0 );
100 
101  geometry.translate( mDeltaX, mDeltaY, mDeltaZ, mDeltaM );
102  f.setGeometry( geometry );
103  }
104  return QgsFeatureList() << f;
105 }
106 
107 QgsWkbTypes::Type QgsTranslateAlgorithm::outputWkbType( QgsWkbTypes::Type inputWkbType ) const
108 {
109  QgsWkbTypes::Type wkb = inputWkbType;
110  if ( mDeltaZ != 0 )
111  wkb = QgsWkbTypes::addZ( wkb );
112  if ( mDeltaM != 0 )
113  wkb = QgsWkbTypes::addM( wkb );
114  return wkb;
115 }
116 
118 
119 
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:111
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 numeric parameter for processing algorithms.
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.
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.