QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 
18 #include "qgsalgorithmsetzvalue.h"
19 #include "qgsvectorlayer.h"
20 
22 
23 QString QgsSetZValueAlgorithm::name() const
24 {
25  return QStringLiteral( "setzvalue" );
26 }
27 
28 QString QgsSetZValueAlgorithm::displayName() const
29 {
30  return QObject::tr( "Set Z value" );
31 }
32 
33 QStringList QgsSetZValueAlgorithm::tags() const
34 {
35  return QObject::tr( "set,add,z,25d,3d,values" ).split( ',' );
36 }
37 
38 QString QgsSetZValueAlgorithm::group() const
39 {
40  return QObject::tr( "Vector geometry" );
41 }
42 
43 QString QgsSetZValueAlgorithm::groupId() const
44 {
45  return QStringLiteral( "vectorgeometry" );
46 }
47 
48 QString QgsSetZValueAlgorithm::shortHelpString() const
49 {
50  return QObject::tr( "This algorithm sets the Z value for geometries in a layer.\n\n"
51  "If Z values already exist in the layer, they will be overwritten "
52  "with the new value. If no Z values exist, the geometry will be "
53  "upgraded to include Z values and the specified value used as "
54  "the initial Z value for all geometries." );
55 }
56 
57 QString QgsSetZValueAlgorithm::outputName() const
58 {
59  return QObject::tr( "Z Added" );
60 }
61 
62 QgsSetZValueAlgorithm *QgsSetZValueAlgorithm::createInstance() const
63 {
64  return new QgsSetZValueAlgorithm();
65 }
66 
67 bool QgsSetZValueAlgorithm::supportInPlaceEdit( const QgsMapLayer *l ) const
68 {
69  const QgsVectorLayer *layer = qobject_cast< const QgsVectorLayer * >( l );
70  if ( !layer )
71  return false;
72 
74 }
75 
76 QgsProcessingFeatureSource::Flag QgsSetZValueAlgorithm::sourceFlags() const
77 {
79 }
80 
81 QgsWkbTypes::Type QgsSetZValueAlgorithm::outputWkbType( QgsWkbTypes::Type type ) const
82 {
83  return QgsWkbTypes::addZ( type );
84 }
85 
86 void QgsSetZValueAlgorithm::initParameters( const QVariantMap & )
87 {
88  auto zValueParam = qgis::make_unique < QgsProcessingParameterNumber >( QStringLiteral( "Z_VALUE" ), QObject::tr( "Z Value" ), QgsProcessingParameterNumber::Double, 0.0 );
89  zValueParam->setIsDynamic( true );
90  zValueParam->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "Z_VALUE" ), QObject::tr( "Z Value" ), QgsPropertyDefinition::Double ) );
91  zValueParam->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
92  addParameter( zValueParam.release() );
93 }
94 
95 bool QgsSetZValueAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
96 {
97  mZValue = parameterAsDouble( parameters, QStringLiteral( "Z_VALUE" ), context );
98  mDynamicZValue = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "Z_VALUE" ) );
99  if ( mDynamicZValue )
100  mZValueProperty = parameters.value( QStringLiteral( "Z_VALUE" ) ).value< QgsProperty >();
101 
102  return true;
103 }
104 
105 QgsFeatureList QgsSetZValueAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
106 {
107  QgsFeature f = feature;
108 
109  if ( f.hasGeometry() )
110  {
111  std::unique_ptr< QgsAbstractGeometry > newGeometry( f.geometry().constGet()->clone() );
112  // addZValue won't alter existing Z values, so drop them first
113  if ( QgsWkbTypes::hasZ( newGeometry->wkbType() ) )
114  newGeometry->dropZValue();
115 
116  double z = mZValue;
117  if ( mDynamicZValue )
118  z = mZValueProperty.valueAsDouble( context.expressionContext(), z );
119 
120  newGeometry->addZValue( z );
121 
122  f.setGeometry( QgsGeometry( std::move( newGeometry ) ) );
123  }
124 
125  return QgsFeatureList() << f;
126 }
127 
QgsProcessingParameterNumber::Double
@ Double
Double/float values.
Definition: qgsprocessingparameters.h:1846
QgsVectorLayer::wkbType
Q_INVOKABLE QgsWkbTypes::Type wkbType() const FINAL
Returns the WKBType or WKBUnknown in case of error.
Definition: qgsvectorlayer.cpp:664
QgsProperty
A store for object properties.
Definition: qgsproperty.h:231
QgsProcessingParameters::isDynamic
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: qgsprocessingparameters.cpp:111
QgsProcessingFeedback
Definition: qgsprocessingfeedback.h:37
qgsalgorithmsetzvalue.h
QgsFeature::geometry
QgsGeometry geometry
Definition: qgsfeature.h:71
QgsPropertyDefinition::Double
@ Double
Double value (including negative values)
Definition: qgsproperty.h:58
QgsProcessingFeatureSource::FlagSkipGeometryValidityChecks
@ FlagSkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Definition: qgsprocessingutils.h:475
QgsWkbTypes::Type
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:68
QgsWkbTypes::hasZ
static bool hasZ(Type type)
Tests whether a WKB type contains the z-dimension.
Definition: qgswkbtypes.h:1042
QgsProcessingFeatureBasedAlgorithm::supportInPlaceEdit
bool supportInPlaceEdit(const QgsMapLayer *layer) const override
Checks whether this algorithm supports in-place editing on the given layer Default implementation for...
Definition: qgsprocessingalgorithm.cpp:1004
QgsFeature::setGeometry
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
Definition: qgsfeature.cpp:137
QgsProcessingContext
Definition: qgsprocessingcontext.h:43
QgsAbstractGeometry::clone
virtual QgsAbstractGeometry * clone() const =0
Clones the geometry by performing a deep copy.
QgsFeatureList
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:572
QgsPropertyDefinition
Definition for a property.
Definition: qgsproperty.h:47
QgsGeometry::constGet
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
Definition: qgsgeometry.cpp:128
QgsWkbTypes::addZ
static Type addZ(Type type)
Adds the z dimension to a WKB type and returns the new type.
Definition: qgswkbtypes.h:1138
qgsvectorlayer.h
QgsProcessingFeatureSource::Flag
Flag
Flags controlling how QgsProcessingFeatureSource fetches features.
Definition: qgsprocessingutils.h:473
QgsGeometry
Definition: qgsgeometry.h:122
QgsVectorLayer
Definition: qgsvectorlayer.h:385
QgsFeature::hasGeometry
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:197
QgsMapLayer
Definition: qgsmaplayer.h:81
QgsProcessingContext::expressionContext
QgsExpressionContext & expressionContext()
Returns the expression context.
Definition: qgsprocessingcontext.h:119
QgsFeature
Definition: qgsfeature.h:55