QGIS API Documentation 3.99.0-Master (357b655ed83)
Loading...
Searching...
No Matches
qgsalgorithmcoveragesimplify.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmcoveragesimplify.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 QgsCoverageSimplifyAlgorithm::name() const
31{
32 return u"coveragesimplify"_s;
33}
34
35QString QgsCoverageSimplifyAlgorithm::displayName() const
36{
37 return QObject::tr( "Simplify coverage" );
38}
39
40QStringList QgsCoverageSimplifyAlgorithm::tags() const
41{
42 return QObject::tr( "topological,boundary" ).split( ',' );
43}
44
45QString QgsCoverageSimplifyAlgorithm::group() const
46{
47 return QObject::tr( "Vector coverage" );
48}
49
50QString QgsCoverageSimplifyAlgorithm::groupId() const
51{
52 return u"vectorcoverage"_s;
53}
54
55void QgsCoverageSimplifyAlgorithm::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 QgsProcessingParameterDistance( u"TOLERANCE"_s, QObject::tr( "Tolerance" ), 1.0, u"INPUT"_s, false, 0, 10000000.0 ) );
59 auto boundaryParameter = std::make_unique<QgsProcessingParameterBoolean>( u"PRESERVE_BOUNDARY"_s, QObject::tr( "Preserve boundary" ), false );
60 boundaryParameter->setHelp( QObject::tr( "When enabled the outside edges of the coverage will be preserved without simplification." ) );
61 addParameter( boundaryParameter.release() );
62
63 addParameter( new QgsProcessingParameterFeatureSink( u"OUTPUT"_s, QObject::tr( "Simplified" ), Qgis::ProcessingSourceType::VectorPolygon ) );
64}
65
66QString QgsCoverageSimplifyAlgorithm::shortDescription() const
67{
68 return QObject::tr( "Simplifies a coverage of polygon features while retaining valid coverage." );
69}
70
71QString QgsCoverageSimplifyAlgorithm::shortHelpString() const
72{
73 return QObject::tr( "This algorithm operates on a coverage (represented as a set of polygon features "
74 "with exactly matching edge geometry) to apply a Visvalingam–Whyatt "
75 "simplification to the edges, reducing complexity in proportion with "
76 "the provided tolerance, while retaining a valid coverage (i.e. no edges "
77 "will cross or touch after the simplification).\n\n"
78 "Geometries will never be removed, but they may be simplified down to just "
79 "a triangle. Also, some geometries (such as polygons which have too "
80 "few non-repeated points) will be returned unchanged.\n\n"
81 "If the input dataset is not a valid coverage due to overlaps, "
82 "it will still be simplified, but invalid topology such as crossing "
83 "edges will still be invalid." );
84}
85
86QgsCoverageSimplifyAlgorithm *QgsCoverageSimplifyAlgorithm::createInstance() const
87{
88 return new QgsCoverageSimplifyAlgorithm();
89}
90
91QVariantMap QgsCoverageSimplifyAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
92{
93 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, u"INPUT"_s, context ) );
94 if ( !source )
95 throw QgsProcessingException( invalidSourceError( parameters, u"INPUT"_s ) );
96
97 const bool preserveBoundary = parameterAsBoolean( parameters, u"PRESERVE_BOUNDARY"_s, context );
98 const double tolerance = parameterAsDouble( parameters, u"TOLERANCE"_s, context );
99
100 QString sinkId;
101 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, u"OUTPUT"_s, context, sinkId, source->fields(), source->wkbType(), source->sourceCrs() ) );
102 if ( !sink )
103 throw QgsProcessingException( invalidSinkError( parameters, u"OUTPUT"_s ) );
104
105 // we have no choice but to build up a list of features in advance
106 QVector<QgsFeature> featuresWithGeom;
107 QVector<QgsFeature> featuresWithoutGeom;
108 QgsGeometryCollection collection;
109
110 const long count = source->featureCount();
111 if ( count > 0 )
112 {
113 featuresWithGeom.reserve( count );
114 collection.reserve( count );
115 }
116
117 const double step = count > 0 ? 100.0 / count : 1;
118 int current = 0;
119
120 feedback->pushInfo( QObject::tr( "Collecting features" ) );
121
122 QgsFeature inFeature;
123 QgsFeatureIterator features = source->getFeatures();
124 while ( features.nextFeature( inFeature ) )
125 {
126 if ( feedback->isCanceled() )
127 {
128 break;
129 }
130
131 if ( inFeature.hasGeometry() )
132 {
133 featuresWithGeom.append( inFeature );
134 collection.addGeometry( inFeature.geometry().constGet()->clone() );
135 }
136 else
137 {
138 featuresWithoutGeom.append( inFeature );
139 }
140
141
142 feedback->setProgress( current * step * 0.2 );
143 current++;
144 }
145
146 QString error;
147 QgsGeos geos( &collection );
148 switch ( source->invalidGeometryCheck() )
149 {
151 break;
152
155 {
156 if ( geos.validateCoverage( 0, nullptr, &error ) != Qgis::CoverageValidityResult::Valid )
157 {
158 throw QgsProcessingException( QObject::tr( "Coverage is not valid" ) );
159 }
160 break;
161 }
162 }
163
164 feedback->pushInfo( QObject::tr( "Simplifying coverage" ) );
165
166 std::unique_ptr<QgsAbstractGeometry> simplified;
167 try
168 {
169 simplified = geos.simplifyCoverageVW( tolerance, preserveBoundary, &error );
170 }
171 catch ( QgsNotSupportedException &e )
172 {
173 throw QgsProcessingException( e.what() );
174 }
175
176 if ( !simplified )
177 {
178 if ( !error.isEmpty() )
179 throw QgsProcessingException( error );
180 else
181 throw QgsProcessingException( QObject::tr( "No geometry was returned for simplified coverage" ) );
182 }
183
184 feedback->setProgress( 80 );
185
186 feedback->pushInfo( QObject::tr( "Storing features" ) );
187 long long featureIndex = 0;
188 for ( auto partsIt = simplified->const_parts_begin(); partsIt != simplified->const_parts_end(); ++partsIt )
189 {
190 QgsFeature outFeature = featuresWithGeom.value( featureIndex );
191 outFeature.setGeometry( QgsGeometry( *partsIt ? ( *partsIt )->clone() : nullptr ) );
192 if ( !sink->addFeature( outFeature, QgsFeatureSink::FastInsert ) )
193 throw QgsProcessingException( writeFeatureError( sink.get(), parameters, u"OUTPUT"_s ) );
194
195
196 feedback->setProgress( featureIndex * step * 0.2 + 80 );
197 featureIndex++;
198 }
199 for ( const QgsFeature &feature : featuresWithoutGeom )
200 {
201 QgsFeature outFeature = feature;
202 if ( !sink->addFeature( outFeature, QgsFeatureSink::FastInsert ) )
203 throw QgsProcessingException( writeFeatureError( sink.get(), parameters, u"OUTPUT"_s ) );
204 }
205
206 sink->finalize();
207
208 QVariantMap outputs;
209 outputs.insert( u"OUTPUT"_s, sinkId );
210 return outputs;
211}
212
@ 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
virtual QgsAbstractGeometry * clone() const =0
Clones the geometry by performing a deep copy.
QString what() const
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.
@ 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
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.
A geometry is the spatial representation of a feature.
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
Custom exception class which is raised when an operation is not supported.
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 double numeric parameter for distance values.
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