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 = std::make_unique < QgsProcessingParameterEnum >( QStringLiteral(
"FILE_TYPE" ), QObject::tr(
"Output file type" ), options,
false, 0,
true );
72 addParameter( fileTypeParam.release() );
80 std::unique_ptr< QgsFeatureSource > source( parameterAsSource( parameters, QStringLiteral(
"INPUT" ), context ) );
84 const QString fieldName = parameterAsString( parameters, QStringLiteral(
"FIELD" ), context );
85 const QString outputDir = parameterAsString( parameters, QStringLiteral(
"OUTPUT" ), context );
87 if ( parameters.value( QStringLiteral(
"FILE_TYPE" ) ).isValid() )
89 const int idx = parameterAsEnum( parameters, QStringLiteral(
"FILE_TYPE" ), context );
96 outputFormat = QStringLiteral(
"gpkg" );
99 if ( !QDir().mkpath( outputDir ) )
102 const QgsFields fields = source->fields();
105 const int fieldIndex = fields.
lookupField( fieldName );
106 const QSet< QVariant > uniqueValues = source->uniqueValues( fieldIndex );
107 const QString baseName = outputDir + QDir::separator() + fieldName;
110 const 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 )
123 if ( ( *it ).isNull() )
125 fileName = QStringLiteral(
"%1_NULL.%2" ).arg( baseName ).arg( outputFormat );
127 else if ( ( *it ).toString().isEmpty() )
129 fileName = QStringLiteral(
"%1_EMPTY.%2" ).arg( baseName ).arg( outputFormat );
133 fileName = QStringLiteral(
"%1_%2.%3" ).arg( baseName ).arg( ( *it ).toString() ).arg( outputFormat );
135 feedback->
pushInfo( QObject::tr(
"Creating layer: %1" ).arg( fileName ) );
151 feedback->
pushInfo( QObject::tr(
"Added %n feature(s) to layer",
nullptr, count ) );
152 outputLayers << fileName;
159 outputs.insert( QStringLiteral(
"OUTPUT" ), outputDir );
160 outputs.insert( QStringLiteral(
"OUTPUT_LAYERS" ), outputLayers );