QGIS API Documentation  3.2.0-Bonn (bc43194)
qgsalgorithmswapxy.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsalgorithmswapxy.cpp
3  ------------------------
4  begin : April 2018
5  copyright : (C) 2018 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 
18 #include "qgsalgorithmswapxy.h"
19 
21 
22 QString QgsSwapXYAlgorithm::name() const
23 {
24  return QStringLiteral( "swapxy" );
25 }
26 
27 QString QgsSwapXYAlgorithm::displayName() const
28 {
29  return QObject::tr( "Swap X and Y coordinates" );
30 }
31 
32 QStringList QgsSwapXYAlgorithm::tags() const
33 {
34  return QObject::tr( "invert,flip,swap,latitude,longitude" ).split( ',' );
35 }
36 
37 QString QgsSwapXYAlgorithm::group() const
38 {
39  return QObject::tr( "Vector geometry" );
40 }
41 
42 QString QgsSwapXYAlgorithm::groupId() const
43 {
44  return QStringLiteral( "vectorgeometry" );
45 }
46 
47 QString QgsSwapXYAlgorithm::outputName() const
48 {
49  return QObject::tr( "Swapped" );
50 }
51 
52 QString QgsSwapXYAlgorithm::shortHelpString() const
53 {
54  return QObject::tr( "This algorithm swaps the X and Y coordinate values in input geometries. It can be used to repair geometries "
55  "which have accidentally had their latitude and longitude values reversed." );
56 }
57 
58 QgsSwapXYAlgorithm *QgsSwapXYAlgorithm::createInstance() const
59 {
60  return new QgsSwapXYAlgorithm();
61 }
62 
63 QgsProcessingFeatureSource::Flag QgsSwapXYAlgorithm::sourceFlags() const
64 {
65  // this algorithm doesn't care about invalid geometries
67 }
68 
69 QgsFeatureList QgsSwapXYAlgorithm::processFeature( const QgsFeature &f, QgsProcessingContext &, QgsProcessingFeedback * )
70 {
71  QgsFeatureList list;
72  QgsFeature feature = f;
73  if ( feature.hasGeometry() )
74  {
75  QgsGeometry geom = feature.geometry();
76  std::unique_ptr< QgsAbstractGeometry > swappedGeom( geom.constGet()->clone() );
77  swappedGeom->swapXy();
78  feature.setGeometry( QgsGeometry( std::move( swappedGeom ) ) );
79  list << feature;
80  }
81  else
82  {
83  list << feature;
84  }
85  return list;
86 }
87 
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...
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:549
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:104
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:62
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:190
virtual QgsAbstractGeometry * clone() const =0
Clones the geometry by performing a deep copy.
QgsGeometry geometry() const
Returns the geometry associated with this feature.
Definition: qgsfeature.cpp:101
Flag
Flags controlling how QgsProcessingFeatureSource fetches features.
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
void setGeometry(const QgsGeometry &geometry)
Set the feature&#39;s geometry.
Definition: qgsfeature.cpp:137
Contains information about the context in which a processing algorithm is executed.