QGIS API Documentation 3.99.0-Master (09f76ad7019)
Loading...
Searching...
No Matches
qgsalgorithmsetmvalue.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmsetmvalue.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
22#include <QString>
23
24using namespace Qt::StringLiterals;
25
27
28QString QgsSetMValueAlgorithm::name() const
29{
30 return u"setmvalue"_s;
31}
32
33QString QgsSetMValueAlgorithm::displayName() const
34{
35 return QObject::tr( "Set M value" );
36}
37
38QStringList QgsSetMValueAlgorithm::tags() const
39{
40 return QObject::tr( "set,add,m,measure,values" ).split( ',' );
41}
42
43QString QgsSetMValueAlgorithm::group() const
44{
45 return QObject::tr( "Vector geometry" );
46}
47
48QString QgsSetMValueAlgorithm::groupId() const
49{
50 return u"vectorgeometry"_s;
51}
52
53QString QgsSetMValueAlgorithm::shortHelpString() const
54{
55 return QObject::tr( "This algorithm sets the M value for geometries in a layer.\n\n"
56 "If M values already exist in the layer, they will be overwritten "
57 "with the new value. If no M values exist, the geometry will be "
58 "upgraded to include M values and the specified value used as "
59 "the initial M value for all geometries." );
60}
61
62QString QgsSetMValueAlgorithm::shortDescription() const
63{
64 return QObject::tr( "Sets the M value for geometries in a layer." );
65}
66
67QString QgsSetMValueAlgorithm::outputName() const
68{
69 return QObject::tr( "M Added" );
70}
71
72QgsSetMValueAlgorithm *QgsSetMValueAlgorithm::createInstance() const
73{
74 return new QgsSetMValueAlgorithm();
75}
76
77bool QgsSetMValueAlgorithm::supportInPlaceEdit( const QgsMapLayer *l ) const
78{
79 const QgsVectorLayer *layer = qobject_cast<const QgsVectorLayer *>( l );
80 if ( !layer )
81 return false;
82
84}
85
86Qgis::ProcessingFeatureSourceFlags QgsSetMValueAlgorithm::sourceFlags() const
87{
89}
90
91Qgis::WkbType QgsSetMValueAlgorithm::outputWkbType( Qgis::WkbType type ) const
92{
93 return QgsWkbTypes::addM( type );
94}
95
96void QgsSetMValueAlgorithm::initParameters( const QVariantMap & )
97{
98 auto mValueParam = std::make_unique<QgsProcessingParameterNumber>( u"M_VALUE"_s, QObject::tr( "M Value" ), Qgis::ProcessingNumberParameterType::Double, 0.0 );
99 mValueParam->setIsDynamic( true );
100 mValueParam->setDynamicPropertyDefinition( QgsPropertyDefinition( u"M_VALUE"_s, QObject::tr( "M Value" ), QgsPropertyDefinition::Double ) );
101 mValueParam->setDynamicLayerParameterName( u"INPUT"_s );
102 addParameter( mValueParam.release() );
103}
104
105bool QgsSetMValueAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
106{
107 mMValue = parameterAsDouble( parameters, u"M_VALUE"_s, context );
108 mDynamicMValue = QgsProcessingParameters::isDynamic( parameters, u"M_VALUE"_s );
109 if ( mDynamicMValue )
110 mMValueProperty = parameters.value( u"M_VALUE"_s ).value<QgsProperty>();
111
112 return true;
113}
114
115QgsFeatureList QgsSetMValueAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
116{
117 QgsFeature f = feature;
118
119 if ( f.hasGeometry() )
120 {
121 std::unique_ptr<QgsAbstractGeometry> newGeometry( f.geometry().constGet()->clone() );
122 // addMValue won't alter existing M values, so drop them first
123 if ( QgsWkbTypes::hasM( newGeometry->wkbType() ) )
124 newGeometry->dropMValue();
125
126 double m = mMValue;
127 if ( mDynamicMValue )
128 m = mMValueProperty.valueAsDouble( context.expressionContext(), m );
129
130 newGeometry->addMValue( m );
131
132 f.setGeometry( QgsGeometry( std::move( newGeometry ) ) );
133 }
134
135 return QgsFeatureList() << f;
136}
137
@ 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
QFlags< ProcessingFeatureSourceFlag > ProcessingFeatureSourceFlags
Flags which control how QgsProcessingFeatureSource fetches features.
Definition qgis.h:3793
@ Double
Double/float values.
Definition qgis.h:3875
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:60
QgsGeometry geometry
Definition qgsfeature.h:71
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:83
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:47
@ Double
Double value (including negative values).
Definition qgsproperty.h:57
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 addM(Qgis::WkbType type)
Adds the m dimension to a WKB type and returns the new type.
static Q_INVOKABLE bool hasM(Qgis::WkbType type)
Tests whether a WKB type contains m values.
QList< QgsFeature > QgsFeatureList