QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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  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 
QgsCurve
Abstract base class for curved geometry type.
Definition: qgscurve.h:35
qgsalgorithmreverselinedirection.h
QgsProcessingFeedback
Base class for providing feedback from a processing algorithm.
Definition: qgsprocessingfeedback.h:37
QgsCurve::reversed
virtual QgsCurve * reversed() const =0
Returns a reversed copy of the curve, where the direction of the curve has been flipped.
QgsFeature::geometry
QgsGeometry geometry
Definition: qgsfeature.h:71
QgsProcessing::TypeVectorLine
@ TypeVectorLine
Vector line layers.
Definition: qgsprocessing.h:50
QgsProcessingFeatureSource::FlagSkipGeometryValidityChecks
@ FlagSkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Definition: qgsprocessingutils.h:584
QgsGeometryCollection::numGeometries
int numGeometries() const SIP_HOLDGIL
Returns the number of geometries within the collection.
Definition: qgsgeometrycollection.h:58
QgsGeometry::isMultipart
bool isMultipart() const SIP_HOLDGIL
Returns true if WKB of the geometry is of WKBMulti* type.
Definition: qgsgeometry.cpp:389
QgsGeometryCollection
Geometry collection.
Definition: qgsgeometrycollection.h:36
QgsFeature::setGeometry
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
Definition: qgsfeature.cpp:170
QgsProcessingContext
Contains information about the context in which a processing algorithm is executed.
Definition: qgsprocessingcontext.h:46
QgsGeometryCollection::addGeometry
virtual bool addGeometry(QgsAbstractGeometry *g)
Adds a geometry and takes ownership. Returns true in case of success.
Definition: qgsgeometrycollection.cpp:256
QgsFeatureList
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:882
QgsGeometry::constGet
const QgsAbstractGeometry * constGet() const SIP_HOLDGIL
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
Definition: qgsgeometry.cpp:136
QgsAbstractGeometry::createEmptyWithSameType
virtual QgsAbstractGeometry * createEmptyWithSameType() const =0
Creates a new geometry with the same class and same WKB type as the original and transfers ownership.
QgsProcessingFeatureSource::Flag
Flag
Flags controlling how QgsProcessingFeatureSource fetches features.
Definition: qgsprocessingutils.h:582
QgsGeometryCollection::geometryN
const QgsAbstractGeometry * geometryN(int n) const
Returns a const reference to a geometry from within the collection.
Definition: qgsgeometrycollection.h:86
qgscurve.h
QgsGeometry
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
QgsFeature::hasGeometry
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:230
qgsgeometrycollection.h
QgsFeature
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:55
QgsGeometryCollection::reserve
void reserve(int size) SIP_HOLDGIL
Attempts to allocate memory for at least size geometries.
Definition: qgsgeometrycollection.cpp:232
QgsProcessing::SourceType
SourceType
Data source types enum.
Definition: qgsprocessing.h:45
QgsProcessingException
Custom exception class for processing related exceptions.
Definition: qgsexception.h:82