23 QString QgsSplitVectorLayerAlgorithm::name()
const
25 return QStringLiteral(
"splitvectorlayer" );
28 QString QgsSplitVectorLayerAlgorithm::displayName()
const
30 return QObject::tr(
"Split vector layer" );
33 QStringList QgsSplitVectorLayerAlgorithm::tags()
const
35 return QObject::tr(
"vector,split,field,unique" ).split(
',' );
38 QString QgsSplitVectorLayerAlgorithm::group()
const
40 return QObject::tr(
"Vector general" );
43 QString QgsSplitVectorLayerAlgorithm::groupId()
const
45 return QStringLiteral(
"vectorgeneral" );
48 QString QgsSplitVectorLayerAlgorithm::shortHelpString()
const
50 return QObject::tr(
"Splits input vector layer into multiple layers by specified unique ID field." )
51 + QStringLiteral(
"\n\n" )
52 + QObject::tr(
"Each of the layers created in the output folder contains all features from "
53 "the input layer with the same value for the specified attribute. The number "
54 "of files generated is equal to the number of different values found for the "
55 "specified attribute." );
58 QgsSplitVectorLayerAlgorithm *QgsSplitVectorLayerAlgorithm::createInstance()
const
60 return new QgsSplitVectorLayerAlgorithm();
63 void QgsSplitVectorLayerAlgorithm::initAlgorithm(
const QVariantMap & )
67 QVariant(), QStringLiteral(
"INPUT" ) ) );
70 auto fileTypeParam = qgis::make_unique < QgsProcessingParameterEnum >( QStringLiteral(
"FILE_TYPE" ), QObject::tr(
"Output file type" ), options,
false, QVariantList() << 0,
true );
72 addParameter( fileTypeParam.release() );
80 std::unique_ptr< QgsFeatureSource > source( parameterAsSource( parameters, QStringLiteral(
"INPUT" ), context ) );
84 QString fieldName = parameterAsString( parameters, QStringLiteral(
"FIELD" ), context );
85 QString outputDir = parameterAsString( parameters, QStringLiteral(
"OUTPUT" ), context );
87 if ( parameters.value( QStringLiteral(
"FILE_TYPE" ) ).isValid() )
89 int idx = parameterAsEnum( parameters, QStringLiteral(
"FILE_TYPE" ), context );
96 outputFormat = QStringLiteral(
"gpkg" );
99 if ( !QDir().mkpath( outputDir ) )
106 QSet< QVariant > uniqueValues = source->uniqueValues( fieldIndex );
107 QString baseName = outputDir + QDir::separator() + fieldName;
110 double step = uniqueValues.size() > 0 ? 100.0 / uniqueValues.size() : 1;
114 QStringList outputLayers;
115 std::unique_ptr< QgsFeatureSink > sink;
117 for (
auto it = uniqueValues.constBegin(); it != uniqueValues.constEnd(); ++it )
122 QString fileName = QStringLiteral(
"%1_%2.%3" ).arg( baseName ).arg( current ).arg( outputFormat );
123 feedback->
pushInfo( QObject::tr(
"Creating layer: %1" ).arg( fileName ) );
137 feedback->
pushInfo( QObject::tr(
"Added %1 features to layer" ).arg( count ) );
138 outputLayers << fileName;
145 outputs.insert( QStringLiteral(
"OUTPUT" ), outputDir );
146 outputs.insert( QStringLiteral(
"OUTPUT_LAYERS" ), outputLayers );