QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
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
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"
21#include "qgscurvepolygon.h"
23#include "qgsmultilinestring.h"
24
26
27QString QgsPolygonsToLinesAlgorithm::name() const
28{
29 return QStringLiteral( "polygonstolines" );
30}
31
32QString QgsPolygonsToLinesAlgorithm::displayName() const
33{
34 return QObject::tr( "Polygons to lines" );
35}
36
37QStringList QgsPolygonsToLinesAlgorithm::tags() const
38{
39 return QObject::tr( "line,polygon,convert" ).split( ',' );
40}
41
42QString QgsPolygonsToLinesAlgorithm::group() const
43{
44 return QObject::tr( "Vector geometry" );
45}
46
47QString QgsPolygonsToLinesAlgorithm::groupId() const
48{
49 return QStringLiteral( "vectorgeometry" );
50}
51
52QString QgsPolygonsToLinesAlgorithm::outputName() const
53{
54 return QObject::tr( "Lines" );
55}
56
57Qgis::ProcessingSourceType QgsPolygonsToLinesAlgorithm::outputLayerType() const
58{
60}
61
62Qgis::WkbType QgsPolygonsToLinesAlgorithm::outputWkbType( Qgis::WkbType inputWkbType ) const
63{
65
70
71 if ( QgsWkbTypes::hasM( inputWkbType ) )
72 wkbType = QgsWkbTypes::addM( wkbType );
73 if ( QgsWkbTypes::hasZ( inputWkbType ) )
74 wkbType = QgsWkbTypes::addZ( wkbType );
75
76 return wkbType;
77}
78
79QString QgsPolygonsToLinesAlgorithm::shortHelpString() const
80{
81 return QObject::tr( "This algorithm converts polygons to lines." );
82}
83
84QString QgsPolygonsToLinesAlgorithm::shortDescription() const
85{
86 return QObject::tr( "Converts polygons to lines." );
87}
88
89QgsPolygonsToLinesAlgorithm *QgsPolygonsToLinesAlgorithm::createInstance() const
90{
91 return new QgsPolygonsToLinesAlgorithm();
92}
93
94QList<int> QgsPolygonsToLinesAlgorithm::inputLayerTypes() const
95{
96 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorPolygon );
97}
98
99QgsFeatureList QgsPolygonsToLinesAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
100{
101 Q_UNUSED( context )
102
103 QgsFeatureList result;
104 QgsFeature feat = feature;
105 if ( feat.hasGeometry() )
106 feat.setGeometry( convertToLines( feat.geometry() ) );
107
108 result << feat;
109 return result;
110}
111
112QgsGeometry QgsPolygonsToLinesAlgorithm::convertToLines( const QgsGeometry &geometry ) const
113{
114 auto rings = extractRings( geometry.constGet() );
115
116 Qgis::WkbType resultType = outputWkbType( geometry.wkbType() );
117
118 std::unique_ptr<QgsMultiCurve> lineGeometry;
119
121 lineGeometry = std::make_unique<QgsMultiLineString>();
122 else
123 lineGeometry = std::make_unique<QgsMultiCurve>();
124
125 lineGeometry->reserve( rings.size() );
126 for ( auto ring : std::as_const( rings ) )
127 lineGeometry->addGeometry( ring );
128
129 return QgsGeometry( lineGeometry.release() );
130}
131
132QList<QgsCurve *> QgsPolygonsToLinesAlgorithm::extractRings( const QgsAbstractGeometry *geom ) const
133{
134 QList<QgsCurve *> rings;
135
137 {
138 QgsGeometryConstPartIterator parts = collection->parts();
139 while ( parts.hasNext() )
140 rings.append( extractRings( parts.next() ) );
141 }
142 else if ( const QgsCurvePolygon *polygon = qgsgeometry_cast<const QgsCurvePolygon *>( geom ) )
143 {
144 if ( auto exteriorRing = polygon->exteriorRing() )
145 rings.append( exteriorRing->clone() );
146 for ( int i = 0; i < polygon->numInteriorRings(); ++i )
147 {
148 rings.append( polygon->interiorRing( i )->clone() );
149 }
150 }
151
152 return rings;
153}
154
155
ProcessingSourceType
Processing data source types.
Definition qgis.h:3531
@ VectorPolygon
Vector polygon layers.
Definition qgis.h:3536
@ VectorLine
Vector line layers.
Definition qgis.h:3535
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:277
@ Polygon
Polygon.
Definition qgis.h:281
@ MultiLineString
MultiLineString.
Definition qgis.h:284
@ Unknown
Unknown.
Definition qgis.h:278
@ MultiCurve
MultiCurve.
Definition qgis.h:290
@ CurvePolygon
CurvePolygon.
Definition qgis.h:289
Abstract base class for all geometries.
Curve polygon geometry type.
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.
Java-style iterator for const traversal of parts of a geometry.
bool hasNext() const
Find out whether there are more parts.
const QgsAbstractGeometry * next()
Returns next part of the geometry (undefined behavior if hasNext() returns false before calling next(...
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.
Qgis::WkbType wkbType() const
Returns type of the geometry as a WKB type (point / linestring / polygon etc.).
Contains information about the context in which a processing algorithm is executed.
Base class for providing feedback from a processing algorithm.
static Qgis::WkbType addM(Qgis::WkbType type)
Adds the m dimension to a WKB type and returns the new type.
static Qgis::WkbType addZ(Qgis::WkbType type)
Adds the z dimension to a WKB type and returns the new type.
static Q_INVOKABLE bool hasZ(Qgis::WkbType type)
Tests whether a WKB type contains the z-dimension.
static Qgis::WkbType singleType(Qgis::WkbType type)
Returns the single type for a WKB type.
Definition qgswkbtypes.h:53
static Q_INVOKABLE bool hasM(Qgis::WkbType type)
Tests whether a WKB type contains m values.
static Qgis::WkbType flatType(Qgis::WkbType type)
Returns the flat type for a WKB type.
T qgsgeometry_cast(QgsAbstractGeometry *geom)
QList< QgsFeature > QgsFeatureList