22using namespace Qt::StringLiterals;
30QVariantMap QgsCollectorAlgorithm::processCollection(
31 const QVariantMap ¶meters,
34 const std::function<
QgsGeometry(
const QVector<QgsGeometry> & )> &collector,
40 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, u
"INPUT"_s, context ) );
45 std::unique_ptr<QgsFeatureSink> sink(
53 outputs.insert( u
"OUTPUT"_s, dest );
55 const QStringList fields = parameterAsStrings( parameters, u
"FIELD"_s, context );
57 const long count = source->featureCount();
62 const double step = count > 0 ? 100.0 / count : 1;
65 if ( fields.isEmpty() )
68 bool firstFeature =
true;
70 QVector<QgsGeometry> geomQueue;
89 if ( maxQueueLength > 0 && geomQueue.length() > maxQueueLength )
92 const QgsGeometry tempOutputGeometry = collector( geomQueue );
94 geomQueue << tempOutputGeometry;
102 if ( geomQueue.isEmpty() )
110 else if ( !separateDisjoint )
112 outputFeature.
setGeometry( collector( geomQueue ) );
120 const QgsGeometry combinedGeometry = collector( geomQueue );
124 partGeom.convertToMultiType();
135 QList<int> fieldIndexes;
136 fieldIndexes.reserve( fields.size() );
137 for (
const QString &field : fields )
139 const int index = source->fields().lookupField( field );
141 fieldIndexes << index;
144 QHash<QVariant, QgsAttributes> attributeHash;
145 QHash<QVariant, QVector<QgsGeometry>> geometryHash;
154 QVariantList indexAttributes;
155 indexAttributes.reserve( fieldIndexes.size() );
156 for (
const int index : std::as_const( fieldIndexes ) )
161 if ( !attributeHash.contains( indexAttributes ) )
164 attributeHash.insert( indexAttributes, f.
attributes() );
169 geometryHash[indexAttributes].append( f.
geometry() );
173 const int numberFeatures = attributeHash.count();
174 QHash<QVariant, QgsAttributes>::const_iterator attrIt = attributeHash.constBegin();
175 for ( ; attrIt != attributeHash.constEnd(); ++attrIt )
184 auto geometryHashIt = geometryHash.find( attrIt.key() );
185 if ( geometryHashIt != geometryHash.end() )
187 QgsGeometry geom = collector( geometryHashIt.value() );
192 if ( !separateDisjoint )
205 partGeom.convertToMultiType();
222 feedback->
setProgress( current * 100.0 / numberFeatures );
238QString QgsDissolveAlgorithm::name()
const
240 return u
"dissolve"_s;
243QString QgsDissolveAlgorithm::displayName()
const
245 return QObject::tr(
"Dissolve" );
248QStringList QgsDissolveAlgorithm::tags()
const
250 return QObject::tr(
"dissolve,union,combine,collect" ).split(
',' );
253QString QgsDissolveAlgorithm::group()
const
255 return QObject::tr(
"Vector geometry" );
258QString QgsDissolveAlgorithm::groupId()
const
260 return u
"vectorgeometry"_s;
264void QgsDissolveAlgorithm::initAlgorithm(
const QVariantMap & )
269 auto disjointParam = std::make_unique<QgsProcessingParameterBoolean>( u
"SEPARATE_DISJOINT"_s, QObject::tr(
"Keep disjoint features separate" ),
false );
271 addParameter( disjointParam.release() );
276QString QgsDissolveAlgorithm::shortHelpString()
const
279 "This algorithm takes a vector layer and combines their features into new features. One or more attributes can "
280 "be specified to dissolve features belonging to the same class (having the same value for the specified attributes), alternatively "
281 "all features can be dissolved in a single one.\n\n"
282 "All output geometries will be converted to multi geometries. "
283 "In case the input is a polygon layer, common boundaries of adjacent polygons being dissolved will get erased.\n\n"
284 "If enabled, the optional \"Keep disjoint features separate\" setting will cause features and parts that do not overlap or touch to be exported "
285 "as separate features (instead of parts of a single multipart feature)."
289QString QgsDissolveAlgorithm::shortDescription()
const
291 return QObject::tr(
"Combines features of a vector layer into new features, optionally grouped by common attributes." );
299QgsDissolveAlgorithm *QgsDissolveAlgorithm::createInstance()
const
301 return new QgsDissolveAlgorithm();
306 const bool separateDisjoint = parameterAsBool( parameters, u
"SEPARATE_DISJOINT"_s, context );
308 return processCollection(
312 [&](
const QVector<QgsGeometry> &parts ) ->
QgsGeometry {
315 result = result.mergeLines();
318 if ( !result.lastError().isEmpty() && parts.count() > 2 )
323 feedback->
pushDebugInfo( QObject::tr(
"GEOS exception: taking the slower route ..." ) );
325 for (
const auto &p : parts )
329 result = result.mergeLines();
334 if ( !result.lastError().isEmpty() )
337 if ( result.isEmpty() )
352QString QgsCollectAlgorithm::name()
const
357QString QgsCollectAlgorithm::displayName()
const
359 return QObject::tr(
"Collect geometries" );
362QStringList QgsCollectAlgorithm::tags()
const
364 return QObject::tr(
"union,combine,collect,multipart,parts,single" ).split(
',' );
367QString QgsCollectAlgorithm::group()
const
369 return QObject::tr(
"Vector geometry" );
372QString QgsCollectAlgorithm::groupId()
const
374 return u
"vectorgeometry"_s;
383void QgsCollectAlgorithm::initAlgorithm(
const QVariantMap & )
391QString QgsCollectAlgorithm::shortHelpString()
const
394 "This algorithm takes a vector layer and collects its geometries into new multipart geometries. One or more attributes can "
395 "be specified to collect only geometries belonging to the same class (having the same value for the specified attributes), alternatively "
396 "all geometries can be collected."
400 "All output geometries will be converted to multi geometries, even those with just a single part. "
401 "This algorithm does not dissolve overlapping geometries - they will be collected together without modifying the shape of each geometry part."
404 + QObject::tr(
"See the 'Promote to multipart' or 'Aggregate' algorithms for alternative options." );
407QString QgsCollectAlgorithm::shortDescription()
const
409 return QObject::tr(
"Collects geometries of a vector layer into new multipart geometries, optionally grouped by common attributes." );
417QgsCollectAlgorithm *QgsCollectAlgorithm::createInstance()
const
419 return new QgsCollectAlgorithm();
@ RegeneratesPrimaryKey
Algorithm always drops any existing primary keys or FID values and regenerates them in outputs.
QFlags< ProcessingAlgorithmDocumentationFlag > ProcessingAlgorithmDocumentationFlags
Flags describing algorithm behavior for documentation purposes.
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
@ Advanced
Parameter is an advanced parameter which should be hidden from users by default.
QFlags< ProcessingFeatureSourceFlag > ProcessingFeatureSourceFlags
Flags which control how QgsProcessingFeatureSource fetches features.
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).
@ FastInsert
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
@ RegeneratePrimaryKey
This flag indicates, that a primary key field cannot be guaranteed to be unique and the sink should i...
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
void clearGeometry()
Removes any geometry associated with the feature.
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Q_INVOKABLE QVariant attribute(const QString &name) const
Lookup attribute value by attribute name.
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.
Encapsulates parameters under which a geometry operation is performed.
A geometry is the spatial representation of a feature.
QgsAbstractGeometry::const_part_iterator const_parts_begin() const
Returns STL-style const iterator pointing to the first part of the geometry.
static QgsGeometry collectGeometry(const QVector< QgsGeometry > &geometries)
Creates a new multipart geometry from a list of QgsGeometry objects.
bool isMultipart() const
Returns true if WKB of the geometry is of WKBMulti* type.
static QgsGeometry unaryUnion(const QVector< QgsGeometry > &geometries, const QgsGeometryParameters ¶meters=QgsGeometryParameters(), QgsFeedback *feedback=nullptr)
Compute the unary union on a list of geometries.
bool isEmpty() const
Returns true if the geometry is empty (eg a linestring with no vertices, or a collection with no geom...
QgsAbstractGeometry::const_part_iterator const_parts_end() const
Returns STL-style iterator pointing to the imaginary part after the last part of the geometry.
bool convertToMultiType()
Converts single type geometry into multitype geometry e.g.
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.
void featureSinkFinalized(const QString &output)
Reports that a feature sink has been finalized.
virtual void pushDebugInfo(const QString &info)
Pushes an informational message containing debugging helpers from the algorithm.
virtual void reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.
A feature sink output for processing algorithms.
An input feature source (such as vector layers) parameter for processing algorithms.
A vector layer or feature source field parameter for processing algorithms.
static Qgis::GeometryType geometryType(Qgis::WkbType type)
Returns the geometry type for a WKB type, e.g., both MultiPolygon and CurvePolygon would have a Polyg...
static Qgis::WkbType multiType(Qgis::WkbType type)
Returns the multi type for a WKB type.