29 std::unique_ptr< QgsProcessingFeatureSource > source( parameterAsSource( parameters, QStringLiteral(
"INPUT" ), context ) );
40 outputs.insert( QStringLiteral(
"OUTPUT" ), dest );
42 const QStringList fields = parameterAsStrings( parameters, QStringLiteral(
"FIELD" ), context );
44 const long count = source->featureCount();
52 const double step = count > 0 ? 100.0 / count : 1;
55 if ( fields.isEmpty() )
58 bool firstFeature =
true;
60 QVector< QgsGeometry > geomQueue;
79 if ( maxQueueLength > 0 && geomQueue.length() > maxQueueLength )
82 const QgsGeometry tempOutputGeometry = collector( geomQueue );
84 geomQueue << tempOutputGeometry;
92 if ( geomQueue.isEmpty() )
98 else if ( !separateDisjoint )
100 outputFeature.
setGeometry( collector( geomQueue ) );
106 const QgsGeometry combinedGeometry = collector( geomQueue );
110 partGeom.convertToMultiType();
119 QList< int > fieldIndexes;
120 fieldIndexes.reserve( fields.size() );
121 for (
const QString &field : fields )
123 const int index = source->fields().lookupField( field );
125 fieldIndexes << index;
128 QHash< QVariant, QgsAttributes > attributeHash;
129 QHash< QVariant, QVector< QgsGeometry > > geometryHash;
138 QVariantList indexAttributes;
139 indexAttributes.reserve( fieldIndexes.size() );
140 for (
const int index : std::as_const( fieldIndexes ) )
145 if ( !attributeHash.contains( indexAttributes ) )
148 attributeHash.insert( indexAttributes, f.
attributes() );
153 geometryHash[ indexAttributes ].append( f.
geometry() );
157 const int numberFeatures = attributeHash.count();
158 QHash< QVariant, QgsAttributes >::const_iterator attrIt = attributeHash.constBegin();
159 for ( ; attrIt != attributeHash.constEnd(); ++attrIt )
168 auto geometryHashIt = geometryHash.find( attrIt.key() );
169 if ( geometryHashIt != geometryHash.end() )
171 QgsGeometry geom = collector( geometryHashIt.value() );
176 if ( !separateDisjoint )
187 partGeom.convertToMultiType();
200 feedback->
setProgress( current * 100.0 / numberFeatures );
213QString QgsDissolveAlgorithm::name()
const
215 return QStringLiteral(
"dissolve" );
218QString QgsDissolveAlgorithm::displayName()
const
220 return QObject::tr(
"Dissolve" );
223QStringList QgsDissolveAlgorithm::tags()
const
225 return QObject::tr(
"dissolve,union,combine,collect" ).split(
',' );
228QString QgsDissolveAlgorithm::group()
const
230 return QObject::tr(
"Vector geometry" );
233QString QgsDissolveAlgorithm::groupId()
const
235 return QStringLiteral(
"vectorgeometry" );
239void QgsDissolveAlgorithm::initAlgorithm(
const QVariantMap & )
245 std::unique_ptr< QgsProcessingParameterBoolean > disjointParam = std::make_unique< QgsProcessingParameterBoolean >( QStringLiteral(
"SEPARATE_DISJOINT" ),
246 QObject::tr(
"Keep disjoint features separate" ),
false );
248 addParameter( disjointParam.release() );
253QString QgsDissolveAlgorithm::shortHelpString()
const
255 return QObject::tr(
"This algorithm takes a vector layer and combines their features into new features. One or more attributes can "
256 "be specified to dissolve features belonging to the same class (having the same value for the specified attributes), alternatively "
257 "all features can be dissolved in a single one.\n\n"
258 "All output geometries will be converted to multi geometries. "
259 "In case the input is a polygon layer, common boundaries of adjacent polygons being dissolved will get erased.\n\n"
260 "If enabled, the optional \"Keep disjoint features separate\" setting will cause features and parts that do not overlap or touch to be exported "
261 "as separate features (instead of parts of a single multipart feature)." );
269QgsDissolveAlgorithm *QgsDissolveAlgorithm::createInstance()
const
271 return new QgsDissolveAlgorithm();
276 const bool separateDisjoint = parameterAsBool( parameters, QStringLiteral(
"SEPARATE_DISJOINT" ), context );
278 return processCollection( parameters, context, feedback, [ & ](
const QVector< QgsGeometry > &parts )->
QgsGeometry
282 result = result.mergeLines();
285 if ( ! result.lastError().isEmpty() && parts.count() > 2 )
290 feedback->
pushDebugInfo( QObject::tr(
"GEOS exception: taking the slower route ..." ) );
292 for (
const auto &p : parts )
296 result = result.mergeLines();
301 if ( ! result.lastError().isEmpty() )
304 if ( result.isEmpty() )
315QString QgsCollectAlgorithm::name()
const
317 return QStringLiteral(
"collect" );
320QString QgsCollectAlgorithm::displayName()
const
322 return QObject::tr(
"Collect geometries" );
325QStringList QgsCollectAlgorithm::tags()
const
327 return QObject::tr(
"union,combine,collect,multipart,parts,single" ).split(
',' );
330QString QgsCollectAlgorithm::group()
const
332 return QObject::tr(
"Vector geometry" );
335QString QgsCollectAlgorithm::groupId()
const
337 return QStringLiteral(
"vectorgeometry" );
342 return processCollection( parameters, context, feedback, [](
const QVector< QgsGeometry > &parts )->
QgsGeometry
349void QgsCollectAlgorithm::initAlgorithm(
const QVariantMap & )
358QString QgsCollectAlgorithm::shortHelpString()
const
360 return QObject::tr(
"This algorithm takes a vector layer and collects its geometries into new multipart geometries. One or more attributes can "
361 "be specified to collect only geometries belonging to the same class (having the same value for the specified attributes), alternatively "
362 "all geometries can be collected." ) +
363 QStringLiteral(
"\n\n" ) +
364 QObject::tr(
"All output geometries will be converted to multi geometries, even those with just a single part. "
365 "This algorithm does not dissolve overlapping geometries - they will be collected together without modifying the shape of each geometry part." ) +
366 QStringLiteral(
"\n\n" ) +
367 QObject::tr(
"See the 'Promote to multipart' or 'Aggregate' algorithms for alternative options." );
375QgsCollectAlgorithm *QgsCollectAlgorithm::createInstance()
const
377 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.
This class 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.
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.
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.
static QgsGeometry unaryUnion(const QVector< QgsGeometry > &geometries, const QgsGeometryParameters ¶meters=QgsGeometryParameters())
Compute the unary union on a list of geometries.
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 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.