QGIS API Documentation 3.99.0-Master (357b655ed83)
Loading...
Searching...
No Matches
qgsalgorithmaddxyfields.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmaddixyfields.cpp
3 -----------------------------------
4 begin : March 2019
5 copyright : (C) 2019 by Nyall Dawson
6 email : nyall dot dawson 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 QgsAddXYFieldsAlgorithm::name() const
29{
30 return u"addxyfields"_s;
31}
32
33QString QgsAddXYFieldsAlgorithm::displayName() const
34{
35 return QObject::tr( "Add X/Y fields to layer" );
36}
37
38QString QgsAddXYFieldsAlgorithm::shortHelpString() const
39{
40 return QObject::tr( "This algorithm adds X and Y (or latitude/longitude) fields to a point layer. The X/Y fields can be calculated in a different CRS to the layer (e.g. creating latitude/longitude fields for a layer in a project CRS)." );
41}
42
43QString QgsAddXYFieldsAlgorithm::shortDescription() const
44{
45 return QObject::tr( "Adds X and Y (or latitude/longitude) fields to a point layer." );
46}
47
48QStringList QgsAddXYFieldsAlgorithm::tags() const
49{
50 return QObject::tr( "add,create,latitude,longitude,columns,attributes" ).split( ',' );
51}
52
53QString QgsAddXYFieldsAlgorithm::group() const
54{
55 return QObject::tr( "Vector table" );
56}
57
58QString QgsAddXYFieldsAlgorithm::groupId() const
59{
60 return u"vectortable"_s;
61}
62
63QString QgsAddXYFieldsAlgorithm::outputName() const
64{
65 return QObject::tr( "Added fields" );
66}
67
68QList<int> QgsAddXYFieldsAlgorithm::inputLayerTypes() const
69{
70 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorPoint );
71}
72
73QgsAddXYFieldsAlgorithm *QgsAddXYFieldsAlgorithm::createInstance() const
74{
75 return new QgsAddXYFieldsAlgorithm();
76}
77
78Qgis::ProcessingFeatureSourceFlags QgsAddXYFieldsAlgorithm::sourceFlags() const
79{
81}
82
83void QgsAddXYFieldsAlgorithm::initParameters( const QVariantMap &configuration )
84{
85 mIsInPlace = configuration.value( u"IN_PLACE"_s ).toBool();
86
87 addParameter( new QgsProcessingParameterCrs( u"CRS"_s, QObject::tr( "Coordinate system" ), u"EPSG:4326"_s ) );
88
89 if ( !mIsInPlace )
90 addParameter( new QgsProcessingParameterString( u"PREFIX"_s, QObject::tr( "Field prefix" ), QVariant(), false, true ) );
91 else
92 {
93 addParameter( new QgsProcessingParameterField( u"FIELD_X"_s, QObject::tr( "X field" ), QVariant(), u"INPUT"_s ) );
94 addParameter( new QgsProcessingParameterField( u"FIELD_Y"_s, QObject::tr( "Y field" ), QVariant(), u"INPUT"_s ) );
95 }
96}
97
98QgsFields QgsAddXYFieldsAlgorithm::outputFields( const QgsFields &inputFields ) const
99{
100 if ( mIsInPlace )
101 {
102 mInPlaceXFieldIndex = inputFields.lookupField( mInPlaceXField );
103 mInPlaceYFieldIndex = inputFields.lookupField( mInPlaceYField );
104 return inputFields;
105 }
106 else
107 {
108 const QString xFieldName = mPrefix + 'x';
109 const QString yFieldName = mPrefix + 'y';
110
111 QgsFields newFields;
112 newFields.append( QgsField( xFieldName, QMetaType::Type::Double, QString(), 20, 10 ) );
113 newFields.append( QgsField( yFieldName, QMetaType::Type::Double, QString(), 20, 10 ) );
114 return QgsProcessingUtils::combineFields( inputFields, newFields );
115 }
116}
117
118QgsCoordinateReferenceSystem QgsAddXYFieldsAlgorithm::outputCrs( const QgsCoordinateReferenceSystem &inputCrs ) const
119{
120 mSourceCrs = inputCrs;
121 return inputCrs;
122}
123
124bool QgsAddXYFieldsAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
125{
126 if ( !mIsInPlace )
127 mPrefix = parameterAsString( parameters, u"PREFIX"_s, context );
128 else
129 {
130 mInPlaceXField = parameterAsString( parameters, u"FIELD_X"_s, context );
131 mInPlaceYField = parameterAsString( parameters, u"FIELD_Y"_s, context );
132 }
133
134 mCrs = parameterAsCrs( parameters, u"CRS"_s, context );
135 return true;
136}
137
138QgsFeatureList QgsAddXYFieldsAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
139{
140 if ( mTransformNeedsInitialization )
141 {
142 mTransform = QgsCoordinateTransform( mSourceCrs, mCrs, context.transformContext() );
143 mTransformNeedsInitialization = false;
144 }
145 if ( mIsInPlace && mInPlaceXFieldIndex == -1 )
146 {
147 throw QgsProcessingException( QObject::tr( "Destination field not found" ) );
148 }
149
150 QVariant x;
151 QVariant y;
152 if ( feature.hasGeometry() )
153 {
154 if ( feature.geometry().isMultipart() )
155 throw QgsProcessingException( QObject::tr( "Multipoint features are not supported - please convert to single point features first." ) );
156
157 const QgsPointXY point = feature.geometry().asPoint();
158 try
159 {
160 const QgsPointXY transformed = mTransform.transform( point );
161 x = transformed.x();
162 y = transformed.y();
163 }
164 catch ( QgsCsException & )
165 {
166 feedback->reportError( QObject::tr( "Could not transform point to destination CRS" ) );
167 }
168 }
169 QgsFeature f = feature;
170 QgsAttributes attributes = f.attributes();
171 if ( !mIsInPlace )
172 {
173 attributes << x << y;
174 }
175 else
176 {
177 attributes[mInPlaceXFieldIndex] = std::move( x );
178 attributes[mInPlaceYFieldIndex] = std::move( y );
179 }
180 f.setAttributes( attributes );
181 return QgsFeatureList() << f;
182}
183
184bool QgsAddXYFieldsAlgorithm::supportInPlaceEdit( const QgsMapLayer *layer ) const
185{
186 if ( const QgsVectorLayer *vl = qobject_cast<const QgsVectorLayer *>( layer ) )
187 {
188 return vl->geometryType() == Qgis::GeometryType::Point;
189 }
190 return false;
191}
192
@ VectorPoint
Vector point layers.
Definition qgis.h:3605
@ Point
Points.
Definition qgis.h:366
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Definition qgis.h:3782
QFlags< ProcessingFeatureSourceFlag > ProcessingFeatureSourceFlags
Flags which control how QgsProcessingFeatureSource fetches features.
Definition qgis.h:3793
A vector of attributes.
Represents a coordinate reference system (CRS).
Handles coordinate transforms between two coordinate systems.
Custom exception class for Coordinate Reference System related exceptions.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:60
QgsAttributes attributes
Definition qgsfeature.h:69
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
QgsGeometry geometry
Definition qgsfeature.h:71
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:56
Container of fields for a vector layer.
Definition qgsfields.h:46
bool append(const QgsField &field, Qgis::FieldOrigin origin=Qgis::FieldOrigin::Provider, int originIndex=-1)
Appends a field.
Definition qgsfields.cpp:76
Q_INVOKABLE int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
QgsPointXY asPoint() const
Returns the contents of the geometry as a 2-dimensional point.
bool isMultipart() const
Returns true if WKB of the geometry is of WKBMulti* type.
Base class for all map layer types.
Definition qgsmaplayer.h:83
Represents a 2D point.
Definition qgspointxy.h:62
double y
Definition qgspointxy.h:66
double x
Definition qgspointxy.h:65
Contains information about the context in which a processing algorithm is executed.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context.
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 coordinate reference system parameter for processing algorithms.
A vector layer or feature source field parameter for processing algorithms.
A string parameter for processing algorithms.
static QgsFields combineFields(const QgsFields &fieldsA, const QgsFields &fieldsB, const QString &fieldsBPrefix=QString())
Combines two field lists, avoiding duplicate field names (in a case-insensitive manner).
Represents a vector layer which manages a vector based dataset.
QList< QgsFeature > QgsFeatureList