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