QGIS API Documentation 3.99.0-Master (357b655ed83)
Loading...
Searching...
No Matches
qgsalgorithmcoverageunion.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmcoverageunion.cpp
3 ---------------------
4 begin : October 2023
5 copyright : (C) 2023 by Nyall Dawson
6 email : nyall dot dawson 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
18
20
22#include "qgsgeos.h"
23
24#include <QString>
25
26using namespace Qt::StringLiterals;
27
29
30QString QgsCoverageUnionAlgorithm::name() const
31{
32 return u"coverageunion"_s;
33}
34
35QString QgsCoverageUnionAlgorithm::displayName() const
36{
37 return QObject::tr( "Dissolve coverage" );
38}
39
40QStringList QgsCoverageUnionAlgorithm::tags() const
41{
42 return QObject::tr( "union,merge,topological,boundary" ).split( ',' );
43}
44
45QString QgsCoverageUnionAlgorithm::group() const
46{
47 return QObject::tr( "Vector coverage" );
48}
49
50QString QgsCoverageUnionAlgorithm::groupId() const
51{
52 return u"vectorcoverage"_s;
53}
54
55void QgsCoverageUnionAlgorithm::initAlgorithm( const QVariantMap & )
56{
57 addParameter( new QgsProcessingParameterFeatureSource( u"INPUT"_s, QObject::tr( "Input layer" ), QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorPolygon ) ) );
58 addParameter( new QgsProcessingParameterFeatureSink( u"OUTPUT"_s, QObject::tr( "Dissolved" ), Qgis::ProcessingSourceType::VectorPolygon ) );
59}
60
61QString QgsCoverageUnionAlgorithm::shortDescription() const
62{
63 return QObject::tr( "Dissolves a coverage of polygon features." );
64}
65
66QString QgsCoverageUnionAlgorithm::shortHelpString() const
67{
68 return QObject::tr( "This algorithm operates on a coverage (represented as a set of polygon features "
69 "with exactly matching edge geometry) to dissolve (union) the geometries.\n\n"
70 "It provides a heavily optimized approach for unioning these features compared with "
71 "the standard Dissolve tools." );
72}
73
74QgsCoverageUnionAlgorithm *QgsCoverageUnionAlgorithm::createInstance() const
75{
76 return new QgsCoverageUnionAlgorithm();
77}
78
79QVariantMap QgsCoverageUnionAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
80{
81 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, u"INPUT"_s, context ) );
82 if ( !source )
83 throw QgsProcessingException( invalidSourceError( parameters, u"INPUT"_s ) );
84
85 QString sinkId;
86 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, u"OUTPUT"_s, context, sinkId, source->fields(), Qgis::WkbType::MultiPolygon, source->sourceCrs() ) );
87 if ( !sink )
88 throw QgsProcessingException( invalidSinkError( parameters, u"OUTPUT"_s ) );
89
90 // we have to build up a list of geometries in advance
91 QgsGeometryCollection collection;
92
93 const long count = source->featureCount();
94 if ( count > 0 )
95 {
96 collection.reserve( count );
97 }
98
99 const double step = count > 0 ? 100.0 / count : 1;
100 int current = 0;
101
102 feedback->pushInfo( QObject::tr( "Collecting features" ) );
103
104 QgsFeature inFeature;
106 req.setNoAttributes();
107 QgsFeatureIterator features = source->getFeatures( req );
108 while ( features.nextFeature( inFeature ) )
109 {
110 if ( feedback->isCanceled() )
111 {
112 break;
113 }
114
115 if ( inFeature.hasGeometry() )
116 {
117 collection.addGeometry( inFeature.geometry().constGet()->clone() );
118 }
119
120 feedback->setProgress( current * step * 0.2 );
121 current++;
122 }
123
124 feedback->pushInfo( QObject::tr( "Dissolving coverage" ) );
125
126 QgsGeos geos( &collection );
127 QString error;
128
129 switch ( source->invalidGeometryCheck() )
130 {
132 break;
133
136 {
137 if ( geos.validateCoverage( 0, nullptr, &error ) != Qgis::CoverageValidityResult::Valid )
138 {
139 throw QgsProcessingException( QObject::tr( "Coverage is not valid" ) );
140 }
141 break;
142 }
143 }
144
145 std::unique_ptr<QgsAbstractGeometry> dissolved = geos.unionCoverage( &error );
146
147 if ( !dissolved )
148 {
149 if ( !error.isEmpty() )
150 throw QgsProcessingException( error );
151 else
152 throw QgsProcessingException( QObject::tr( "No geometry was returned for dissolved coverage" ) );
153 }
154
155 feedback->setProgress( 95 );
156
157 feedback->pushInfo( QObject::tr( "Storing output" ) );
158 QgsFeature outFeature( source->fields() );
159 outFeature.setGeometry( std::move( dissolved ) );
160 if ( !sink->addFeature( outFeature, QgsFeatureSink::FastInsert ) )
161 throw QgsProcessingException( writeFeatureError( sink.get(), parameters, u"OUTPUT"_s ) );
162
163 sink->finalize();
164
165 QVariantMap outputs;
166 outputs.insert( u"OUTPUT"_s, sinkId );
167 return outputs;
168}
169
@ VectorPolygon
Vector polygon layers.
Definition qgis.h:3607
@ Valid
Coverage is valid.
Definition qgis.h:2227
@ NoCheck
No invalid geometry checking.
Definition qgis.h:2297
@ AbortOnInvalid
Close iterator on encountering any features with invalid geometry. This requires a slow geometry vali...
Definition qgis.h:2299
@ SkipInvalid
Skip any features with invalid geometry. This requires a slow geometry validity check for every featu...
Definition qgis.h:2298
@ MultiPolygon
MultiPolygon.
Definition qgis.h:288
virtual QgsAbstractGeometry * clone() const =0
Clones the geometry by performing a deep copy.
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).
QgsFeatureRequest & setNoAttributes()
Set that no attributes will be fetched.
@ 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.
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
void reserve(int size)
Attempts to allocate memory for at least size geometries.
virtual bool addGeometry(QgsAbstractGeometry *g)
Adds a geometry and takes ownership. Returns true in case of success.
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
Does vector analysis using the GEOS library and handles import, export, and exception handling.
Definition qgsgeos.h:141
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.
A feature sink output for processing algorithms.
An input feature source (such as vector layers) parameter for processing algorithms.
Contains geos related utilities and functions.
Definition qgsgeos.h:77