26using namespace Qt::StringLiterals;
30QString QgsCoverageSimplifyAlgorithm::name()
const
32 return u
"coveragesimplify"_s;
35QString QgsCoverageSimplifyAlgorithm::displayName()
const
37 return QObject::tr(
"Simplify coverage" );
40QStringList QgsCoverageSimplifyAlgorithm::tags()
const
42 return QObject::tr(
"topological,boundary" ).split(
',' );
45QString QgsCoverageSimplifyAlgorithm::group()
const
47 return QObject::tr(
"Vector coverage" );
50QString QgsCoverageSimplifyAlgorithm::groupId()
const
52 return u
"vectorcoverage"_s;
55void QgsCoverageSimplifyAlgorithm::initAlgorithm(
const QVariantMap & )
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() );
66QString QgsCoverageSimplifyAlgorithm::shortDescription()
const
68 return QObject::tr(
"Simplifies a coverage of polygon features while retaining valid coverage." );
71QString QgsCoverageSimplifyAlgorithm::shortHelpString()
const
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." );
86QgsCoverageSimplifyAlgorithm *QgsCoverageSimplifyAlgorithm::createInstance()
const
88 return new QgsCoverageSimplifyAlgorithm();
93 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, u
"INPUT"_s, context ) );
97 const bool preserveBoundary = parameterAsBoolean( parameters, u
"PRESERVE_BOUNDARY"_s, context );
98 const double tolerance = parameterAsDouble( parameters, u
"TOLERANCE"_s, context );
101 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, u
"OUTPUT"_s, context, sinkId, source->fields(), source->wkbType(), source->sourceCrs() ) );
106 QVector<QgsFeature> featuresWithGeom;
107 QVector<QgsFeature> featuresWithoutGeom;
110 const long count = source->featureCount();
113 featuresWithGeom.reserve( count );
117 const double step = count > 0 ? 100.0 / count : 1;
120 feedback->
pushInfo( QObject::tr(
"Collecting features" ) );
133 featuresWithGeom.append( inFeature );
138 featuresWithoutGeom.append( inFeature );
148 switch ( source->invalidGeometryCheck() )
164 feedback->
pushInfo( QObject::tr(
"Simplifying coverage" ) );
166 std::unique_ptr<QgsAbstractGeometry> simplified;
169 simplified =
geos.simplifyCoverageVW( tolerance, preserveBoundary, &error );
178 if ( !error.isEmpty() )
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 )
190 QgsFeature outFeature = featuresWithGeom.value( featureIndex );
196 feedback->
setProgress( featureIndex * step * 0.2 + 80 );
199 for (
const QgsFeature &feature : featuresWithoutGeom )
209 outputs.insert( u
"OUTPUT"_s, sinkId );
@ 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.
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...
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.
void setProgress(double progress)
Sets the current progress for the feedback object.
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.
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.