/
qgsalgorithmpolygonstolines.cpp
158 lines (123 loc) · 4.7 KB
/
qgsalgorithmpolygonstolines.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/***************************************************************************
qgsalgorithmpolygonstolines.cpp
---------------------
begin : January 2019
copyright : (C) 2019 by Matthias Kuhn
email : matthias@opengis.ch
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsalgorithmpolygonstolines.h"
#include "qgsgeometrycollection.h"
#include "qgscurvepolygon.h"
#include "qgscurve.h"
#include "qgsmultilinestring.h"
///@cond PRIVATE
QString QgsPolygonsToLinesAlgorithm::name() const
{
return QStringLiteral( "polygonstolines" );
}
QString QgsPolygonsToLinesAlgorithm::displayName() const
{
return QObject::tr( "Polygons to lines" );
}
QStringList QgsPolygonsToLinesAlgorithm::tags() const
{
return QObject::tr( "line,polygon,convert" ).split( ',' );
}
QString QgsPolygonsToLinesAlgorithm::group() const
{
return QObject::tr( "Vector geometry" );
}
QString QgsPolygonsToLinesAlgorithm::groupId() const
{
return QStringLiteral( "vectorgeometry" );
}
QString QgsPolygonsToLinesAlgorithm::outputName() const
{
return QObject::tr( "Lines" );
}
QgsProcessing::SourceType QgsPolygonsToLinesAlgorithm::outputLayerType() const
{
return QgsProcessing::TypeVectorLine;
}
QgsWkbTypes::Type QgsPolygonsToLinesAlgorithm::outputWkbType( QgsWkbTypes::Type inputWkbType ) const
{
QgsWkbTypes::Type wkbType = QgsWkbTypes::Unknown;
if ( QgsWkbTypes::singleType( QgsWkbTypes::flatType( inputWkbType ) ) == QgsWkbTypes::Polygon )
wkbType = QgsWkbTypes::MultiLineString;
else if ( QgsWkbTypes::singleType( QgsWkbTypes::flatType( inputWkbType ) ) == QgsWkbTypes::CurvePolygon )
wkbType = QgsWkbTypes::MultiCurve;
if ( QgsWkbTypes::hasM( inputWkbType ) )
wkbType = QgsWkbTypes::addM( wkbType );
if ( QgsWkbTypes::hasZ( inputWkbType ) )
wkbType = QgsWkbTypes::addZ( wkbType );
return wkbType;
}
QString QgsPolygonsToLinesAlgorithm::shortHelpString() const
{
return QObject::tr( "Converts polygons to lines" );
}
QString QgsPolygonsToLinesAlgorithm::shortDescription() const
{
return QObject::tr( "Converts polygons to lines." );
}
QgsPolygonsToLinesAlgorithm *QgsPolygonsToLinesAlgorithm::createInstance() const
{
return new QgsPolygonsToLinesAlgorithm();
}
QList<int> QgsPolygonsToLinesAlgorithm::inputLayerTypes() const
{
return QList<int>() << QgsProcessing::TypeVectorPolygon;
}
QgsFeatureList QgsPolygonsToLinesAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
{
Q_UNUSED( context )
QgsFeatureList result;
QgsFeature feat = feature;
if ( feat.hasGeometry() )
feat.setGeometry( convertToLines( feat.geometry() ) );
result << feat;
return result;
}
QgsGeometry QgsPolygonsToLinesAlgorithm::convertToLines( const QgsGeometry &geometry ) const
{
auto rings = extractRings( geometry.constGet() );
QgsWkbTypes::Type resultType = outputWkbType( geometry.wkbType() );
std::unique_ptr<QgsMultiCurve> lineGeometry;
if ( QgsWkbTypes::flatType( resultType ) == QgsWkbTypes::MultiLineString )
lineGeometry = std::make_unique<QgsMultiLineString>();
else
lineGeometry = std::make_unique<QgsMultiCurve>();
lineGeometry->reserve( rings.size() );
for ( auto ring : std::as_const( rings ) )
lineGeometry->addGeometry( ring );
return QgsGeometry( lineGeometry.release() );
}
QList<QgsCurve *> QgsPolygonsToLinesAlgorithm::extractRings( const QgsAbstractGeometry *geom ) const
{
QList<QgsCurve *> rings;
if ( QgsGeometryCollection *collection = qgsgeometry_cast<QgsGeometryCollection *>( geom ) )
{
QgsGeometryPartIterator parts = collection->parts();
while ( parts.hasNext() )
rings.append( extractRings( parts.next() ) );
}
else if ( QgsCurvePolygon *polygon = qgsgeometry_cast<QgsCurvePolygon *>( geom ) )
{
if ( auto exteriorRing = polygon->exteriorRing() )
rings.append( exteriorRing->clone() );
for ( int i = 0; i < polygon->numInteriorRings(); ++i )
{
rings.append( polygon->interiorRing( i )->clone() );
}
}
return rings;
}
///@endcond