QGIS API Documentation 3.38.0-Grenoble (exported)
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#include "qgscurve.h"
21
23
24QString QgsReverseLineDirectionAlgorithm ::name() const
25{
26 return QStringLiteral( "reverselinedirection" );
27}
28
29QString QgsReverseLineDirectionAlgorithm ::displayName() const
30{
31 return QObject::tr( "Reverse line direction" );
32}
33
34QStringList QgsReverseLineDirectionAlgorithm ::tags() const
35{
36 return QObject::tr( "swap,reverse,switch,flip,linestring,orientation" ).split( ',' );
37}
38
39QString QgsReverseLineDirectionAlgorithm ::group() const
40{
41 return QObject::tr( "Vector geometry" );
42}
43
44QString QgsReverseLineDirectionAlgorithm ::groupId() const
45{
46 return QStringLiteral( "vectorgeometry" );
47}
48
49QString QgsReverseLineDirectionAlgorithm ::outputName() const
50{
51 return QObject::tr( "Reversed" );
52}
53
54QString QgsReverseLineDirectionAlgorithm ::shortHelpString() const
55{
56 return QObject::tr( "This algorithm reverses the direction of curve or LineString geometries." );
57}
58
59QString QgsReverseLineDirectionAlgorithm::shortDescription() const
60{
61 return QObject::tr( "Reverses the direction of curve or LineString geometries." );
62}
63
64QgsReverseLineDirectionAlgorithm *QgsReverseLineDirectionAlgorithm ::createInstance() const
65{
66 return new QgsReverseLineDirectionAlgorithm();
67}
68
69Qgis::ProcessingSourceType QgsReverseLineDirectionAlgorithm::outputLayerType() const
70{
72}
73
74QList<int> QgsReverseLineDirectionAlgorithm::inputLayerTypes() const
75{
76 return QList<int>() << static_cast< int >( Qgis::ProcessingSourceType::VectorLine );
77}
78
79Qgis::ProcessingFeatureSourceFlags QgsReverseLineDirectionAlgorithm ::sourceFlags() const
80{
81 // this algorithm doesn't care about invalid geometries
83}
84
85QgsFeatureList 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 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( dest ) );
127 feature.setGeometry( outGeom );
128 }
129 }
130 return QgsFeatureList() << feature;
131}
132
ProcessingSourceType
Processing data source types.
Definition qgis.h:2995
@ VectorLine
Vector line layers.
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
QFlags< ProcessingFeatureSourceFlag > ProcessingFeatureSourceFlags
Flags which control how QgsProcessingFeatureSource fetches features.
Definition qgis.h:3148
virtual QgsAbstractGeometry * createEmptyWithSameType() const =0
Creates a new geometry with the same class and same WKB type as the original and transfers ownership.
Abstract base class for curved geometry type.
Definition qgscurve.h:35
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.
void reserve(int size)
Attempts to allocate memory for at least size geometries.
virtual bool addGeometry(QgsAbstractGeometry *g)
Adds a geometry and takes ownership. Returns true in case of success.
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.
QList< QgsFeature > QgsFeatureList