QGIS API Documentation 3.43.0-Master (e01d6d7c4c0)
qgsalgorithmgeometrybyexpression.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmgeometrybyexpression.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#include "qgsvariantutils.h"
20
22
23QString QgsGeometryByExpressionAlgorithm::name() const
24{
25 return QStringLiteral( "geometrybyexpression" );
26}
27
28QString QgsGeometryByExpressionAlgorithm::displayName() const
29{
30 return QObject::tr( "Geometry by expression" );
31}
32
33QStringList QgsGeometryByExpressionAlgorithm::tags() const
34{
35 return QObject::tr( "geometry,expression,create,modify,update" ).split( ',' );
36}
37
38QString QgsGeometryByExpressionAlgorithm::group() const
39{
40 return QObject::tr( "Vector geometry" );
41}
42
43QString QgsGeometryByExpressionAlgorithm::groupId() const
44{
45 return QStringLiteral( "vectorgeometry" );
46}
47
48QString QgsGeometryByExpressionAlgorithm::outputName() const
49{
50 return QObject::tr( "Modified geometry" );
51}
52
53QString QgsGeometryByExpressionAlgorithm::shortHelpString() const
54{
55 return QObject::tr( "This algorithm updates existing geometries (or creates new geometries) for input "
56 "features by use of a QGIS expression. This allows complex geometry modifications "
57 "which can utilize all the flexibility of the QGIS expression engine to manipulate "
58 "and create geometries for output features.\n\n"
59 "For help with QGIS expression functions, see the inbuilt help for specific functions "
60 "which is available in the expression builder." );
61}
62
63QString QgsGeometryByExpressionAlgorithm::shortDescription() const
64{
65 return QObject::tr( "Updates existing geometries (or creates new geometries) for input "
66 "features by use of a QGIS expression." );
67}
68
69QgsGeometryByExpressionAlgorithm *QgsGeometryByExpressionAlgorithm::createInstance() const
70{
71 return new QgsGeometryByExpressionAlgorithm();
72}
73
74QList<int> QgsGeometryByExpressionAlgorithm::inputLayerTypes() const
75{
76 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::Vector );
77}
78
79Qgis::WkbType QgsGeometryByExpressionAlgorithm::outputWkbType( Qgis::WkbType ) const
80{
81 return mWkbType;
82}
83
84Qgis::ProcessingFeatureSourceFlags QgsGeometryByExpressionAlgorithm::sourceFlags() const
85{
87}
88
89void QgsGeometryByExpressionAlgorithm::initParameters( const QVariantMap & )
90{
91 addParameter( new QgsProcessingParameterEnum( QStringLiteral( "OUTPUT_GEOMETRY" ), QObject::tr( "Output geometry type" ), QStringList() << QObject::tr( "Polygon" ) << QObject::tr( "Line" ) << QObject::tr( "Point" ), false, 0 ) );
92 addParameter( new QgsProcessingParameterBoolean( QStringLiteral( "WITH_Z" ), QObject::tr( "Output geometry has z dimension" ), false ) );
93 addParameter( new QgsProcessingParameterBoolean( QStringLiteral( "WITH_M" ), QObject::tr( "Output geometry has m values" ), false ) );
94 addParameter( new QgsProcessingParameterExpression( QStringLiteral( "EXPRESSION" ), QObject::tr( "Geometry expression" ), QStringLiteral( "@geometry" ), QStringLiteral( "INPUT" ) ) );
95}
96
97bool QgsGeometryByExpressionAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
98{
99 const int geometryType = parameterAsInt( parameters, QStringLiteral( "OUTPUT_GEOMETRY" ), context );
100 switch ( geometryType )
101 {
102 case 0:
103 mWkbType = Qgis::WkbType::Polygon;
104 break;
105 case 1:
106 mWkbType = Qgis::WkbType::LineString;
107 break;
108 case 2:
109 mWkbType = Qgis::WkbType::Point;
110 break;
111 }
112
113 if ( parameterAsBoolean( parameters, QStringLiteral( "WITH_Z" ), context ) )
114 {
115 mWkbType = QgsWkbTypes::addZ( mWkbType );
116 }
117 if ( parameterAsBoolean( parameters, QStringLiteral( "WITH_M" ), context ) )
118 {
119 mWkbType = QgsWkbTypes::addM( mWkbType );
120 }
121
122 mExpression = QgsExpression( parameterAsString( parameters, QStringLiteral( "EXPRESSION" ), context ) );
123 if ( mExpression.hasParserError() )
124 {
125 feedback->reportError( mExpression.parserErrorString() );
126 return false;
127 }
128
129 mExpressionContext = createExpressionContext( parameters, context );
130 mExpression.prepare( &mExpressionContext );
131
132 return true;
133}
134
135QgsFeatureList QgsGeometryByExpressionAlgorithm::processFeature( const QgsFeature &f, QgsProcessingContext &, QgsProcessingFeedback * )
136{
137 QgsFeature feature = f;
138 mExpressionContext.setFeature( feature );
139 const QVariant value = mExpression.evaluate( &mExpressionContext );
140
141 if ( mExpression.hasEvalError() )
142 {
143 throw QgsProcessingException( QObject::tr( "Evaluation error: %1" ).arg( mExpression.evalErrorString() ) );
144 }
145
146 if ( QgsVariantUtils::isNull( value ) )
147 {
148 feature.setGeometry( QgsGeometry() );
149 }
150 else
151 {
152 if ( value.userType() == qMetaTypeId<QgsGeometry>() )
153 {
154 const QgsGeometry geom = value.value<QgsGeometry>();
155 feature.setGeometry( geom );
156 }
157 else
158 {
159 throw QgsProcessingException( QObject::tr( "%1 is not a geometry" ).arg( value.toString() ) );
160 }
161 }
162
163 return QgsFeatureList() << feature;
164}
165
@ Vector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:256
@ LineString
LineString.
@ Polygon
Polygon.
QFlags< ProcessingFeatureSourceFlag > ProcessingFeatureSourceFlags
Flags which control how QgsProcessingFeatureSource fetches features.
Definition qgis.h:3588
Handles parsing and evaluation of expressions (formerly called "search strings").
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
A geometry is the spatial representation of a feature.
Contains information about the context in which a processing algorithm is executed.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
virtual void reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.
A boolean parameter for processing algorithms.
An enum based parameter for processing algorithms, allowing for selection from predefined values.
An expression parameter for processing algorithms.
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.
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