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