Skip to content

Commit 4c8af20

Browse files
committedJan 18, 2019
Add native polygonstolines algorithm
1 parent 8709ab6 commit 4c8af20

File tree

6 files changed

+216
-133
lines changed

6 files changed

+216
-133
lines changed
 

‎python/plugins/processing/algs/qgis/PolygonsToLines.py

Lines changed: 0 additions & 130 deletions
This file was deleted.

‎python/plugins/processing/algs/qgis/QgisAlgorithmProvider.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@
9898
from .PointsToPaths import PointsToPaths
9999
from .PoleOfInaccessibility import PoleOfInaccessibility
100100
from .Polygonize import Polygonize
101-
from .PolygonsToLines import PolygonsToLines
102101
from .PostGISExecuteSQL import PostGISExecuteSQL
103102
from .PostGISExecuteAndLoadSQL import PostGISExecuteAndLoadSQL
104103
from .RandomExtract import RandomExtract
@@ -210,7 +209,6 @@ def getAlgs(self):
210209
PointsToPaths(),
211210
PoleOfInaccessibility(),
212211
Polygonize(),
213-
PolygonsToLines(),
214212
PostGISExecuteSQL(),
215213
PostGISExecuteAndLoadSQL(),
216214
RandomExtract(),

‎src/analysis/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ SET(QGIS_ANALYSIS_SRCS
7373
processing/qgsalgorithmorientedminimumboundingbox.cpp
7474
processing/qgsalgorithmpackage.cpp
7575
processing/qgsalgorithmarrayoffsetlines.cpp
76+
processing/qgsalgorithmpolygonstolines.cpp
7677
processing/qgsalgorithmpointonsurface.cpp
7778
processing/qgsalgorithmprojectpointcartesian.cpp
7879
processing/qgsalgorithmpromotetomultipart.cpp
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
/***************************************************************************
2+
qgsalgorithmpolygonstolines.cpp
3+
---------------------
4+
begin : January 2019
5+
copyright : (C) 2019 by Matthias Kuhn
6+
email : matthias@opengis.ch
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+
18+
#include "qgsalgorithmpolygonstolines.h"
19+
#include "qgsgeometrycollection.h"
20+
#include "qgscurvepolygon.h"
21+
#include "qgscurve.h"
22+
#include "qgsmultilinestring.h"
23+
24+
///@cond PRIVATE
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 creation" );
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+
QString QgsPolygonsToLinesAlgorithm::shortHelpString() const
57+
{
58+
return QObject::tr( "Converts polygons to lines" );
59+
}
60+
61+
QString QgsPolygonsToLinesAlgorithm::shortDescription() const
62+
{
63+
return QObject::tr( "Converts polygons to lines." );
64+
}
65+
66+
QgsPolygonsToLinesAlgorithm *QgsPolygonsToLinesAlgorithm::createInstance() const
67+
{
68+
return new QgsPolygonsToLinesAlgorithm();
69+
}
70+
71+
QList<int> QgsPolygonsToLinesAlgorithm::inputLayerTypes() const
72+
{
73+
return QList< int >() << QgsProcessing::TypeVectorPolygon;
74+
}
75+
76+
QgsFeatureList QgsPolygonsToLinesAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
77+
{
78+
QgsFeatureList result;
79+
QgsFeature feat = feature;
80+
if ( feat.hasGeometry() )
81+
feat.setGeometry( convertToLines( feat.geometry() ) );
82+
83+
result << feat;
84+
return result;
85+
}
86+
87+
QgsGeometry QgsPolygonsToLinesAlgorithm::convertToLines( const QgsGeometry &geometry ) const
88+
{
89+
auto rings = extractRings( geometry.constGet() );
90+
91+
QgsWkbTypes::Type resultType = outWkbType( geometry.wkbType() );
92+
93+
std::unique_ptr<QgsMultiCurve> lineGeometry;
94+
95+
if ( QgsWkbTypes::flatType( resultType ) == QgsWkbTypes::MultiLineString )
96+
lineGeometry = qgis::make_unique<QgsMultiLineString>();
97+
else
98+
lineGeometry = qgis::make_unique<QgsMultiCurve>();
99+
100+
for ( auto ring : qgis::as_const( rings ) )
101+
lineGeometry->addGeometry( ring );
102+
103+
return QgsGeometry( lineGeometry.release() );
104+
}
105+
106+
QgsWkbTypes::Type QgsPolygonsToLinesAlgorithm::outWkbType( QgsWkbTypes::Type polygonWkbType ) const
107+
{
108+
QgsWkbTypes::Type wkbType = QgsWkbTypes::NoGeometry;
109+
110+
if ( QgsWkbTypes::singleType( QgsWkbTypes::flatType( polygonWkbType ) ) == QgsWkbTypes::Polygon )
111+
wkbType = QgsWkbTypes::MultiLineString;
112+
else if ( QgsWkbTypes::singleType( QgsWkbTypes::flatType( polygonWkbType ) ) == QgsWkbTypes::CurvePolygon )
113+
wkbType = QgsWkbTypes::MultiCurve;
114+
115+
if ( QgsWkbTypes::hasM( polygonWkbType ) )
116+
wkbType = QgsWkbTypes::addM( wkbType );
117+
if ( QgsWkbTypes::hasZ( polygonWkbType ) )
118+
wkbType = QgsWkbTypes::addZ( wkbType );
119+
120+
return wkbType;
121+
}
122+
123+
QList<QgsCurve *> QgsPolygonsToLinesAlgorithm::extractRings( const QgsAbstractGeometry *geom ) const
124+
{
125+
QList<QgsCurve *> rings;
126+
127+
if ( QgsGeometryCollection *collection = qgsgeometry_cast<QgsGeometryCollection *>( geom ) )
128+
{
129+
for ( int i = 0; i < collection->numGeometries(); ++i )
130+
{
131+
rings.append( extractRings( collection->geometryN( i ) ) );
132+
}
133+
}
134+
else if ( QgsCurvePolygon *polygon = qgsgeometry_cast<QgsCurvePolygon *>( geom ) )
135+
{
136+
rings.append( polygon->exteriorRing()->clone() );
137+
for ( int i = 0; i < polygon->numInteriorRings(); ++i )
138+
{
139+
rings.append( polygon->interiorRing( i )->clone() );
140+
}
141+
}
142+
143+
return rings;
144+
}
145+
146+
147+
148+
///@endcond
149+
150+
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/***************************************************************************
2+
qgsalgorithmpolygontolines.h
3+
---------------------
4+
begin : January 2019
5+
copyright : (C) 2019 by Matthias Kuhn
6+
email : matthias@opengis.ch
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+
18+
#ifndef QGSALGORITHMPOLYGONSTOLINES_H
19+
#define QGSALGORITHMPOLYGONSTOLINES_H
20+
21+
#define SIP_NO_FILE
22+
23+
#include "qgis.h"
24+
#include "qgsprocessingalgorithm.h"
25+
26+
///@cond PRIVATE
27+
28+
/**
29+
* Native convert polygons to lines algorithm
30+
*/
31+
class QgsPolygonsToLinesAlgorithm : public QgsProcessingFeatureBasedAlgorithm
32+
{
33+
34+
public:
35+
36+
QgsPolygonsToLinesAlgorithm() = default;
37+
QString name() const override;
38+
QString displayName() const override;
39+
QStringList tags() const override;
40+
QString group() const override;
41+
QString groupId() const override;
42+
QString shortHelpString() const override;
43+
QString shortDescription() const override;
44+
QgsPolygonsToLinesAlgorithm *createInstance() const override SIP_FACTORY;
45+
QList<int> inputLayerTypes() const override;
46+
47+
protected:
48+
QString outputName() const override;
49+
QgsFeatureList processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;
50+
51+
private:
52+
QgsGeometry convertToLines( const QgsGeometry &geometry ) const;
53+
QList<QgsCurve *> extractRings( const QgsAbstractGeometry *geom ) const;
54+
QgsWkbTypes::Type outWkbType( QgsWkbTypes::Type polygonWkbType ) const;
55+
56+
friend class TestQgsProcessingAlgs;
57+
};
58+
59+
///@endcond PRIVATE
60+
61+
#endif // QGSALGORITHMPOLYGONSTOLINES_H
62+
63+

‎src/analysis/processing/qgsnativealgorithms.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@
105105
#include "qgsalgorithmvectorize.h"
106106
#include "qgsalgorithmwedgebuffers.h"
107107
#include "qgsalgorithmzonalhistogram.h"
108-
108+
#include "qgsalgorithmpolygonstolines.h"
109109

110110
///@cond PRIVATE
111111

@@ -243,6 +243,7 @@ void QgsNativeAlgorithms::loadAlgorithms()
243243
addAlgorithm( new QgsVariableWidthBufferByMAlgorithm() );
244244
addAlgorithm( new QgsWedgeBuffersAlgorithm() );
245245
addAlgorithm( new QgsZonalHistogramAlgorithm() );
246+
addAlgorithm( new QgsPolygonsToLinesAlgorithm() );
246247
}
247248

248249

0 commit comments

Comments
 (0)
Please sign in to comment.