QGIS API Documentation 3.99.0-Master (357b655ed83)
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 QgsFeature feature = f;
93 if ( feature.hasGeometry() )
94 {
95 const QgsGeometry geom = feature.geometry();
96 if ( !geom.isMultipart() )
97 {
99 if ( curve )
100 {
101 std::unique_ptr<QgsCurve> reversed( curve->reversed() );
102 if ( !reversed )
103 {
104 // can this even happen?
105 throw QgsProcessingException( QObject::tr( "Error reversing line" ) );
106 }
107 const QgsGeometry outGeom( std::move( reversed ) );
108 feature.setGeometry( outGeom );
109 }
110 }
111 else
112 {
113 const QgsGeometryCollection *collection = qgis::down_cast<const QgsGeometryCollection *>( geom.constGet() );
114 std::unique_ptr<QgsGeometryCollection> destCollection( collection->createEmptyWithSameType() );
115 destCollection->reserve( collection->numGeometries() );
116 for ( int i = 0; i < collection->numGeometries(); ++i )
117 {
118 const QgsCurve *curve = qgsgeometry_cast<const QgsCurve *>( collection->geometryN( i ) );
119 if ( curve )
120 {
121 std::unique_ptr<QgsCurve> reversed( curve->reversed() );
122 if ( !reversed )
123 {
124 // can this even happen?
125 throw QgsProcessingException( QObject::tr( "Error reversing line" ) );
126 }
127 destCollection->addGeometry( reversed.release() );
128 }
129 }
130 const QgsGeometry outGeom( std::move( destCollection ) );
131 feature.setGeometry( outGeom );
132 }
133 }
134 return QgsFeatureList() << feature;
135}
136
ProcessingSourceType
Processing data source types.
Definition qgis.h:3602
@ VectorLine
Vector line layers.
Definition qgis.h:3606
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Definition qgis.h:3782
QFlags< ProcessingFeatureSourceFlag > ProcessingFeatureSourceFlags
Flags which control how QgsProcessingFeatureSource fetches features.
Definition qgis.h:3793
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
QgsGeometry geometry
Definition qgsfeature.h:71
bool hasGeometry() const
Returns true if the feature has an associated geometry.
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
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