QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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
Definition: qgsprocessingfeedback.h:37
QgsGeometryCollection::numGeometries
int numGeometries() const
Returns the number of geometries within the collection.
Definition: qgsgeometrycollection.h:51
QgsGeometryCollection::reserve
void reserve(int size)
Attempts to allocate memory for at least size geometries.
Definition: qgsgeometrycollection.cpp:202
QgsCurve::reversed
virtual QgsCurve * reversed() const =0
Returns a reversed copy of the curve, where the direction of the curve has been flipped.
QgsGeometry::isMultipart
bool isMultipart() const
Returns true if WKB of the geometry is of WKBMulti* type.
Definition: qgsgeometry.cpp:377
QgsFeature::geometry
QgsGeometry geometry
Definition: qgsfeature.h:71
QgsProcessing::TypeVectorLine
@ TypeVectorLine
Vector line layers.
Definition: qgsprocessing.h:49
QgsProcessingFeatureSource::FlagSkipGeometryValidityChecks
@ FlagSkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Definition: qgsprocessingutils.h:475
QgsGeometryCollection
Geometry collection.
Definition: qgsgeometrycollection.h:35
QgsFeature::setGeometry
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
Definition: qgsfeature.cpp:137
QgsProcessingContext
Definition: qgsprocessingcontext.h:43
QgsGeometryCollection::addGeometry
virtual bool addGeometry(QgsAbstractGeometry *g)
Adds a geometry and takes ownership. Returns true in case of success.
Definition: qgsgeometrycollection.cpp:226
QgsFeatureList
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:572
QgsGeometry::constGet
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
Definition: qgsgeometry.cpp:128
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:473
QgsGeometryCollection::geometryN
const QgsAbstractGeometry * geometryN(int n) const
Returns a const reference to a geometry from within the collection.
Definition: qgsgeometrycollection.h:79
qgscurve.h
QgsGeometry
Definition: qgsgeometry.h:122
QgsFeature::hasGeometry
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:197
qgsgeometrycollection.h
QgsFeature
Definition: qgsfeature.h:55
QgsProcessing::SourceType
SourceType
Data source types enum.
Definition: qgsprocessing.h:44
QgsProcessingException
Definition: qgsexception.h:82