27 const std::function<
QgsGeometry(
const QVector< QgsGeometry >& )> &collector,
int maxQueueLength )
29 std::unique_ptr< QgsFeatureSource > source( parameterAsSource( parameters, QStringLiteral(
"INPUT" ), context ) );
34 std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral(
"OUTPUT" ), context, dest, source->fields(),
QgsWkbTypes::multiType( source->wkbType() ), source->sourceCrs() ) );
39 QStringList fields = parameterAsFields( parameters, QStringLiteral(
"FIELD" ), context );
41 long count = source->featureCount();
46 double step = count > 0 ? 100.0 / count : 1;
49 if ( fields.isEmpty() )
52 bool firstFeature =
true;
54 QVector< QgsGeometry > geomQueue;
73 if ( maxQueueLength > 0 && geomQueue.length() > maxQueueLength )
76 QgsGeometry tempOutputGeometry = collector( geomQueue );
78 geomQueue << tempOutputGeometry;
86 outputFeature.
setGeometry( collector( geomQueue ) );
91 QList< int > fieldIndexes;
92 Q_FOREACH (
const QString &field, fields )
94 int index = source->fields().lookupField( field );
96 fieldIndexes << index;
99 QHash< QVariant, QgsAttributes > attributeHash;
100 QHash< QVariant, QVector< QgsGeometry > > geometryHash;
109 QVariantList indexAttributes;
110 Q_FOREACH (
int index, fieldIndexes )
115 if ( !attributeHash.contains( indexAttributes ) )
118 attributeHash.insert( indexAttributes, f.
attributes() );
123 geometryHash[ indexAttributes ].append( f.
geometry() );
127 int numberFeatures = attributeHash.count();
128 QHash< QVariant, QgsAttributes >::const_iterator attrIt = attributeHash.constBegin();
129 for ( ; attrIt != attributeHash.constEnd(); ++attrIt )
137 if ( geometryHash.contains( attrIt.key() ) )
139 QgsGeometry geom = collector( geometryHash.value( attrIt.key() ) );
149 feedback->
setProgress( current * 100.0 / numberFeatures );
155 outputs.insert( QStringLiteral(
"OUTPUT" ), dest );
164 QString QgsDissolveAlgorithm::name()
const 166 return QStringLiteral(
"dissolve" );
169 QString QgsDissolveAlgorithm::displayName()
const 171 return QObject::tr(
"Dissolve" );
174 QStringList QgsDissolveAlgorithm::tags()
const 176 return QObject::tr(
"dissolve,union,combine,collect" ).split(
',' );
179 QString QgsDissolveAlgorithm::group()
const 181 return QObject::tr(
"Vector geometry" );
184 QString QgsDissolveAlgorithm::groupId()
const 186 return QStringLiteral(
"vectorgeometry" );
190 void QgsDissolveAlgorithm::initAlgorithm(
const QVariantMap & )
199 QString QgsDissolveAlgorithm::shortHelpString()
const 201 return QObject::tr(
"This algorithm takes a vector layer and combines their features into new features. One or more attributes can " 202 "be specified to dissolve features belonging to the same class (having the same value for the specified attributes), alternatively " 203 "all features can be dissolved in a single one.\n\n" 204 "All output geometries will be converted to multi geometries. " 205 "In case the input is a polygon layer, common boundaries of adjacent polygons being dissolved will get erased." );
208 QgsDissolveAlgorithm *QgsDissolveAlgorithm::createInstance()
const 210 return new QgsDissolveAlgorithm();
216 return processCollection( parameters, context, feedback, [ & ](
const QVector< QgsGeometry > &parts )->
QgsGeometry 221 if ( ! result.lastError().isEmpty() && parts.count() > 2 )
226 feedback->
pushDebugInfo( QStringLiteral(
"GEOS exception: taking the slower route ..." ) );
228 for (
const auto &p : parts )
235 if ( ! result.lastError().isEmpty() )
238 if ( result.isEmpty() )
249 QString QgsCollectAlgorithm::name()
const 251 return QStringLiteral(
"collect" );
254 QString QgsCollectAlgorithm::displayName()
const 256 return QObject::tr(
"Collect geometries" );
259 QStringList QgsCollectAlgorithm::tags()
const 261 return QObject::tr(
"union,combine,collect,multipart,parts,single" ).split(
',' );
264 QString QgsCollectAlgorithm::group()
const 266 return QObject::tr(
"Vector geometry" );
269 QString QgsCollectAlgorithm::groupId()
const 271 return QStringLiteral(
"vectorgeometry" );
276 return processCollection( parameters, context, feedback, [](
const QVector< QgsGeometry > &parts )->
QgsGeometry 283 void QgsCollectAlgorithm::initAlgorithm(
const QVariantMap & )
292 QString QgsCollectAlgorithm::shortHelpString()
const 294 return QObject::tr(
"This algorithm takes a vector layer and collects its geometries into new multipart geometries. One or more attributes can " 295 "be specified to collect only geometries belonging to the same class (having the same value for the specified attributes), alternatively " 296 "all geometries can be collected." ) +
297 QStringLiteral(
"\n\n" ) +
298 QObject::tr(
"All output geometries will be converted to multi geometries, even those with just a single part. " 299 "This algorithm does not dissolve overlapping geometries - they will be collected together without modifying the shape of each geometry part." ) +
300 QStringLiteral(
"\n\n" ) +
301 QObject::tr(
"See the 'Promote to multipart' or 'Aggregate' algorithms for alternative options." );
304 QgsCollectAlgorithm *QgsCollectAlgorithm::createInstance()
const 306 return new QgsCollectAlgorithm();
Wrapper for iterator of features from vector data provider or vector layer.
bool isCanceled() const
Tells whether the operation has been canceled already.
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
static Type multiType(Type type)
Returns the multi type for a WKB type.
Base class for providing feedback from a processing algorithm.
bool isNull() const
Returns true if the geometry is null (ie, contains no underlying geometry accessible via geometry() )...
A vector layer or feature source field parameter for processing algorithms.
void setProgress(double progress)
Sets the current progress for the feedback object.
A geometry is the spatial representation of a feature.
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
A feature sink output for processing algorithms.
Custom exception class for processing related exceptions.
virtual void pushDebugInfo(const QString &info)
Pushes an informational message containing debugging helpers from the algorithm.
bool convertToMultiType()
Converts single type geometry into multitype geometry e.g.
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
An input feature source (such as vector layers) parameter for processing algorithms.
bool isMultipart() const
Returns true if WKB of the geometry is of WKBMulti* type.
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
bool hasGeometry() const
Returns true if the feature has an associated geometry.
bool nextFeature(QgsFeature &f)
Contains information about the context in which a processing algorithm is executed.
static QgsGeometry unaryUnion(const QVector< QgsGeometry > &geometries)
Compute the unary union on a list of geometries.
static QgsGeometry collectGeometry(const QVector< QgsGeometry > &geometries)
Creates a new multipart geometry from a list of QgsGeometry objects.
virtual void reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.