27using namespace Qt::StringLiterals;
31QString QgsCoverageSimplifyAlgorithm::name()
const
33 return u
"coveragesimplify"_s;
36QString QgsCoverageSimplifyAlgorithm::displayName()
const
38 return QObject::tr(
"Simplify coverage" );
41QStringList QgsCoverageSimplifyAlgorithm::tags()
const
43 return QObject::tr(
"topological,boundary" ).split(
',' );
46QString QgsCoverageSimplifyAlgorithm::group()
const
48 return QObject::tr(
"Vector coverage" );
51QString QgsCoverageSimplifyAlgorithm::groupId()
const
53 return u
"vectorcoverage"_s;
56void QgsCoverageSimplifyAlgorithm::initAlgorithm(
const QVariantMap & )
60 auto boundaryParameter = std::make_unique<QgsProcessingParameterBoolean>( u
"PRESERVE_BOUNDARY"_s, QObject::tr(
"Preserve boundary" ),
false );
61 boundaryParameter->setHelp( QObject::tr(
"When enabled the outside edges of the coverage will be preserved without simplification." ) );
62 addParameter( boundaryParameter.release() );
67QString QgsCoverageSimplifyAlgorithm::shortDescription()
const
69 return QObject::tr(
"Simplifies a coverage of polygon features while retaining valid coverage." );
72QString QgsCoverageSimplifyAlgorithm::shortHelpString()
const
75 "This algorithm operates on a coverage (represented as a set of polygon features "
76 "with exactly matching edge geometry) to apply a Visvalingam–Whyatt "
77 "simplification to the edges, reducing complexity in proportion with "
78 "the provided tolerance, while retaining a valid coverage (i.e. no edges "
79 "will cross or touch after the simplification).\n\n"
80 "Geometries will never be removed, but they may be simplified down to just "
81 "a triangle. Also, some geometries (such as polygons which have too "
82 "few non-repeated points) will be returned unchanged.\n\n"
83 "If the input dataset is not a valid coverage due to overlaps, "
84 "it will still be simplified, but invalid topology such as crossing "
85 "edges will still be invalid."
89QgsCoverageSimplifyAlgorithm *QgsCoverageSimplifyAlgorithm::createInstance()
const
91 return new QgsCoverageSimplifyAlgorithm();
96 QGS_MARK_ALGORITHM_SOURCE
98 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, u
"INPUT"_s, context ) );
102 const bool preserveBoundary = parameterAsBoolean( parameters, u
"PRESERVE_BOUNDARY"_s, context );
103 const double tolerance = parameterAsDouble( parameters, u
"TOLERANCE"_s, context );
106 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, u
"OUTPUT"_s, context, sinkId, source->fields(), source->wkbType(), source->sourceCrs() ) );
111 QVector<QgsFeature> featuresWithGeom;
112 QVector<QgsFeature> featuresWithoutGeom;
115 const long count = source->featureCount();
118 featuresWithGeom.reserve( count );
122 const double step = count > 0 ? 100.0 / count : 1;
125 feedback->
pushInfo( QObject::tr(
"Collecting features" ) );
138 featuresWithGeom.append( inFeature );
143 featuresWithoutGeom.append( inFeature );
153 switch ( source->invalidGeometryCheck() )
169 feedback->
pushInfo( QObject::tr(
"Simplifying coverage" ) );
171 std::unique_ptr<QgsAbstractGeometry> simplified;
174 simplified =
geos.simplifyCoverageVW( tolerance, preserveBoundary, &error );
183 if ( !error.isEmpty() )
191 feedback->
pushInfo( QObject::tr(
"Storing features" ) );
192 long long featureIndex = 0;
193 for (
auto partsIt = simplified->const_parts_begin(); partsIt != simplified->const_parts_end(); ++partsIt )
195 QgsFeature outFeature = featuresWithGeom.value( featureIndex );
202 feedback->
setProgress( featureIndex * step * 0.2 + 80 );
205 for (
const QgsFeature &feature : featuresWithoutGeom )
218 outputs.insert( u
"OUTPUT"_s, sinkId );
222bool QgsCoverageSimplifyAlgorithm::supportInPlaceEdit(
const QgsMapLayer *layer )
const
224 const QgsVectorLayer *vlayer = qobject_cast<const QgsVectorLayer *>( layer );
@ VectorPolygon
Vector polygon layers.
QFlags< ProcessingAlgorithmFlag > ProcessingAlgorithmFlags
Flags indicating how and when an algorithm operates and should be exposed to users.
@ 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...
@ SupportsInPlaceEdits
Algorithm supports in-place editing.
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.
Base class for all map layer types.
Custom exception class which is raised when an operation is not supported.
virtual Qgis::ProcessingAlgorithmFlags flags() const
Returns the flags indicating how and when the algorithm operates and should be exposed to users.
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.
void featureAddedToSink(const QString &output)
Reports that a feature was added to the the sink associated with the specified algorithm output.
virtual void pushInfo(const QString &info)
Pushes a general informational message from the algorithm.
void featureSinkFinalized(const QString &output)
Reports that a feature sink has been finalized.
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.
Represents a vector layer which manages a vector based dataset.
Q_INVOKABLE Qgis::GeometryType geometryType() const
Returns point, line or polygon.
Contains geos related utilities and functions.