QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
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
20#include "qgsvariantutils.h"
21
23
24QString QgsGeometryByExpressionAlgorithm::name() const
25{
26 return QStringLiteral( "geometrybyexpression" );
27}
28
29QString QgsGeometryByExpressionAlgorithm::displayName() const
30{
31 return QObject::tr( "Geometry by expression" );
32}
33
34QStringList QgsGeometryByExpressionAlgorithm::tags() const
35{
36 return QObject::tr( "geometry,expression,create,modify,update" ).split( ',' );
37}
38
39QString QgsGeometryByExpressionAlgorithm::group() const
40{
41 return QObject::tr( "Vector geometry" );
42}
43
44QString QgsGeometryByExpressionAlgorithm::groupId() const
45{
46 return QStringLiteral( "vectorgeometry" );
47}
48
49QString QgsGeometryByExpressionAlgorithm::outputName() const
50{
51 return QObject::tr( "Modified geometry" );
52}
53
54QString QgsGeometryByExpressionAlgorithm::shortHelpString() const
55{
56 return QObject::tr( "This algorithm updates existing geometries (or creates new geometries) for input "
57 "features by use of a QGIS expression. This allows complex geometry modifications "
58 "which can utilize all the flexibility of the QGIS expression engine to manipulate "
59 "and create geometries for output features.\n\n"
60 "For help with QGIS expression functions, see the inbuilt help for specific functions "
61 "which is available in the expression builder." );
62}
63
64QString QgsGeometryByExpressionAlgorithm::shortDescription() const
65{
66 return QObject::tr( "Updates existing geometries (or creates new geometries) for input "
67 "features by use of a QGIS expression." );
68}
69
70QgsGeometryByExpressionAlgorithm *QgsGeometryByExpressionAlgorithm::createInstance() const
71{
72 return new QgsGeometryByExpressionAlgorithm();
73}
74
75QList<int> QgsGeometryByExpressionAlgorithm::inputLayerTypes() const
76{
77 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::Vector );
78}
79
80Qgis::WkbType QgsGeometryByExpressionAlgorithm::outputWkbType( Qgis::WkbType ) const
81{
82 return mWkbType;
83}
84
85Qgis::ProcessingFeatureSourceFlags QgsGeometryByExpressionAlgorithm::sourceFlags() const
86{
88}
89
90void QgsGeometryByExpressionAlgorithm::initParameters( const QVariantMap & )
91{
92 addParameter( new QgsProcessingParameterEnum( QStringLiteral( "OUTPUT_GEOMETRY" ), QObject::tr( "Output geometry type" ), QStringList() << QObject::tr( "Polygon" ) << QObject::tr( "Line" ) << QObject::tr( "Point" ), false, 0 ) );
93 addParameter( new QgsProcessingParameterBoolean( QStringLiteral( "WITH_Z" ), QObject::tr( "Output geometry has z dimension" ), false ) );
94 addParameter( new QgsProcessingParameterBoolean( QStringLiteral( "WITH_M" ), QObject::tr( "Output geometry has m values" ), false ) );
95 addParameter( new QgsProcessingParameterExpression( QStringLiteral( "EXPRESSION" ), QObject::tr( "Geometry expression" ), QStringLiteral( "@geometry" ), QStringLiteral( "INPUT" ) ) );
96}
97
98bool QgsGeometryByExpressionAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
99{
100 const int geometryType = parameterAsInt( parameters, QStringLiteral( "OUTPUT_GEOMETRY" ), context );
101 switch ( geometryType )
102 {
103 case 0:
104 mWkbType = Qgis::WkbType::Polygon;
105 break;
106 case 1:
107 mWkbType = Qgis::WkbType::LineString;
108 break;
109 case 2:
110 mWkbType = Qgis::WkbType::Point;
111 break;
112 }
113
114 if ( parameterAsBoolean( parameters, QStringLiteral( "WITH_Z" ), context ) )
115 {
116 mWkbType = QgsWkbTypes::addZ( mWkbType );
117 }
118 if ( parameterAsBoolean( parameters, QStringLiteral( "WITH_M" ), context ) )
119 {
120 mWkbType = QgsWkbTypes::addM( mWkbType );
121 }
122
123 mExpression = QgsExpression( parameterAsString( parameters, QStringLiteral( "EXPRESSION" ), context ) );
124 if ( mExpression.hasParserError() )
125 {
126 feedback->reportError( mExpression.parserErrorString() );
127 return false;
128 }
129
130 mExpressionContext = createExpressionContext( parameters, context );
131 mExpression.prepare( &mExpressionContext );
132
133 return true;
134}
135
136QgsFeatureList QgsGeometryByExpressionAlgorithm::processFeature( const QgsFeature &f, QgsProcessingContext &, QgsProcessingFeedback * )
137{
138 QgsFeature feature = f;
139 mExpressionContext.setFeature( feature );
140 const QVariant value = mExpression.evaluate( &mExpressionContext );
141
142 if ( mExpression.hasEvalError() )
143 {
144 throw QgsProcessingException( QObject::tr( "Evaluation error: %1" ).arg( mExpression.evalErrorString() ) );
145 }
146
147 if ( QgsVariantUtils::isNull( value ) )
148 {
149 feature.setGeometry( QgsGeometry() );
150 }
151 else
152 {
153 if ( value.userType() == qMetaTypeId<QgsGeometry>() )
154 {
155 const QgsGeometry geom = value.value<QgsGeometry>();
156 feature.setGeometry( geom );
157 }
158 else
159 {
160 throw QgsProcessingException( QObject::tr( "%1 is not a geometry" ).arg( value.toString() ) );
161 }
162 }
163
164 return QgsFeatureList() << feature;
165}
166
@ Vector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
Definition qgis.h:3539
@ 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
@ Point
Point.
Definition qgis.h:279
@ LineString
LineString.
Definition qgis.h:280
@ Polygon
Polygon.
Definition qgis.h:281
QFlags< ProcessingFeatureSourceFlag > ProcessingFeatureSourceFlags
Flags which control how QgsProcessingFeatureSource fetches features.
Definition qgis.h:3722
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