QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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 #include "qgsfeaturerequest.h"
20 
22 
23 QString QgsAddXYFieldsAlgorithm::name() const
24 {
25  return QStringLiteral( "addxyfields" );
26 }
27 
28 QString QgsAddXYFieldsAlgorithm::displayName() const
29 {
30  return QObject::tr( "Add X/Y fields to layer" );
31 }
32 
33 QString QgsAddXYFieldsAlgorithm::shortHelpString() const
34 {
35  return QObject::tr( "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)." );
36 }
37 
38 QString QgsAddXYFieldsAlgorithm::shortDescription() const
39 {
40  return QObject::tr( "Adds X and Y (or latitude/longitude) fields to a point layer." );
41 }
42 
43 QStringList QgsAddXYFieldsAlgorithm::tags() const
44 {
45  return QObject::tr( "add,create,latitude,longitude,columns,attributes" ).split( ',' );
46 }
47 
48 QString QgsAddXYFieldsAlgorithm::group() const
49 {
50  return QObject::tr( "Vector table" );
51 }
52 
53 QString QgsAddXYFieldsAlgorithm::groupId() const
54 {
55  return QStringLiteral( "vectortable" );
56 }
57 
58 QString QgsAddXYFieldsAlgorithm::outputName() const
59 {
60  return QObject::tr( "Added fields" );
61 }
62 
63 QList<int> QgsAddXYFieldsAlgorithm::inputLayerTypes() const
64 {
65  return QList<int>() << QgsProcessing::TypeVectorPoint;
66 }
67 
68 QgsAddXYFieldsAlgorithm *QgsAddXYFieldsAlgorithm::createInstance() const
69 {
70  return new QgsAddXYFieldsAlgorithm();
71 }
72 
73 QgsProcessingFeatureSource::Flag QgsAddXYFieldsAlgorithm::sourceFlags() const
74 {
76 }
77 
78 void QgsAddXYFieldsAlgorithm::initParameters( const QVariantMap & )
79 {
80  addParameter( new QgsProcessingParameterCrs( QStringLiteral( "CRS" ), QObject::tr( "Coordinate system" ), QStringLiteral( "EPSG:4326" ) ) );
81  addParameter( new QgsProcessingParameterString( QStringLiteral( "PREFIX" ), QObject::tr( "Field prefix" ), QVariant(), false, true ) );
82 }
83 
84 QgsFields QgsAddXYFieldsAlgorithm::outputFields( const QgsFields &inputFields ) const
85 {
86  const QString xFieldName = mPrefix + 'x';
87  const QString yFieldName = mPrefix + 'y';
88 
89  QgsFields outFields = inputFields;
90  outFields.append( QgsField( xFieldName, QVariant::Double, QString(), 20, 10 ) );
91  outFields.append( QgsField( yFieldName, QVariant::Double, QString(), 20, 10 ) );
92  return outFields;
93 }
94 
96 {
97  mSourceCrs = inputCrs;
98  return inputCrs;
99 }
100 
101 bool QgsAddXYFieldsAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
102 {
103  mPrefix = parameterAsString( parameters, QStringLiteral( "PREFIX" ), context );
104  mCrs = parameterAsCrs( parameters, QStringLiteral( "CRS" ), context );
105  return true;
106 }
107 
108 QgsFeatureList QgsAddXYFieldsAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
109 {
110  if ( mTransformNeedsInitialization )
111  {
112  mTransform = QgsCoordinateTransform( mSourceCrs, mCrs, context.transformContext() );
113  mTransformNeedsInitialization = false;
114  }
115 
116  QVariant x;
117  QVariant y;
118  if ( feature.hasGeometry() )
119  {
120  if ( feature.geometry().isMultipart() )
121  throw QgsProcessingException( QObject::tr( "Multipoint features are not supported - please convert to single point features first." ) );
122 
123  const QgsPointXY point = feature.geometry().asPoint();
124  try
125  {
126  const QgsPointXY transformed = mTransform.transform( point );
127  x = transformed.x();
128  y = transformed.y();
129  }
130  catch ( QgsCsException & )
131  {
132  feedback->reportError( QObject::tr( "Could not transform point to destination CRS" ) );
133  }
134  }
135  QgsFeature f = feature;
136  QgsAttributes attributes = f.attributes();
137  attributes << x << y;
138  f.setAttributes( attributes );
139  return QgsFeatureList() << f;
140 }
141 
142 bool QgsAddXYFieldsAlgorithm::supportInPlaceEdit( const QgsMapLayer *layer ) const
143 {
144  Q_UNUSED( layer )
145  return false;
146 }
147 
Base class for all map layer types.
Definition: qgsmaplayer.h:79
Base class for providing feedback from a processing algorithm.
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
bool isMultipart() const
Returns true if WKB of the geometry is of WKBMulti* type.
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:571
double y
Definition: qgspointxy.h:48
A class to represent a 2D point.
Definition: qgspointxy.h:43
Container of fields for a vector layer.
Definition: qgsfields.h:42
void setAttributes(const QgsAttributes &attrs)
Sets the feature&#39;s attributes.
Definition: qgsfeature.cpp:127
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:55
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:197
Custom exception class for processing related exceptions.
Definition: qgsexception.h:82
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context.
bool append(const QgsField &field, FieldOrigin origin=OriginProvider, int originIndex=-1)
Appends a field. The field must have unique name, otherwise it is rejected (returns false) ...
Definition: qgsfields.cpp:59
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:48
A coordinate reference system parameter for processing algorithms.
double x
Definition: qgspointxy.h:47
Flag
Flags controlling how QgsProcessingFeatureSource fetches features.
QgsPointXY asPoint() const
Returns the contents of the geometry as a 2-dimensional point.
Vector point layers.
Definition: qgsprocessing.h:48
This class represents a coordinate reference system (CRS).
Class for doing transforms between two map coordinate systems.
const QgsCoordinateReferenceSystem & outputCrs
virtual void reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.
QgsGeometry geometry
Definition: qgsfeature.h:67
Custom exception class for Coordinate Reference System related exceptions.
Definition: qgsexception.h:65
A vector of attributes.
Definition: qgsattributes.h:57
Contains information about the context in which a processing algorithm is executed.
A string parameter for processing algorithms.
QgsAttributes attributes
Definition: qgsfeature.h:65