QGIS API Documentation 3.99.0-Master (09f76ad7019)
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 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, u"INPUT"_s, context ) );
79 if ( !source )
80 throw QgsProcessingException( invalidSourceError( parameters, u"INPUT"_s ) );
81
82 QgsFields fields = QgsFields();
83 if ( parameterAsBoolean( parameters, u"KEEP_FIELDS"_s, context ) )
84 fields = source->fields();
85
86 QString dest;
87 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, u"OUTPUT"_s, context, dest, fields, Qgis::WkbType::Polygon, source->sourceCrs() ) );
88 if ( !sink )
89 throw QgsProcessingException( invalidSinkError( parameters, u"OUTPUT"_s ) );
90
91 int polygonCount = 0;
92
93 feedback->pushInfo( QObject::tr( "Collecting lines…" ) );
94 const int i = 0;
95 double step = source->featureCount() > 0 ? 40.0 / source->featureCount() : 1;
96 QgsFeature f;
97 QgsFeatureIterator features = source->getFeatures( QgsFeatureRequest().setNoAttributes() );
98 QVector<QgsGeometry> linesList;
99 linesList.reserve( source->featureCount() );
100 while ( features.nextFeature( f ) )
101 {
102 if ( feedback->isCanceled() )
103 break;
104
105 if ( f.hasGeometry() )
106 linesList << f.geometry();
107
108 feedback->setProgress( i * step );
109 }
110 feedback->setProgress( 40 );
111
112 feedback->pushInfo( QObject::tr( "Noding lines…" ) );
113 const QgsGeometry lines = QgsGeometry::unaryUnion( linesList );
114 if ( feedback->isCanceled() )
115 return QVariantMap();
116 feedback->setProgress( 45 );
117
118 feedback->pushInfo( QObject::tr( "Polygonizing…" ) );
119 const QgsGeometry polygons = QgsGeometry::polygonize( QVector<QgsGeometry>() << lines );
120 if ( polygons.isEmpty() )
121 feedback->reportError( QObject::tr( "No polygons were created." ) );
122
123 feedback->setProgress( 50 );
124
125 if ( !polygons.isEmpty() )
126 {
128 const int numGeometries = collection ? collection->numGeometries() : 1;
129 step = numGeometries > 0 ? 50.0 / numGeometries : 1;
130
131 int part = 0;
132 for ( auto partIt = polygons.const_parts_begin(); partIt != polygons.const_parts_end(); ++partIt, ++part )
133 {
134 if ( feedback->isCanceled() )
135 break;
136
137 QgsFeature outFeat;
138 outFeat.setGeometry( QgsGeometry( ( *partIt )->clone() ) );
139 if ( !sink->addFeature( outFeat, QgsFeatureSink::FastInsert ) )
140 throw QgsProcessingException( writeFeatureError( sink.get(), parameters, u"OUTPUT"_s ) );
141 feedback->setProgress( 50 + part * step );
142 polygonCount += 1;
143 }
144 }
145
146 sink->finalize();
147
148 QVariantMap outputs;
149 outputs.insert( u"OUTPUT"_s, dest );
150 outputs.insert( u"NUM_POLYGONS"_s, polygonCount );
151 return outputs;
152}
153
@ VectorPolygon
Vector polygon layers.
Definition qgis.h:3607
@ VectorLine
Vector line layers.
Definition qgis.h:3606
@ Polygon
Polygon.
Definition qgis.h:284
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:71
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:55
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition qgsfeedback.h:63
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.
T qgsgeometry_cast(QgsAbstractGeometry *geom)