QGIS API Documentation  3.4.15-Madeira (e83d02e274)
qgsalgorithmreverselinedirection.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsalgorithmreverselinedirection.cpp
3  ------------------------------------
4  begin : July 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 
19 #include "qgscurve.h"
20 #include "qgsgeometrycollection.h"
21 
23 
24 QString QgsReverseLineDirectionAlgorithm ::name() const
25 {
26  return QStringLiteral( "reverselinedirection" );
27 }
28 
29 QString QgsReverseLineDirectionAlgorithm ::displayName() const
30 {
31  return QObject::tr( "Reverse line direction" );
32 }
33 
34 QStringList QgsReverseLineDirectionAlgorithm ::tags() const
35 {
36  return QObject::tr( "swap,reverse,switch,flip,linestring,orientation" ).split( ',' );
37 }
38 
39 QString QgsReverseLineDirectionAlgorithm ::group() const
40 {
41  return QObject::tr( "Vector geometry" );
42 }
43 
44 QString QgsReverseLineDirectionAlgorithm ::groupId() const
45 {
46  return QStringLiteral( "vectorgeometry" );
47 }
48 
49 QString QgsReverseLineDirectionAlgorithm ::outputName() const
50 {
51  return QObject::tr( "Reversed" );
52 }
53 
54 QString QgsReverseLineDirectionAlgorithm ::shortHelpString() const
55 {
56  return QObject::tr( "This algorithm reverses the direction of curve or LineString geometries." );
57 }
58 
59 QString QgsReverseLineDirectionAlgorithm::shortDescription() const
60 {
61  return QObject::tr( "Reverses the direction of curve or LineString geometries." );
62 }
63 
64 QgsReverseLineDirectionAlgorithm *QgsReverseLineDirectionAlgorithm ::createInstance() const
65 {
66  return new QgsReverseLineDirectionAlgorithm();
67 }
68 
69 QgsProcessing::SourceType QgsReverseLineDirectionAlgorithm::outputLayerType() const
70 {
72 }
73 
74 QList<int> QgsReverseLineDirectionAlgorithm::inputLayerTypes() const
75 {
76  return QList<int>() << QgsProcessing::TypeVectorLine;
77 }
78 
79 QgsProcessingFeatureSource::Flag QgsReverseLineDirectionAlgorithm ::sourceFlags() const
80 {
81  // this algorithm doesn't care about invalid geometries
83 }
84 
85 QgsFeatureList QgsReverseLineDirectionAlgorithm ::processFeature( const QgsFeature &f, QgsProcessingContext &, QgsProcessingFeedback * )
86 {
87  QgsFeature feature = f;
88  if ( feature.hasGeometry() )
89  {
90  const QgsGeometry geom = feature.geometry();
91  if ( !geom.isMultipart() )
92  {
93  const QgsCurve *curve = qgsgeometry_cast< const QgsCurve * >( geom.constGet() );
94  if ( curve )
95  {
96  std::unique_ptr< QgsCurve > reversed( curve->reversed() );
97  if ( !reversed )
98  {
99  // can this even happen?
100  throw QgsProcessingException( QObject::tr( "Error reversing line" ) );
101  }
102  const QgsGeometry outGeom( std::move( reversed ) );
103  feature.setGeometry( outGeom );
104  }
105  }
106  else
107  {
108  std::unique_ptr< QgsAbstractGeometry > dest( geom.constGet()->createEmptyWithSameType() );
109  const QgsGeometryCollection *collection = qgsgeometry_cast< const QgsGeometryCollection * >( geom.constGet() );
110  QgsGeometryCollection *destCollection = qgsgeometry_cast< QgsGeometryCollection * >( dest.get() );
111  for ( int i = 0; i < collection->numGeometries(); ++i )
112  {
113  const QgsCurve *curve = qgsgeometry_cast< const QgsCurve *>( collection->geometryN( i ) );
114  if ( curve )
115  {
116  std::unique_ptr< QgsCurve > reversed( curve->reversed() );
117  if ( !reversed )
118  {
119  // can this even happen?
120  throw QgsProcessingException( QObject::tr( "Error reversing line" ) );
121  }
122  destCollection->addGeometry( reversed.release() );
123  }
124  }
125  const QgsGeometry outGeom( std::move( dest ) );
126  feature.setGeometry( outGeom );
127  }
128  }
129  return QgsFeatureList() << feature;
130 }
131 
virtual QgsCurve * reversed() const =0
Returns a reversed copy of the curve, where the direction of the curve has been flipped.
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
virtual QgsAbstractGeometry * createEmptyWithSameType() const =0
Creates a new geometry with the same class and same WKB type as the original and transfers ownership...
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:55
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
Geometry collection.
T qgsgeometry_cast(const QgsAbstractGeometry *geom)
Custom exception class for processing related exceptions.
Definition: qgsexception.h:82
Abstract base class for curved geometry type.
Definition: qgscurve.h:35
Flag
Flags controlling how QgsProcessingFeatureSource fetches features.
bool isMultipart() const
Returns true if WKB of the geometry is of WKBMulti* type.
Vector line layers.
Definition: qgsprocessing.h:49
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
SourceType
Data source types enum.
Definition: qgsprocessing.h:44
QgsGeometry geometry
Definition: qgsfeature.h:67
Contains information about the context in which a processing algorithm is executed.