QGIS API Documentation 4.3.0-Master (45633be667c)
Loading...
Searching...
No Matches
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
20#include "qgscurve.h"
22
23#include <QString>
24
25using namespace Qt::StringLiterals;
26
28
29QString QgsReverseLineDirectionAlgorithm ::name() const
30{
31 return u"reverselinedirection"_s;
32}
33
34QString QgsReverseLineDirectionAlgorithm ::displayName() const
35{
36 return QObject::tr( "Reverse line direction" );
37}
38
39QStringList QgsReverseLineDirectionAlgorithm ::tags() const
40{
41 return QObject::tr( "swap,reverse,switch,flip,linestring,orientation" ).split( ',' );
42}
43
44QString QgsReverseLineDirectionAlgorithm ::group() const
45{
46 return QObject::tr( "Vector geometry" );
47}
48
49QString QgsReverseLineDirectionAlgorithm ::groupId() const
50{
51 return u"vectorgeometry"_s;
52}
53
54QString QgsReverseLineDirectionAlgorithm ::outputName() const
55{
56 return QObject::tr( "Reversed" );
57}
58
59QString QgsReverseLineDirectionAlgorithm ::shortHelpString() const
60{
61 return QObject::tr( "This algorithm reverses the direction of curve or LineString geometries." );
62}
63
64QString QgsReverseLineDirectionAlgorithm::shortDescription() const
65{
66 return QObject::tr( "Reverses the direction of curve or LineString geometries." );
67}
68
69QgsReverseLineDirectionAlgorithm *QgsReverseLineDirectionAlgorithm ::createInstance() const
70{
71 return new QgsReverseLineDirectionAlgorithm();
72}
73
74Qgis::ProcessingSourceType QgsReverseLineDirectionAlgorithm::outputLayerType() const
75{
77}
78
79QList<int> QgsReverseLineDirectionAlgorithm::inputLayerTypes() const
80{
81 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorLine );
82}
83
84Qgis::ProcessingFeatureSourceFlags QgsReverseLineDirectionAlgorithm ::sourceFlags() const
85{
86 // this algorithm doesn't care about invalid geometries
88}
89
90QgsFeatureList QgsReverseLineDirectionAlgorithm ::processFeature( const QgsFeature &f, QgsProcessingContext &, QgsProcessingFeedback * )
91{
92 QGS_MARK_ALGORITHM_SOURCE
93
94 QgsFeature feature = f;
95 if ( feature.hasGeometry() )
96 {
97 const QgsGeometry geom = feature.geometry();
98 if ( !geom.isMultipart() )
99 {
101 if ( curve )
102 {
103 std::unique_ptr<QgsCurve> reversed( curve->reversed() );
104 if ( !reversed )
105 {
106 // can this even happen?
107 throw QgsProcessingException( QObject::tr( "Error reversing line" ) );
108 }
109 const QgsGeometry outGeom( std::move( reversed ) );
110 feature.setGeometry( outGeom );
111 }
112 }
113 else
114 {
115 const QgsGeometryCollection *collection = qgis::down_cast<const QgsGeometryCollection *>( geom.constGet() );
116 std::unique_ptr<QgsGeometryCollection> destCollection( collection->createEmptyWithSameType() );
117 destCollection->reserve( collection->numGeometries() );
118 for ( int i = 0; i < collection->numGeometries(); ++i )
119 {
120 const QgsCurve *curve = qgsgeometry_cast<const QgsCurve *>( collection->geometryN( i ) );
121 if ( curve )
122 {
123 std::unique_ptr<QgsCurve> reversed( curve->reversed() );
124 if ( !reversed )
125 {
126 // can this even happen?
127 throw QgsProcessingException( QObject::tr( "Error reversing line" ) );
128 }
129 destCollection->addGeometry( reversed.release() );
130 }
131 }
132 const QgsGeometry outGeom( std::move( destCollection ) );
133 feature.setGeometry( outGeom );
134 }
135 }
136 return QgsFeatureList() << feature;
137}
138
ProcessingSourceType
Processing data source types.
Definition qgis.h:3749
@ VectorLine
Vector line layers.
Definition qgis.h:3753
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Definition qgis.h:3954
QFlags< ProcessingFeatureSourceFlag > ProcessingFeatureSourceFlags
Flags which control how QgsProcessingFeatureSource fetches features.
Definition qgis.h:3965
Abstract base class for curved geometry type.
Definition qgscurve.h:36
virtual QgsCurve * reversed() const =0
Returns a reversed copy of the curve, where the direction of the curve has been flipped.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:60
QgsGeometryCollection * createEmptyWithSameType() const override
Creates a new geometry with the same class and same WKB type as the original and transfers ownership.
int numGeometries() const
Returns the number of geometries within the collection.
const QgsAbstractGeometry * geometryN(int n) const
Returns a const reference to a geometry from within the collection.
A geometry is the spatial representation of a feature.
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
bool isMultipart() const
Returns true if WKB of the geometry is of WKBMulti* type.
Contains information about the context in which a processing algorithm is executed.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
T qgsgeometry_cast(QgsAbstractGeometry *geom)
QList< QgsFeature > QgsFeatureList