@@ -29,6 +29,16 @@ QString QgsMultipartToSinglepartAlgorithm::displayName() const
2929 return QObject::tr ( " Multipart to singleparts" );
3030}
3131
32+ QString QgsMultipartToSinglepartAlgorithm::outputName () const
33+ {
34+ return QObject::tr ( " Single parts" );
35+ }
36+
37+ QgsWkbTypes::Type QgsMultipartToSinglepartAlgorithm::outputWkbType ( QgsWkbTypes::Type inputWkbType ) const
38+ {
39+ return QgsWkbTypes::singleType ( inputWkbType );
40+ }
41+
3242QStringList QgsMultipartToSinglepartAlgorithm::tags () const
3343{
3444 return QObject::tr ( " multi,single,multiple,split,dump" ).split ( ' ,' );
@@ -44,13 +54,6 @@ QString QgsMultipartToSinglepartAlgorithm::groupId() const
4454 return QStringLiteral ( " vectorgeometry" );
4555}
4656
47- void QgsMultipartToSinglepartAlgorithm::initAlgorithm ( const QVariantMap & )
48- {
49- addParameter ( new QgsProcessingParameterFeatureSource ( QStringLiteral ( " INPUT" ), QObject::tr ( " Input layer" ) ) );
50-
51- addParameter ( new QgsProcessingParameterFeatureSink ( QStringLiteral ( " OUTPUT" ), QObject::tr ( " Single parts" ) ) );
52- }
53-
5457QString QgsMultipartToSinglepartAlgorithm::shortHelpString () const
5558{
5659 return QObject::tr ( " This algorithm takes a vector layer with multipart geometries and generates a new one in which all geometries contain "
@@ -63,63 +66,28 @@ QgsMultipartToSinglepartAlgorithm *QgsMultipartToSinglepartAlgorithm::createInst
6366 return new QgsMultipartToSinglepartAlgorithm ();
6467}
6568
66- QVariantMap QgsMultipartToSinglepartAlgorithm::processAlgorithm ( const QVariantMap ¶meters , QgsProcessingContext &context , QgsProcessingFeedback *feedback )
69+ QgsFeatureList QgsMultipartToSinglepartAlgorithm::processFeature ( const QgsFeature &feature , QgsProcessingContext &, QgsProcessingFeedback * )
6770{
68- std::unique_ptr< QgsFeatureSource > source ( parameterAsSource ( parameters, QStringLiteral ( " INPUT" ), context ) );
69- if ( !source )
70- return QVariantMap ();
71-
72- QgsWkbTypes::Type sinkType = QgsWkbTypes::singleType ( source->wkbType () );
73-
74- QString dest;
75- std::unique_ptr< QgsFeatureSink > sink ( parameterAsSink ( parameters, QStringLiteral ( " OUTPUT" ), context, dest, source->fields (),
76- sinkType, source->sourceCrs () ) );
77- if ( !sink )
78- return QVariantMap ();
71+ if ( !feature.hasGeometry () )
72+ return QgsFeatureList () << feature;
7973
80- long count = source->featureCount ();
81-
82- QgsFeature f;
83- QgsFeatureIterator it = source->getFeatures ();
84-
85- double step = count > 0 ? 100.0 / count : 1 ;
86- int current = 0 ;
87- while ( it.nextFeature ( f ) )
74+ QgsGeometry inputGeometry = feature.geometry ();
75+ QgsFeatureList outputs;
76+ if ( inputGeometry.isMultipart () )
8877 {
89- if ( feedback->isCanceled () )
90- {
91- break ;
92- }
93-
94- QgsFeature out = f;
95- if ( out.hasGeometry () )
78+ const QVector<QgsGeometry> parts = inputGeometry.asGeometryCollection ();
79+ for ( const QgsGeometry &g : parts )
9680 {
97- QgsGeometry inputGeometry = f.geometry ();
98- if ( inputGeometry.isMultipart () )
99- {
100- Q_FOREACH ( const QgsGeometry &g, inputGeometry.asGeometryCollection () )
101- {
102- out.setGeometry ( g );
103- sink->addFeature ( out, QgsFeatureSink::FastInsert );
104- }
105- }
106- else
107- {
108- sink->addFeature ( out, QgsFeatureSink::FastInsert );
109- }
81+ QgsFeature out;
82+ out.setAttributes ( feature.attributes () );
83+ out.setGeometry ( g );
84+ outputs.append ( out );
11085 }
111- else
112- {
113- // feature with null geometry
114- sink->addFeature ( out, QgsFeatureSink::FastInsert );
115- }
116-
117- feedback->setProgress ( current * step );
118- current++;
11986 }
120-
121- QVariantMap outputs;
122- outputs.insert ( QStringLiteral ( " OUTPUT" ), dest );
87+ else
88+ {
89+ outputs.append ( feature );
90+ }
12391 return outputs;
12492}
12593
0 commit comments