QGIS API Documentation 3.43.0-Master (2366440f66a)
qgsalgorithmpolygonize.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmdpolygonize.cpp
3 ---------------------
4 begin : May 2020
5 copyright : (C) 2020 by Alexander Bruy
6 email : alexander dot bruy at gmail dot com
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
20
22
23QString QgsPolygonizeAlgorithm::name() const
24{
25 return QStringLiteral( "polygonize" );
26}
27
28QString QgsPolygonizeAlgorithm::displayName() const
29{
30 return QObject::tr( "Polygonize" );
31}
32
33QString QgsPolygonizeAlgorithm::shortHelpString() const
34{
35 return QObject::tr( "This algorithm creates a polygon layer from the input lines layer." );
36}
37
38QString QgsPolygonizeAlgorithm::shortDescription() const
39{
40 return QObject::tr( "Creates a polygon layer from the input lines layer." );
41}
42
43QStringList QgsPolygonizeAlgorithm::tags() const
44{
45 return QObject::tr( "create,lines,polygons,convert" ).split( ',' );
46}
47
48QString QgsPolygonizeAlgorithm::group() const
49{
50 return QObject::tr( "Vector geometry" );
51}
52
53QString QgsPolygonizeAlgorithm::groupId() const
54{
55 return QStringLiteral( "vectorgeometry" );
56}
57
58void QgsPolygonizeAlgorithm::initAlgorithm( const QVariantMap & )
59{
60 addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ), QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorLine ) ) );
61 addParameter( new QgsProcessingParameterBoolean( QStringLiteral( "KEEP_FIELDS" ), QObject::tr( "Keep table structure of line layer" ), false ) );
62 addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Polygons" ), Qgis::ProcessingSourceType::VectorPolygon ) );
63 addOutput( new QgsProcessingOutputNumber( QStringLiteral( "NUM_POLYGONS" ), QObject::tr( "Number of polygons" ) ) );
64}
65
66QgsPolygonizeAlgorithm *QgsPolygonizeAlgorithm::createInstance() const
67{
68 return new QgsPolygonizeAlgorithm();
69}
70
71QVariantMap QgsPolygonizeAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
72{
73 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
74 if ( !source )
75 throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "INPUT" ) ) );
76
77 QgsFields fields = QgsFields();
78 if ( parameterAsBoolean( parameters, QStringLiteral( "KEEP_FIELDS" ), context ) )
79 fields = source->fields();
80
81 QString dest;
82 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, fields, Qgis::WkbType::Polygon, source->sourceCrs() ) );
83 if ( !sink )
84 throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "OUTPUT" ) ) );
85
86 int polygonCount = 0;
87
88 feedback->pushInfo( QObject::tr( "Collecting lines…" ) );
89 const int i = 0;
90 double step = source->featureCount() > 0 ? 40.0 / source->featureCount() : 1;
91 QgsFeature f;
92 QgsFeatureIterator features = source->getFeatures( QgsFeatureRequest().setNoAttributes() );
93 QVector<QgsGeometry> linesList;
94 linesList.reserve( source->featureCount() );
95 while ( features.nextFeature( f ) )
96 {
97 if ( feedback->isCanceled() )
98 break;
99
100 if ( f.hasGeometry() )
101 linesList << f.geometry();
102
103 feedback->setProgress( i * step );
104 }
105 feedback->setProgress( 40 );
106
107 feedback->pushInfo( QObject::tr( "Noding lines…" ) );
108 const QgsGeometry lines = QgsGeometry::unaryUnion( linesList );
109 if ( feedback->isCanceled() )
110 return QVariantMap();
111 feedback->setProgress( 45 );
112
113 feedback->pushInfo( QObject::tr( "Polygonizing…" ) );
114 const QgsGeometry polygons = QgsGeometry::polygonize( QVector<QgsGeometry>() << lines );
115 if ( polygons.isEmpty() )
116 feedback->reportError( QObject::tr( "No polygons were created." ) );
117
118 feedback->setProgress( 50 );
119
120 if ( !polygons.isEmpty() )
121 {
122 const QgsGeometryCollection *collection = qgsgeometry_cast<const QgsGeometryCollection *>( polygons.constGet() );
123 const int numGeometries = collection ? collection->numGeometries() : 1;
124 step = numGeometries > 0 ? 50.0 / numGeometries : 1;
125
126 int part = 0;
127 for ( auto partIt = polygons.const_parts_begin(); partIt != polygons.const_parts_end(); ++partIt, ++part )
128 {
129 if ( feedback->isCanceled() )
130 break;
131
132 QgsFeature outFeat;
133 outFeat.setGeometry( QgsGeometry( ( *partIt )->clone() ) );
134 if ( !sink->addFeature( outFeat, QgsFeatureSink::FastInsert ) )
135 throw QgsProcessingException( writeFeatureError( sink.get(), parameters, QStringLiteral( "OUTPUT" ) ) );
136 feedback->setProgress( 50 + part * step );
137 polygonCount += 1;
138 }
139 }
140
141 sink->finalize();
142
143 QVariantMap outputs;
144 outputs.insert( QStringLiteral( "OUTPUT" ), dest );
145 outputs.insert( QStringLiteral( "NUM_POLYGONS" ), polygonCount );
146 return outputs;
147}
148
@ VectorPolygon
Vector polygon layers.
@ VectorLine
Vector line layers.
@ Polygon
Polygon.
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
Fetch next feature and stores in f, returns true on success.
Wraps a request for features to a vector layer (or directly its vector data provider).
@ FastInsert
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
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.
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition qgsfeedback.h:53
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition qgsfeedback.h:61
Container of fields for a vector layer.
Definition qgsfields.h:46
int numGeometries() const
Returns the number of geometries within the collection.
A geometry is the spatial representation of a feature.
QgsAbstractGeometry::const_part_iterator const_parts_begin() const
Returns STL-style const iterator pointing to the first part of the geometry.
static QgsGeometry polygonize(const QVector< QgsGeometry > &geometries)
Creates a GeometryCollection geometry containing possible polygons formed from the constituent linewo...
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
bool isEmpty() const
Returns true if the geometry is empty (eg a linestring with no vertices, or a collection with no geom...
QgsAbstractGeometry::const_part_iterator const_parts_end() const
Returns STL-style iterator pointing to the imaginary part after the last part of the geometry.
static QgsGeometry unaryUnion(const QVector< QgsGeometry > &geometries, const QgsGeometryParameters &parameters=QgsGeometryParameters())
Compute the unary union on a list of geometries.
Contains information about the context in which a processing algorithm is executed.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
virtual void pushInfo(const QString &info)
Pushes a general informational message from the algorithm.
virtual void reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.
A numeric output for processing algorithms.
A boolean parameter for processing algorithms.
A feature sink output for processing algorithms.
An input feature source (such as vector layers) parameter for processing algorithms.