QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
qgsalgorithmsetzvalue.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmsetzvalue.cpp
3 ---------------------
4 begin : November 2019
5 copyright : (C) 2019 by Alexander Bruy
6 email : alexander dot bruy 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 QgsSetZValueAlgorithm::name() const
25{
26 return QStringLiteral( "setzvalue" );
27}
28
29QString QgsSetZValueAlgorithm::displayName() const
30{
31 return QObject::tr( "Set Z value" );
32}
33
34QStringList QgsSetZValueAlgorithm::tags() const
35{
36 return QObject::tr( "set,add,z,25d,3d,values" ).split( ',' );
37}
38
39QString QgsSetZValueAlgorithm::group() const
40{
41 return QObject::tr( "Vector geometry" );
42}
43
44QString QgsSetZValueAlgorithm::groupId() const
45{
46 return QStringLiteral( "vectorgeometry" );
47}
48
49QString QgsSetZValueAlgorithm::shortHelpString() const
50{
51 return QObject::tr( "This algorithm sets the Z value for geometries in a layer.\n\n"
52 "If Z values already exist in the layer, they will be overwritten "
53 "with the new value. If no Z values exist, the geometry will be "
54 "upgraded to include Z values and the specified value used as "
55 "the initial Z value for all geometries." );
56}
57
58QString QgsSetZValueAlgorithm::shortDescription() const
59{
60 return QObject::tr( "Sets the Z value for geometries in a layer." );
61}
62
63QString QgsSetZValueAlgorithm::outputName() const
64{
65 return QObject::tr( "Z Added" );
66}
67
68QgsSetZValueAlgorithm *QgsSetZValueAlgorithm::createInstance() const
69{
70 return new QgsSetZValueAlgorithm();
71}
72
73bool QgsSetZValueAlgorithm::supportInPlaceEdit( const QgsMapLayer *l ) const
74{
75 const QgsVectorLayer *layer = qobject_cast<const QgsVectorLayer *>( l );
76 if ( !layer )
77 return false;
78
80}
81
82Qgis::ProcessingFeatureSourceFlags QgsSetZValueAlgorithm::sourceFlags() const
83{
85}
86
87Qgis::WkbType QgsSetZValueAlgorithm::outputWkbType( Qgis::WkbType type ) const
88{
89 return QgsWkbTypes::addZ( type );
90}
91
92void QgsSetZValueAlgorithm::initParameters( const QVariantMap & )
93{
94 auto zValueParam = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral( "Z_VALUE" ), QObject::tr( "Z Value" ), Qgis::ProcessingNumberParameterType::Double, 0.0 );
95 zValueParam->setIsDynamic( true );
96 zValueParam->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "Z_VALUE" ), QObject::tr( "Z Value" ), QgsPropertyDefinition::Double ) );
97 zValueParam->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
98 addParameter( zValueParam.release() );
99}
100
101bool QgsSetZValueAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
102{
103 mZValue = parameterAsDouble( parameters, QStringLiteral( "Z_VALUE" ), context );
104 mDynamicZValue = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "Z_VALUE" ) );
105 if ( mDynamicZValue )
106 mZValueProperty = parameters.value( QStringLiteral( "Z_VALUE" ) ).value<QgsProperty>();
107
108 return true;
109}
110
111QgsFeatureList QgsSetZValueAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
112{
113 QgsFeature f = feature;
114
115 if ( f.hasGeometry() )
116 {
117 std::unique_ptr<QgsAbstractGeometry> newGeometry( f.geometry().constGet()->clone() );
118 // addZValue won't alter existing Z values, so drop them first
119 if ( QgsWkbTypes::hasZ( newGeometry->wkbType() ) )
120 newGeometry->dropZValue();
121
122 double z = mZValue;
123 if ( mDynamicZValue )
124 z = mZValueProperty.valueAsDouble( context.expressionContext(), z );
125
126 newGeometry->addZValue( z );
127
128 f.setGeometry( QgsGeometry( std::move( newGeometry ) ) );
129 }
130
131 return QgsFeatureList() << f;
132}
133
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Definition qgis.h:3711
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:277
QFlags< ProcessingFeatureSourceFlag > ProcessingFeatureSourceFlags
Flags which control how QgsProcessingFeatureSource fetches features.
Definition qgis.h:3722
@ Double
Double/float values.
Definition qgis.h:3804
virtual QgsAbstractGeometry * clone() const =0
Clones the geometry by performing a deep copy.
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.
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
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.
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 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.
QList< QgsFeature > QgsFeatureList