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