QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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
24
25QString QgsReverseLineDirectionAlgorithm ::name() const
26{
27 return QStringLiteral( "reverselinedirection" );
28}
29
30QString QgsReverseLineDirectionAlgorithm ::displayName() const
31{
32 return QObject::tr( "Reverse line direction" );
33}
34
35QStringList QgsReverseLineDirectionAlgorithm ::tags() const
36{
37 return QObject::tr( "swap,reverse,switch,flip,linestring,orientation" ).split( ',' );
38}
39
40QString QgsReverseLineDirectionAlgorithm ::group() const
41{
42 return QObject::tr( "Vector geometry" );
43}
44
45QString QgsReverseLineDirectionAlgorithm ::groupId() const
46{
47 return QStringLiteral( "vectorgeometry" );
48}
49
50QString QgsReverseLineDirectionAlgorithm ::outputName() const
51{
52 return QObject::tr( "Reversed" );
53}
54
55QString QgsReverseLineDirectionAlgorithm ::shortHelpString() const
56{
57 return QObject::tr( "This algorithm reverses the direction of curve or LineString geometries." );
58}
59
60QString QgsReverseLineDirectionAlgorithm::shortDescription() const
61{
62 return QObject::tr( "Reverses the direction of curve or LineString geometries." );
63}
64
65QgsReverseLineDirectionAlgorithm *QgsReverseLineDirectionAlgorithm ::createInstance() const
66{
67 return new QgsReverseLineDirectionAlgorithm();
68}
69
70Qgis::ProcessingSourceType QgsReverseLineDirectionAlgorithm::outputLayerType() const
71{
73}
74
75QList<int> QgsReverseLineDirectionAlgorithm::inputLayerTypes() const
76{
77 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorLine );
78}
79
80Qgis::ProcessingFeatureSourceFlags QgsReverseLineDirectionAlgorithm ::sourceFlags() const
81{
82 // this algorithm doesn't care about invalid geometries
84}
85
86QgsFeatureList QgsReverseLineDirectionAlgorithm ::processFeature( const QgsFeature &f, QgsProcessingContext &, QgsProcessingFeedback * )
87{
88 QgsFeature feature = f;
89 if ( feature.hasGeometry() )
90 {
91 const QgsGeometry geom = feature.geometry();
92 if ( !geom.isMultipart() )
93 {
95 if ( curve )
96 {
97 std::unique_ptr<QgsCurve> reversed( curve->reversed() );
98 if ( !reversed )
99 {
100 // can this even happen?
101 throw QgsProcessingException( QObject::tr( "Error reversing line" ) );
102 }
103 const QgsGeometry outGeom( std::move( reversed ) );
104 feature.setGeometry( outGeom );
105 }
106 }
107 else
108 {
109 const QgsGeometryCollection *collection = qgis::down_cast<const QgsGeometryCollection *>( geom.constGet() );
110 std::unique_ptr<QgsGeometryCollection> destCollection( collection->createEmptyWithSameType() );
111 destCollection->reserve( collection->numGeometries() );
112 for ( int i = 0; i < collection->numGeometries(); ++i )
113 {
114 const QgsCurve *curve = qgsgeometry_cast<const QgsCurve *>( collection->geometryN( i ) );
115 if ( curve )
116 {
117 std::unique_ptr<QgsCurve> reversed( curve->reversed() );
118 if ( !reversed )
119 {
120 // can this even happen?
121 throw QgsProcessingException( QObject::tr( "Error reversing line" ) );
122 }
123 destCollection->addGeometry( reversed.release() );
124 }
125 }
126 const QgsGeometry outGeom( std::move( destCollection ) );
127 feature.setGeometry( outGeom );
128 }
129 }
130 return QgsFeatureList() << feature;
131}
132
ProcessingSourceType
Processing data source types.
Definition qgis.h:3531
@ VectorLine
Vector line layers.
Definition qgis.h:3535
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Definition qgis.h:3711
QFlags< ProcessingFeatureSourceFlag > ProcessingFeatureSourceFlags
Flags which control how QgsProcessingFeatureSource fetches features.
Definition qgis.h:3722
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:58
QgsGeometry geometry
Definition qgsfeature.h:69
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