QGIS API Documentation 4.3.0-Master (45633be667c)
Loading...
Searching...
No Matches
qgsalgorithmtexttofloat.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmtexttofloat.cpp
3 ---------------------
4 begin : September 2026
5 copyright : (C) 2026 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 <QString>
21
22using namespace Qt::StringLiterals;
23
25
26QString QgsTextToFloatAlgorithm::name() const
27{
28 return u"texttofloat"_s;
29}
30
31QString QgsTextToFloatAlgorithm::displayName() const
32{
33 return QObject::tr( "Text to float" );
34}
35
36QString QgsTextToFloatAlgorithm::shortHelpString() const
37{
38 return QObject::tr(
39 "This algorithm modifies the type of a given attribute in a vector layer, "
40 "converting a text attribute containing numeric strings into a numeric attribute.\n\n"
41 "Values containing a '%' symbol (e.g., '12.5%') will have the percentage sign removed "
42 "and will be converted to their decimal equivalent (e.g., 0.125).\n"
43 "Any non-numeric or invalid string values that cannot be parsed into a float will be "
44 "replaced with NULL."
45 );
46}
47
48QString QgsTextToFloatAlgorithm::shortDescription() const
49{
50 return QObject::tr( "Converts a text attribute containing numeric strings into a numeric attribute." );
51}
52
53QStringList QgsTextToFloatAlgorithm::tags() const
54{
55 return QObject::tr( "convert,string,number,double,cast,type" ).split( ',' );
56}
57
58QString QgsTextToFloatAlgorithm::group() const
59{
60 return QObject::tr( "Vector table" );
61}
62
63QString QgsTextToFloatAlgorithm::groupId() const
64{
65 return u"vectortable"_s;
66}
67
68QString QgsTextToFloatAlgorithm::outputName() const
69{
70 return QObject::tr( "Float from text" );
71}
72
73QList<int> QgsTextToFloatAlgorithm::inputLayerTypes() const
74{
75 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::Vector );
76}
77
78QgsTextToFloatAlgorithm *QgsTextToFloatAlgorithm::createInstance() const
79{
80 return new QgsTextToFloatAlgorithm();
81}
82
83Qgis::ProcessingFeatureSourceFlags QgsTextToFloatAlgorithm::sourceFlags() const
84{
86}
87
88bool QgsTextToFloatAlgorithm::supportInPlaceEdit( const QgsMapLayer * ) const
89{
90 return false;
91}
92
93void QgsTextToFloatAlgorithm::initParameters( const QVariantMap & )
94{
95 auto fieldParam = std::make_unique<QgsProcessingParameterField>( u"FIELD"_s, QObject::tr( "Text attribute to convert to float" ), QVariant(), u"INPUT"_s, Qgis::ProcessingFieldParameterDataType::String );
96 fieldParam->setHelp( QObject::tr( "Text/string field containing numeric values to convert. Supports standard numeric strings as well as formatted percentage strings." ) );
97 addParameter( fieldParam.release() );
98}
99
100QgsFields QgsTextToFloatAlgorithm::outputFields( const QgsFields &inputFields ) const
101{
102 QList<QgsField> fieldList = inputFields.toList();
103 fieldList[mFieldIndex] = QgsField( mFieldName, QMetaType::Type::Double, u"Double"_s, 24, 15 );
104
105 QgsFields outFields( fieldList );
106 return outFields;
107}
108
109bool QgsTextToFloatAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
110{
111 mFieldName = parameterAsString( parameters, u"FIELD"_s, context );
112 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, u"INPUT"_s, context ) );
113 if ( source )
114 {
115 mFieldIndex = source->fields().lookupField( mFieldName );
116 if ( mFieldIndex < 0 )
117 {
118 throw QgsProcessingException( QObject::tr( "Field “%1” does not exist in input layer " ).arg( mFieldName ) );
119 }
120 }
121
122 return true;
123}
124
125QgsFeatureList QgsTextToFloatAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &, QgsProcessingFeedback * )
126{
127 QGS_MARK_ALGORITHM_SOURCE
128
129 QgsFeature outFeature = feature;
130
131 const QVariant value = outFeature.attribute( mFieldIndex );
132 bool ok = false;
133
134 if ( !QgsVariantUtils::isNull( value ) )
135 {
136 QString stringValue = value.toString().trimmed();
137 if ( stringValue.endsWith( '%' ) )
138 {
139 stringValue.chop( 1 );
140 double d = stringValue.toDouble( &ok );
141 if ( ok )
142 {
143 outFeature.setAttribute( mFieldIndex, d / 100.0 );
144 }
145 }
146 else
147 {
148 double d = stringValue.toDouble( &ok );
149 if ( ok )
150 {
151 outFeature.setAttribute( mFieldIndex, d );
152 }
153 }
154 }
155
156 if ( !ok )
157 {
158 outFeature.setAttribute( mFieldIndex, QVariant() );
159 }
160
161 return QgsFeatureList() << outFeature;
162}
163
@ Vector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
Definition qgis.h:3757
@ String
Accepts string fields.
Definition qgis.h:4062
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Definition qgis.h:3954
QFlags< ProcessingFeatureSourceFlag > ProcessingFeatureSourceFlags
Flags which control how QgsProcessingFeatureSource fetches features.
Definition qgis.h:3965
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:60
Q_INVOKABLE QVariant attribute(const QString &name) const
Lookup attribute value by attribute name.
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:56
Container of fields for a vector layer.
Definition qgsfields.h:45
QList< QgsField > toList() const
Utility function to return a list of QgsField instances.
Base class for all map layer types.
Definition qgsmaplayer.h:83
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.
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.
QList< QgsFeature > QgsFeatureList