QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
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
19
20#include "qgsvectorlayer.h"
21
23
24QString QgsTranslateAlgorithm::name() const
25{
26 return QStringLiteral( "translategeometry" );
27}
28
29QString QgsTranslateAlgorithm::displayName() const
30{
31 return QObject::tr( "Translate" );
32}
33
34QStringList QgsTranslateAlgorithm::tags() const
35{
36 return QObject::tr( "move,shift,transform,z,m,values,add" ).split( ',' );
37}
38
39QString QgsTranslateAlgorithm::group() const
40{
41 return QObject::tr( "Vector geometry" );
42}
43
44QString QgsTranslateAlgorithm::groupId() const
45{
46 return QStringLiteral( "vectorgeometry" );
47}
48
49QString QgsTranslateAlgorithm::outputName() const
50{
51 return QObject::tr( "Translated" );
52}
53
54QString QgsTranslateAlgorithm::shortHelpString() const
55{
56 return QObject::tr( "This algorithm moves the geometries within a layer, by offsetting them with a specified x and y displacement." )
57 + QStringLiteral( "\n\n" )
58 + QObject::tr( "Z and M values present in the geometry can also be translated." );
59}
60
61QString QgsTranslateAlgorithm::shortDescription() const
62{
63 return QObject::tr( "Moves the geometries within a layer, by offsetting them with a specified x, y, z or m displacement." );
64}
65
66QgsTranslateAlgorithm *QgsTranslateAlgorithm::createInstance() const
67{
68 return new QgsTranslateAlgorithm();
69}
70
71void QgsTranslateAlgorithm::initParameters( const QVariantMap & )
72{
73 auto xOffset = std::make_unique<QgsProcessingParameterDistance>( QStringLiteral( "DELTA_X" ), QObject::tr( "Offset distance (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 auto yOffset = std::make_unique<QgsProcessingParameterDistance>( QStringLiteral( "DELTA_Y" ), QObject::tr( "Offset distance (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 auto zOffset = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral( "DELTA_Z" ), QObject::tr( "Offset distance (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 auto mOffset = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral( "DELTA_M" ), QObject::tr( "Offset distance (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
98bool QgsTranslateAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
99{
100 mDeltaX = parameterAsDouble( parameters, QStringLiteral( "DELTA_X" ), context );
101 mDynamicDeltaX = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "DELTA_X" ) );
102 if ( mDynamicDeltaX )
103 mDeltaXProperty = parameters.value( QStringLiteral( "DELTA_X" ) ).value<QgsProperty>();
104
105 mDeltaY = parameterAsDouble( parameters, QStringLiteral( "DELTA_Y" ), context );
106 mDynamicDeltaY = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "DELTA_Y" ) );
107 if ( mDynamicDeltaY )
108 mDeltaYProperty = parameters.value( QStringLiteral( "DELTA_Y" ) ).value<QgsProperty>();
109
110 mDeltaZ = parameterAsDouble( parameters, QStringLiteral( "DELTA_Z" ), context );
111 mDynamicDeltaZ = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "DELTA_Z" ) );
112 if ( mDynamicDeltaZ )
113 mDeltaZProperty = parameters.value( QStringLiteral( "DELTA_Z" ) ).value<QgsProperty>();
114
115 mDeltaM = parameterAsDouble( parameters, QStringLiteral( "DELTA_M" ), context );
116 mDynamicDeltaM = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "DELTA_M" ) );
117 if ( mDynamicDeltaM )
118 mDeltaMProperty = parameters.value( QStringLiteral( "DELTA_M" ) ).value<QgsProperty>();
119
120 return true;
121}
122
123QgsFeatureList QgsTranslateAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
124{
125 QgsFeature f = feature;
126 if ( f.hasGeometry() )
127 {
128 QgsGeometry geometry = f.geometry();
129
130 double deltaX = mDeltaX;
131 if ( mDynamicDeltaX )
132 deltaX = mDeltaXProperty.valueAsDouble( context.expressionContext(), deltaX );
133 double deltaY = mDeltaY;
134 if ( mDynamicDeltaY )
135 deltaY = mDeltaYProperty.valueAsDouble( context.expressionContext(), deltaY );
136 double deltaZ = mDeltaZ;
137 if ( mDynamicDeltaZ )
138 deltaZ = mDeltaZProperty.valueAsDouble( context.expressionContext(), deltaZ );
139 double deltaM = mDeltaM;
140 if ( mDynamicDeltaM )
141 deltaM = mDeltaMProperty.valueAsDouble( context.expressionContext(), deltaM );
142
143 if ( QgsWkbTypes::hasZ( mOutputWkbType ) && !geometry.constGet()->is3D() )
144 geometry.get()->addZValue( 0 );
145 if ( QgsWkbTypes::hasM( mOutputWkbType ) && !geometry.constGet()->isMeasure() )
146 geometry.get()->addMValue( 0 );
147
148 geometry.translate( deltaX, deltaY, deltaZ, deltaM );
149 f.setGeometry( geometry );
150 }
151 return QgsFeatureList() << f;
152}
153
154Qgis::WkbType QgsTranslateAlgorithm::outputWkbType( Qgis::WkbType inputWkbType ) const
155{
156 mOutputWkbType = inputWkbType;
157 if ( mDynamicDeltaZ || mDeltaZ != 0.0 )
158 mOutputWkbType = QgsWkbTypes::addZ( mOutputWkbType );
159 if ( mDynamicDeltaM || mDeltaM != 0.0 )
160 mOutputWkbType = QgsWkbTypes::addM( mOutputWkbType );
161 return mOutputWkbType;
162}
163
164bool QgsTranslateAlgorithm::supportInPlaceEdit( const QgsMapLayer *l ) const
165{
166 const QgsVectorLayer *layer = qobject_cast<const QgsVectorLayer *>( l );
167 if ( !layer )
168 return false;
169
171 return false;
172
173 // Check if we can drop Z/M and still have some work done
174 if ( mDeltaX != 0.0 || mDeltaY != 0.0 )
175 return true;
176
177 // If the type differs there is no sense in executing the algorithm and drop the result
178 const Qgis::WkbType inPlaceWkbType = layer->wkbType();
179 return inPlaceWkbType == outputWkbType( inPlaceWkbType );
180}
181
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.
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.
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.
static Q_INVOKABLE bool hasZ(Qgis::WkbType type)
Tests whether a WKB type contains the z-dimension.
static Q_INVOKABLE bool hasM(Qgis::WkbType type)
Tests whether a WKB type contains m values.
QList< QgsFeature > QgsFeatureList