QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsalgorithmpolygonstolines.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsalgorithmpolygonstolines.cpp
3  ---------------------
4  begin : January 2019
5  copyright : (C) 2019 by Matthias Kuhn
6  email : [email protected]
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 "qgsgeometrycollection.h"
20 #include "qgscurvepolygon.h"
21 #include "qgscurve.h"
22 #include "qgsmultilinestring.h"
23 
25 
26 QString QgsPolygonsToLinesAlgorithm::name() const
27 {
28  return QStringLiteral( "polygonstolines" );
29 }
30 
31 QString QgsPolygonsToLinesAlgorithm::displayName() const
32 {
33  return QObject::tr( "Polygons to lines" );
34 }
35 
36 QStringList QgsPolygonsToLinesAlgorithm::tags() const
37 {
38  return QObject::tr( "line,polygon,convert" ).split( ',' );
39 }
40 
41 QString QgsPolygonsToLinesAlgorithm::group() const
42 {
43  return QObject::tr( "Vector geometry" );
44 }
45 
46 QString QgsPolygonsToLinesAlgorithm::groupId() const
47 {
48  return QStringLiteral( "vectorgeometry" );
49 }
50 
51 QString QgsPolygonsToLinesAlgorithm::outputName() const
52 {
53  return QObject::tr( "Lines" );
54 }
55 
56 QgsProcessing::SourceType QgsPolygonsToLinesAlgorithm::outputLayerType() const
57 {
59 }
60 
61 QgsWkbTypes::Type QgsPolygonsToLinesAlgorithm::outputWkbType( QgsWkbTypes::Type inputWkbType ) const
62 {
64 
68  wkbType = QgsWkbTypes::MultiCurve;
69 
70  if ( QgsWkbTypes::hasM( inputWkbType ) )
71  wkbType = QgsWkbTypes::addM( wkbType );
72  if ( QgsWkbTypes::hasZ( inputWkbType ) )
73  wkbType = QgsWkbTypes::addZ( wkbType );
74 
75  return wkbType;
76 }
77 
78 QString QgsPolygonsToLinesAlgorithm::shortHelpString() const
79 {
80  return QObject::tr( "Converts polygons to lines" );
81 }
82 
83 QString QgsPolygonsToLinesAlgorithm::shortDescription() const
84 {
85  return QObject::tr( "Converts polygons to lines." );
86 }
87 
88 QgsPolygonsToLinesAlgorithm *QgsPolygonsToLinesAlgorithm::createInstance() const
89 {
90  return new QgsPolygonsToLinesAlgorithm();
91 }
92 
93 QList<int> QgsPolygonsToLinesAlgorithm::inputLayerTypes() const
94 {
95  return QList<int>() << QgsProcessing::TypeVectorPolygon;
96 }
97 
98 QgsFeatureList QgsPolygonsToLinesAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
99 {
100  Q_UNUSED( context )
101 
102  QgsFeatureList result;
103  QgsFeature feat = feature;
104  if ( feat.hasGeometry() )
105  feat.setGeometry( convertToLines( feat.geometry() ) );
106 
107  result << feat;
108  return result;
109 }
110 
111 QgsGeometry QgsPolygonsToLinesAlgorithm::convertToLines( const QgsGeometry &geometry ) const
112 {
113  auto rings = extractRings( geometry.constGet() );
114 
115  QgsWkbTypes::Type resultType = outputWkbType( geometry.wkbType() );
116 
117  std::unique_ptr<QgsMultiCurve> lineGeometry;
118 
120  lineGeometry = std::make_unique<QgsMultiLineString>();
121  else
122  lineGeometry = std::make_unique<QgsMultiCurve>();
123 
124  lineGeometry->reserve( rings.size() );
125  for ( auto ring : std::as_const( rings ) )
126  lineGeometry->addGeometry( ring );
127 
128  return QgsGeometry( lineGeometry.release() );
129 }
130 
131 QList<QgsCurve *> QgsPolygonsToLinesAlgorithm::extractRings( const QgsAbstractGeometry *geom ) const
132 {
133  QList<QgsCurve *> rings;
134 
135  if ( QgsGeometryCollection *collection = qgsgeometry_cast<QgsGeometryCollection *>( geom ) )
136  {
137  QgsGeometryPartIterator parts = collection->parts();
138  while ( parts.hasNext() )
139  rings.append( extractRings( parts.next() ) );
140  }
141  else if ( QgsCurvePolygon *polygon = qgsgeometry_cast<QgsCurvePolygon *>( geom ) )
142  {
143  if ( auto exteriorRing = polygon->exteriorRing() )
144  rings.append( exteriorRing->clone() );
145  for ( int i = 0; i < polygon->numInteriorRings(); ++i )
146  {
147  rings.append( polygon->interiorRing( i )->clone() );
148  }
149  }
150 
151  return rings;
152 }
153 
154 
155 
157 
158 
QgsProcessingFeedback
Base class for providing feedback from a processing algorithm.
Definition: qgsprocessingfeedback.h:37
QgsWkbTypes::flatType
static Type flatType(Type type) SIP_HOLDGIL
Returns the flat type for a WKB type.
Definition: qgswkbtypes.h:732
QgsCurvePolygon
Curve polygon geometry type.
Definition: qgscurvepolygon.h:34
QgsProcessing::TypeVectorPolygon
@ TypeVectorPolygon
Vector polygon layers.
Definition: qgsprocessing.h:51
QgsWkbTypes::addZ
static Type addZ(Type type) SIP_HOLDGIL
Adds the z dimension to a WKB type and returns the new type.
Definition: qgswkbtypes.h:1176
QgsFeature::geometry
QgsGeometry geometry
Definition: qgsfeature.h:71
QgsGeometryPartIterator::hasNext
bool hasNext() const SIP_HOLDGIL
Find out whether there are more parts.
Definition: qgsabstractgeometry.h:1222
QgsProcessing::TypeVectorLine
@ TypeVectorLine
Vector line layers.
Definition: qgsprocessing.h:50
QgsWkbTypes::Type
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:69
QgsWkbTypes::MultiCurve
@ MultiCurve
Definition: qgswkbtypes.h:83
QgsWkbTypes::addM
static Type addM(Type type) SIP_HOLDGIL
Adds the m dimension to a WKB type and returns the new type.
Definition: qgswkbtypes.h:1201
QgsWkbTypes::MultiLineString
@ MultiLineString
Definition: qgswkbtypes.h:77
QgsGeometryCollection
Geometry collection.
Definition: qgsgeometrycollection.h:36
QgsWkbTypes::Unknown
@ Unknown
Definition: qgswkbtypes.h:71
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
qgsalgorithmpolygonstolines.h
QgsWkbTypes::CurvePolygon
@ CurvePolygon
Definition: qgswkbtypes.h:82
QgsFeatureList
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:882
QgsWkbTypes::hasM
static bool hasM(Type type) SIP_HOLDGIL
Tests whether a WKB type contains m values.
Definition: qgswkbtypes.h:1130
qgscurvepolygon.h
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
Abstract base class for all geometries.
Definition: qgsabstractgeometry.h:79
qgscurve.h
QgsGeometry
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
QgsGeometryPartIterator
Java-style iterator for traversal of parts of a geometry.
Definition: qgsabstractgeometry.h:1207
QgsFeature::hasGeometry
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:230
QgsWkbTypes::Polygon
@ Polygon
Definition: qgswkbtypes.h:74
QgsGeometryPartIterator::next
QgsAbstractGeometry * next()
Returns next part of the geometry (undefined behavior if hasNext() returns false before calling next(...
Definition: qgsabstractgeometry.cpp:561
QgsWkbTypes::hasZ
static bool hasZ(Type type) SIP_HOLDGIL
Tests whether a WKB type contains the z-dimension.
Definition: qgswkbtypes.h:1080
qgsgeometrycollection.h
QgsFeature
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:55
QgsWkbTypes::singleType
static Type singleType(Type type) SIP_HOLDGIL
Returns the single type for a WKB type.
Definition: qgswkbtypes.h:157
QgsProcessing::SourceType
SourceType
Data source types enum.
Definition: qgsprocessing.h:45
QgsGeometry::wkbType
QgsWkbTypes::Type wkbType() const SIP_HOLDGIL
Returns type of the geometry as a WKB type (point / linestring / polygon etc.)
Definition: qgsgeometry.cpp:357
qgsmultilinestring.h