QGIS API Documentation 4.3.0-Master (ffcfc20b9b4)
Loading...
Searching...
No Matches
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
19
21
22#include <QString>
23
24using namespace Qt::StringLiterals;
25
27
28QString QgsPolygonizeAlgorithm::name() const
29{
30 return u"polygonize"_s;
31}
32
33QString QgsPolygonizeAlgorithm::displayName() const
34{
35 return QObject::tr( "Polygonize" );
36}
37
38QString QgsPolygonizeAlgorithm::shortHelpString() const
39{
40 return QObject::tr( "This algorithm creates a polygon layer from the input lines layer." );
41}
42
43QString QgsPolygonizeAlgorithm::shortDescription() const
44{
45 return QObject::tr( "Creates a polygon layer from the input lines layer." );
46}
47
48QStringList QgsPolygonizeAlgorithm::tags() const
49{
50 return QObject::tr( "create,lines,polygons,convert" ).split( ',' );
51}
52
53QString QgsPolygonizeAlgorithm::group() const
54{
55 return QObject::tr( "Vector geometry" );
56}
57
58QString QgsPolygonizeAlgorithm::groupId() const
59{
60 return u"vectorgeometry"_s;
61}
62
63void QgsPolygonizeAlgorithm::initAlgorithm( const QVariantMap & )
64{
65 addParameter( new QgsProcessingParameterFeatureSource( u"INPUT"_s, QObject::tr( "Input layer" ), QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorLine ) ) );
66 addParameter( new QgsProcessingParameterBoolean( u"KEEP_FIELDS"_s, QObject::tr( "Keep table structure of line layer" ), false ) );
67 addParameter( new QgsProcessingParameterFeatureSink( u"OUTPUT"_s, QObject::tr( "Polygons" ), Qgis::ProcessingSourceType::VectorPolygon ) );
68 addOutput( new QgsProcessingOutputNumber( u"NUM_POLYGONS"_s, QObject::tr( "Number of polygons" ) ) );
69}
70
71QgsPolygonizeAlgorithm *QgsPolygonizeAlgorithm::createInstance() const
72{
73 return new QgsPolygonizeAlgorithm();
74}
75
76QVariantMap QgsPolygonizeAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
77{
78 QGS_MARK_ALGORITHM_SOURCE
79
80 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, u"INPUT"_s, context ) );
81 if ( !source )
82 throw QgsProcessingException( invalidSourceError( parameters, u"INPUT"_s ) );
83
84 QgsFields fields = QgsFields();
85 if ( parameterAsBoolean( parameters, u"KEEP_FIELDS"_s, context ) )
86 fields = source->fields();
87
88 QString dest;
89 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, u"OUTPUT"_s, context, dest, fields, Qgis::WkbType::Polygon, source->sourceCrs() ) );
90 if ( !sink )
91 throw QgsProcessingException( invalidSinkError( parameters, u"OUTPUT"_s ) );
92
93 int polygonCount = 0;
94
95 feedback->pushInfo( QObject::tr( "Collecting lines…" ) );
96 const int i = 0;
97 double step = source->featureCount() > 0 ? 40.0 / source->featureCount() : 1;
98 QgsFeature f;
99 QgsFeatureIterator features = source->getFeatures( QgsFeatureRequest().setNoAttributes() );
100 QVector<QgsGeometry> linesList;
101 linesList.reserve( source->featureCount() );
102 while ( features.nextFeature( f ) )
103 {
104 if ( feedback->isCanceled() )
105 break;
106
107 if ( f.hasGeometry() )
108 linesList << f.geometry();
109
110 feedback->setProgress( i * step );
111 }
112 feedback->setProgress( 40 );
113
114 feedback->pushInfo( QObject::tr( "Noding lines…" ) );
115 const QgsGeometry lines = QgsGeometry::unaryUnion( linesList, QgsGeometryParameters(), feedback );
116 if ( feedback->isCanceled() )
117 return QVariantMap();
118 feedback->setProgress( 45 );
119
120 feedback->pushInfo( QObject::tr( "Polygonizing…" ) );
121 const QgsGeometry polygons = QgsGeometry::polygonize( QVector<QgsGeometry>() << lines );
122 if ( polygons.isEmpty() )
123 feedback->reportError( QObject::tr( "No polygons were created." ) );
124
125 feedback->setProgress( 50 );
126
127 if ( !polygons.isEmpty() )
128 {
130 const int numGeometries = collection ? collection->numGeometries() : 1;
131 step = numGeometries > 0 ? 50.0 / numGeometries : 1;
132
133 int part = 0;
134 for ( auto partIt = polygons.const_parts_begin(); partIt != polygons.const_parts_end(); ++partIt, ++part )
135 {
136 if ( feedback->isCanceled() )
137 break;
138
139 QgsFeature outFeat;
140 outFeat.setGeometry( QgsGeometry( ( *partIt )->clone() ) );
141 if ( !sink->addFeature( outFeat, QgsFeatureSink::FastInsert ) )
142 throw QgsProcessingException( writeFeatureError( sink.get(), parameters, u"OUTPUT"_s ) );
143 else
144 feedback->featureAddedToSink( u"OUTPUT"_s );
145 feedback->setProgress( 50 + part * step );
146 polygonCount += 1;
147 }
148 }
149
150 sink->finalize();
151 feedback->featureSinkFinalized( u"OUTPUT"_s );
152
153 QVariantMap outputs;
154 outputs.insert( u"OUTPUT"_s, dest );
155 outputs.insert( u"NUM_POLYGONS"_s, polygonCount );
156 return outputs;
157}
158
@ VectorPolygon
Vector polygon layers.
Definition qgis.h:3752
@ VectorLine
Vector line layers.
Definition qgis.h:3751
@ Polygon
Polygon.
Definition qgis.h:298
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:60
QgsGeometry geometry
Definition qgsfeature.h:66
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:56
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition qgsfeedback.h:65
Container of fields for a vector layer.
Definition qgsfields.h:45
int numGeometries() const
Returns the number of geometries within the collection.
Encapsulates parameters under which a geometry operation is performed.
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.
static QgsGeometry unaryUnion(const QVector< QgsGeometry > &geometries, const QgsGeometryParameters &parameters=QgsGeometryParameters(), QgsFeedback *feedback=nullptr)
Compute the unary union on a list of geometries.
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.
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.
void featureAddedToSink(const QString &output)
Reports that a feature was added to the the sink associated with the specified algorithm output.
virtual void pushInfo(const QString &info)
Pushes a general informational message from the algorithm.
void featureSinkFinalized(const QString &output)
Reports that a feature sink has been finalized.
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.
T qgsgeometry_cast(QgsAbstractGeometry *geom)