QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 
QgsPointXY::y
double y
Definition: qgspointxy.h:48
outputCrs
const QgsCoordinateReferenceSystem & outputCrs
Definition: qgswfsgetfeature.cpp:115
qgsfeaturerequest.h
qgsalgorithmaddxyfields.h
QgsProcessingFeedback
Definition: qgsprocessingfeedback.h:37
QgsGeometry::isMultipart
bool isMultipart() const
Returns true if WKB of the geometry is of WKBMulti* type.
Definition: qgsgeometry.cpp:377
QgsProcessingFeedback::reportError
virtual void reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.
Definition: qgsprocessingfeedback.cpp:39
QgsFields
Definition: qgsfields.h:44
QgsFeature::geometry
QgsGeometry geometry
Definition: qgsfeature.h:71
QgsProcessingFeatureSource::FlagSkipGeometryValidityChecks
@ FlagSkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Definition: qgsprocessingutils.h:475
QgsFields::append
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
QgsProcessing::TypeVectorPoint
@ TypeVectorPoint
Vector point layers.
Definition: qgsprocessing.h:48
QgsCsException
Definition: qgsexception.h:65
QgsProcessingParameterCrs
Definition: qgsprocessingparameters.h:1470
QgsProcessingContext
Definition: qgsprocessingcontext.h:43
QgsProcessingParameterString
Definition: qgsprocessingparameters.h:2202
QgsFeatureList
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:572
QgsProcessingContext::transformContext
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context.
Definition: qgsprocessingcontext.h:135
QgsFeature::attributes
QgsAttributes attributes
Definition: qgsfeature.h:69
QgsCoordinateReferenceSystem
Definition: qgscoordinatereferencesystem.h:206
QgsPointXY
Definition: qgspointxy.h:43
QgsGeometry::asPoint
QgsPointXY asPoint() const
Returns the contents of the geometry as a 2-dimensional point.
Definition: qgsgeometry.cpp:1559
QgsProcessingFeatureSource::Flag
Flag
Flags controlling how QgsProcessingFeatureSource fetches features.
Definition: qgsprocessingutils.h:473
QgsFeature::hasGeometry
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:197
QgsMapLayer
Definition: qgsmaplayer.h:81
QgsPointXY::x
double x
Definition: qgspointxy.h:47
QgsAttributes
Definition: qgsattributes.h:57
QgsFeature
Definition: qgsfeature.h:55
QgsFeature::setAttributes
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
Definition: qgsfeature.cpp:127
QgsCoordinateTransform
Definition: qgscoordinatetransform.h:52
QgsProcessingException
Definition: qgsexception.h:82
QgsField
Definition: qgsfield.h:49