QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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 #include "qgsvectorlayer.h"
20 
22 
23 QString QgsSwapXYAlgorithm::name() const
24 {
25  return QStringLiteral( "swapxy" );
26 }
27 
28 QString QgsSwapXYAlgorithm::displayName() const
29 {
30  return QObject::tr( "Swap X and Y coordinates" );
31 }
32 
33 QStringList QgsSwapXYAlgorithm::tags() const
34 {
35  return QObject::tr( "invert,flip,swap,latitude,longitude" ).split( ',' );
36 }
37 
38 QString QgsSwapXYAlgorithm::group() const
39 {
40  return QObject::tr( "Vector geometry" );
41 }
42 
43 QString QgsSwapXYAlgorithm::groupId() const
44 {
45  return QStringLiteral( "vectorgeometry" );
46 }
47 
48 QString QgsSwapXYAlgorithm::outputName() const
49 {
50  return QObject::tr( "Swapped" );
51 }
52 
53 QString QgsSwapXYAlgorithm::shortHelpString() const
54 {
55  return QObject::tr( "This algorithm swaps the X and Y coordinate values in input geometries. It can be used to repair geometries "
56  "which have accidentally had their latitude and longitude values reversed." );
57 }
58 
59 QgsSwapXYAlgorithm *QgsSwapXYAlgorithm::createInstance() const
60 {
61  return new QgsSwapXYAlgorithm();
62 }
63 
64 bool QgsSwapXYAlgorithm::supportInPlaceEdit( const QgsMapLayer *l ) const
65 {
66  const QgsVectorLayer *layer = qobject_cast< const QgsVectorLayer * >( l );
67  if ( !layer )
68  return false;
69 
71  return false;
72 
73  return layer->isSpatial();
74 }
75 
76 QgsProcessingFeatureSource::Flag QgsSwapXYAlgorithm::sourceFlags() const
77 {
78  // this algorithm doesn't care about invalid geometries
80 }
81 
82 QgsFeatureList QgsSwapXYAlgorithm::processFeature( const QgsFeature &f, QgsProcessingContext &, QgsProcessingFeedback * )
83 {
84  QgsFeatureList list;
85  QgsFeature feature = f;
86  if ( feature.hasGeometry() )
87  {
88  QgsGeometry geom = feature.geometry();
89  std::unique_ptr< QgsAbstractGeometry > swappedGeom( geom.constGet()->clone() );
90  swappedGeom->swapXy();
91  feature.setGeometry( QgsGeometry( std::move( swappedGeom ) ) );
92  list << feature;
93  }
94  else
95  {
96  list << feature;
97  }
98  return list;
99 }
100 
Base class for all map layer types.
Definition: qgsmaplayer.h:63
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:571
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:106
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:55
virtual QgsAbstractGeometry * clone() const =0
Clones the geometry by performing a deep copy.
bool isSpatial() const FINAL
Returns true if this is a geometry layer and false in case of NoGeometry (table only) or UnknownGeome...
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
Flag
Flags controlling how QgsProcessingFeatureSource fetches features.
void setGeometry(const QgsGeometry &geometry)
Set the feature&#39;s geometry.
Definition: qgsfeature.cpp:137
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:197
bool supportInPlaceEdit(const QgsMapLayer *layer) const override
Checks whether this algorithm supports in-place editing on the given layer Default implementation for...
QgsGeometry geometry
Definition: qgsfeature.h:67
Represents a vector layer which manages a vector based data sets.
Contains information about the context in which a processing algorithm is executed.