QGIS API Documentation 3.99.0-Master (09f76ad7019)
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
22#include <QString>
23
24using namespace Qt::StringLiterals;
25
27
28QString QgsTranslateAlgorithm::name() const
29{
30 return u"translategeometry"_s;
31}
32
33QString QgsTranslateAlgorithm::displayName() const
34{
35 return QObject::tr( "Translate" );
36}
37
38QStringList QgsTranslateAlgorithm::tags() const
39{
40 return QObject::tr( "move,shift,transform,z,m,values,add" ).split( ',' );
41}
42
43QString QgsTranslateAlgorithm::group() const
44{
45 return QObject::tr( "Vector geometry" );
46}
47
48QString QgsTranslateAlgorithm::groupId() const
49{
50 return u"vectorgeometry"_s;
51}
52
53QString QgsTranslateAlgorithm::outputName() const
54{
55 return QObject::tr( "Translated" );
56}
57
58QString QgsTranslateAlgorithm::shortHelpString() const
59{
60 return QObject::tr( "This algorithm moves the geometries within a layer, by offsetting them with a specified x and y displacement." )
61 + u"\n\n"_s
62 + QObject::tr( "Z and M values present in the geometry can also be translated." );
63}
64
65QString QgsTranslateAlgorithm::shortDescription() const
66{
67 return QObject::tr( "Moves the geometries within a layer, by offsetting them with a specified x, y, z or m displacement." );
68}
69
70QgsTranslateAlgorithm *QgsTranslateAlgorithm::createInstance() const
71{
72 return new QgsTranslateAlgorithm();
73}
74
75void QgsTranslateAlgorithm::initParameters( const QVariantMap & )
76{
77 auto xOffset = std::make_unique<QgsProcessingParameterDistance>( u"DELTA_X"_s, QObject::tr( "Offset distance (x-axis)" ), 0.0, u"INPUT"_s );
78 xOffset->setIsDynamic( true );
79 xOffset->setDynamicPropertyDefinition( QgsPropertyDefinition( u"DELTA_X"_s, QObject::tr( "Offset distance (x-axis)" ), QgsPropertyDefinition::Double ) );
80 xOffset->setDynamicLayerParameterName( u"INPUT"_s );
81 addParameter( xOffset.release() );
82
83 auto yOffset = std::make_unique<QgsProcessingParameterDistance>( u"DELTA_Y"_s, QObject::tr( "Offset distance (y-axis)" ), 0.0, u"INPUT"_s );
84 yOffset->setIsDynamic( true );
85 yOffset->setDynamicPropertyDefinition( QgsPropertyDefinition( u"DELTA_Y"_s, QObject::tr( "Offset distance (y-axis)" ), QgsPropertyDefinition::Double ) );
86 yOffset->setDynamicLayerParameterName( u"INPUT"_s );
87 addParameter( yOffset.release() );
88
89 auto zOffset = std::make_unique<QgsProcessingParameterNumber>( u"DELTA_Z"_s, QObject::tr( "Offset distance (z-axis)" ), Qgis::ProcessingNumberParameterType::Double, 0.0 );
90 zOffset->setIsDynamic( true );
91 zOffset->setDynamicPropertyDefinition( QgsPropertyDefinition( u"DELTA_Z"_s, QObject::tr( "Offset distance (z-axis)" ), QgsPropertyDefinition::Double ) );
92 zOffset->setDynamicLayerParameterName( u"INPUT"_s );
93 addParameter( zOffset.release() );
94
95 auto mOffset = std::make_unique<QgsProcessingParameterNumber>( u"DELTA_M"_s, QObject::tr( "Offset distance (m values)" ), Qgis::ProcessingNumberParameterType::Double, 0.0 );
96 mOffset->setIsDynamic( true );
97 mOffset->setDynamicPropertyDefinition( QgsPropertyDefinition( u"DELTA_M"_s, QObject::tr( "Offset distance (m values)" ), QgsPropertyDefinition::Double ) );
98 mOffset->setDynamicLayerParameterName( u"INPUT"_s );
99 addParameter( mOffset.release() );
100}
101
102bool QgsTranslateAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
103{
104 mDeltaX = parameterAsDouble( parameters, u"DELTA_X"_s, context );
105 mDynamicDeltaX = QgsProcessingParameters::isDynamic( parameters, u"DELTA_X"_s );
106 if ( mDynamicDeltaX )
107 mDeltaXProperty = parameters.value( u"DELTA_X"_s ).value<QgsProperty>();
108
109 mDeltaY = parameterAsDouble( parameters, u"DELTA_Y"_s, context );
110 mDynamicDeltaY = QgsProcessingParameters::isDynamic( parameters, u"DELTA_Y"_s );
111 if ( mDynamicDeltaY )
112 mDeltaYProperty = parameters.value( u"DELTA_Y"_s ).value<QgsProperty>();
113
114 mDeltaZ = parameterAsDouble( parameters, u"DELTA_Z"_s, context );
115 mDynamicDeltaZ = QgsProcessingParameters::isDynamic( parameters, u"DELTA_Z"_s );
116 if ( mDynamicDeltaZ )
117 mDeltaZProperty = parameters.value( u"DELTA_Z"_s ).value<QgsProperty>();
118
119 mDeltaM = parameterAsDouble( parameters, u"DELTA_M"_s, context );
120 mDynamicDeltaM = QgsProcessingParameters::isDynamic( parameters, u"DELTA_M"_s );
121 if ( mDynamicDeltaM )
122 mDeltaMProperty = parameters.value( u"DELTA_M"_s ).value<QgsProperty>();
123
124 return true;
125}
126
127QgsFeatureList QgsTranslateAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
128{
129 QgsFeature f = feature;
130 if ( f.hasGeometry() )
131 {
132 QgsGeometry geometry = f.geometry();
133
134 double deltaX = mDeltaX;
135 if ( mDynamicDeltaX )
136 deltaX = mDeltaXProperty.valueAsDouble( context.expressionContext(), deltaX );
137 double deltaY = mDeltaY;
138 if ( mDynamicDeltaY )
139 deltaY = mDeltaYProperty.valueAsDouble( context.expressionContext(), deltaY );
140 double deltaZ = mDeltaZ;
141 if ( mDynamicDeltaZ )
142 deltaZ = mDeltaZProperty.valueAsDouble( context.expressionContext(), deltaZ );
143 double deltaM = mDeltaM;
144 if ( mDynamicDeltaM )
145 deltaM = mDeltaMProperty.valueAsDouble( context.expressionContext(), deltaM );
146
147 if ( QgsWkbTypes::hasZ( mOutputWkbType ) && !geometry.constGet()->is3D() )
148 geometry.get()->addZValue( 0 );
149 if ( QgsWkbTypes::hasM( mOutputWkbType ) && !geometry.constGet()->isMeasure() )
150 geometry.get()->addMValue( 0 );
151
152 geometry.translate( deltaX, deltaY, deltaZ, deltaM );
153 f.setGeometry( geometry );
154 }
155 return QgsFeatureList() << f;
156}
157
158Qgis::WkbType QgsTranslateAlgorithm::outputWkbType( Qgis::WkbType inputWkbType ) const
159{
160 mOutputWkbType = inputWkbType;
161 if ( mDynamicDeltaZ || mDeltaZ != 0.0 )
162 mOutputWkbType = QgsWkbTypes::addZ( mOutputWkbType );
163 if ( mDynamicDeltaM || mDeltaM != 0.0 )
164 mOutputWkbType = QgsWkbTypes::addM( mOutputWkbType );
165 return mOutputWkbType;
166}
167
168bool QgsTranslateAlgorithm::supportInPlaceEdit( const QgsMapLayer *l ) const
169{
170 const QgsVectorLayer *layer = qobject_cast<const QgsVectorLayer *>( l );
171 if ( !layer )
172 return false;
173
175 return false;
176
177 // Check if we can drop Z/M and still have some work done
178 if ( mDeltaX != 0.0 || mDeltaY != 0.0 )
179 return true;
180
181 // If the type differs there is no sense in executing the algorithm and drop the result
182 const Qgis::WkbType inPlaceWkbType = layer->wkbType();
183 return inPlaceWkbType == outputWkbType( inPlaceWkbType );
184}
185
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:280
@ Double
Double/float values.
Definition qgis.h:3875
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:60
QgsGeometry geometry
Definition qgsfeature.h:71
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:83
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:47
@ Double
Double value (including negative values).
Definition qgsproperty.h:57
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