QGIS API Documentation 3.99.0-Master (357b655ed83)
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
25#include <QString>
26
27using namespace Qt::StringLiterals;
28
30
31QString QgsPolygonsToLinesAlgorithm::name() const
32{
33 return u"polygonstolines"_s;
34}
35
36QString QgsPolygonsToLinesAlgorithm::displayName() const
37{
38 return QObject::tr( "Polygons to lines" );
39}
40
41QStringList QgsPolygonsToLinesAlgorithm::tags() const
42{
43 return QObject::tr( "line,polygon,convert" ).split( ',' );
44}
45
46QString QgsPolygonsToLinesAlgorithm::group() const
47{
48 return QObject::tr( "Vector geometry" );
49}
50
51QString QgsPolygonsToLinesAlgorithm::groupId() const
52{
53 return u"vectorgeometry"_s;
54}
55
56QString QgsPolygonsToLinesAlgorithm::outputName() const
57{
58 return QObject::tr( "Lines" );
59}
60
61Qgis::ProcessingSourceType QgsPolygonsToLinesAlgorithm::outputLayerType() const
62{
64}
65
66Qgis::WkbType QgsPolygonsToLinesAlgorithm::outputWkbType( Qgis::WkbType inputWkbType ) const
67{
69
74
75 if ( QgsWkbTypes::hasM( inputWkbType ) )
76 wkbType = QgsWkbTypes::addM( wkbType );
77 if ( QgsWkbTypes::hasZ( inputWkbType ) )
78 wkbType = QgsWkbTypes::addZ( wkbType );
79
80 return wkbType;
81}
82
83QString QgsPolygonsToLinesAlgorithm::shortHelpString() const
84{
85 return QObject::tr( "This algorithm converts polygons to lines." );
86}
87
88QString QgsPolygonsToLinesAlgorithm::shortDescription() const
89{
90 return QObject::tr( "Converts polygons to lines." );
91}
92
93QgsPolygonsToLinesAlgorithm *QgsPolygonsToLinesAlgorithm::createInstance() const
94{
95 return new QgsPolygonsToLinesAlgorithm();
96}
97
98QList<int> QgsPolygonsToLinesAlgorithm::inputLayerTypes() const
99{
100 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorPolygon );
101}
102
103QgsFeatureList QgsPolygonsToLinesAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
104{
105 Q_UNUSED( context )
106
107 QgsFeatureList result;
108 QgsFeature feat = feature;
109 if ( feat.hasGeometry() )
110 feat.setGeometry( convertToLines( feat.geometry() ) );
111
112 result << feat;
113 return result;
114}
115
116QgsGeometry QgsPolygonsToLinesAlgorithm::convertToLines( const QgsGeometry &geometry ) const
117{
118 auto rings = extractRings( geometry.constGet() );
119
120 Qgis::WkbType resultType = outputWkbType( geometry.wkbType() );
121
122 std::unique_ptr<QgsMultiCurve> lineGeometry;
123
125 lineGeometry = std::make_unique<QgsMultiLineString>();
126 else
127 lineGeometry = std::make_unique<QgsMultiCurve>();
128
129 lineGeometry->reserve( rings.size() );
130 for ( auto ring : std::as_const( rings ) )
131 lineGeometry->addGeometry( ring );
132
133 return QgsGeometry( lineGeometry.release() );
134}
135
136QList<QgsCurve *> QgsPolygonsToLinesAlgorithm::extractRings( const QgsAbstractGeometry *geom ) const
137{
138 QList<QgsCurve *> rings;
139
141 {
142 QgsGeometryConstPartIterator parts = collection->parts();
143 while ( parts.hasNext() )
144 rings.append( extractRings( parts.next() ) );
145 }
146 else if ( const QgsCurvePolygon *polygon = qgsgeometry_cast<const QgsCurvePolygon *>( geom ) )
147 {
148 if ( auto exteriorRing = polygon->exteriorRing() )
149 rings.append( exteriorRing->clone() );
150 for ( int i = 0; i < polygon->numInteriorRings(); ++i )
151 {
152 rings.append( polygon->interiorRing( i )->clone() );
153 }
154 }
155
156 return rings;
157}
158
159
ProcessingSourceType
Processing data source types.
Definition qgis.h:3602
@ VectorPolygon
Vector polygon layers.
Definition qgis.h:3607
@ VectorLine
Vector line layers.
Definition qgis.h:3606
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:280
@ Polygon
Polygon.
Definition qgis.h:284
@ MultiLineString
MultiLineString.
Definition qgis.h:287
@ Unknown
Unknown.
Definition qgis.h:281
@ MultiCurve
MultiCurve.
Definition qgis.h:293
@ CurvePolygon
CurvePolygon.
Definition qgis.h:292
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:60
QgsGeometry geometry
Definition qgsfeature.h:71
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:52
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