26using namespace Qt::StringLiterals;
30QString QgsSplitVectorLayerAlgorithm::name()
const
32 return u
"splitvectorlayer"_s;
35QString QgsSplitVectorLayerAlgorithm::displayName()
const
37 return QObject::tr(
"Split vector layer" );
40QStringList QgsSplitVectorLayerAlgorithm::tags()
const
42 return QObject::tr(
"vector,split,field,unique" ).split(
',' );
45QString QgsSplitVectorLayerAlgorithm::group()
const
47 return QObject::tr(
"Vector general" );
50QString QgsSplitVectorLayerAlgorithm::groupId()
const
52 return u
"vectorgeneral"_s;
55QString QgsSplitVectorLayerAlgorithm::shortHelpString()
const
57 return QObject::tr(
"This algorithm splits input vector layer into multiple layers by specified unique ID field." )
59 + QObject::tr(
"Each of the layers created in the output folder contains all features from "
60 "the input layer with the same value for the specified attribute. The number "
61 "of files generated is equal to the number of different values found for the "
62 "specified attribute." );
65QString QgsSplitVectorLayerAlgorithm::shortDescription()
const
67 return QObject::tr(
"Splits a vector layer into multiple layers, each containing "
68 "all the features with the same value for a specified attribute." );
71QgsSplitVectorLayerAlgorithm *QgsSplitVectorLayerAlgorithm::createInstance()
const
73 return new QgsSplitVectorLayerAlgorithm();
76void QgsSplitVectorLayerAlgorithm::initAlgorithm(
const QVariantMap & )
80 auto prefixFieldParam = std::make_unique<QgsProcessingParameterBoolean>( u
"PREFIX_FIELD"_s, QObject::tr(
"Add field prefix to file names" ),
true );
81 addParameter( prefixFieldParam.release() );
84 auto fileTypeParam = std::make_unique<QgsProcessingParameterEnum>( u
"FILE_TYPE"_s, QObject::tr(
"Output file type" ), options,
false, 0,
true );
86 addParameter( fileTypeParam.release() );
94 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, u
"INPUT"_s, context ) );
98 const QString fieldName = parameterAsString( parameters, u
"FIELD"_s, context );
99 const QString outputDir = parameterAsString( parameters, u
"OUTPUT"_s, context );
100 QString outputFormat;
101 if ( parameters.value( u
"FILE_TYPE"_s ).isValid() )
103 const int idx = parameterAsEnum( parameters, u
"FILE_TYPE"_s, context );
110 outputFormat = u
"gpkg"_s;
113 if ( !QDir().mkpath( outputDir ) )
116 const QgsFields fields = source->fields();
119 const int fieldIndex = fields.
lookupField( fieldName );
120 const QSet<QVariant> uniqueValues = source->uniqueValues( fieldIndex );
121 QString baseName = outputDir + QDir::separator();
123 if ( parameterAsBool( parameters, u
"PREFIX_FIELD"_s, context ) )
125 baseName.append( fieldName +
"_" );
129 const double step = uniqueValues.size() > 0 ? 100.0 / uniqueValues.size() : 1;
133 QStringList outputLayers;
134 std::unique_ptr<QgsFeatureSink> sink;
136 for (
auto it = uniqueValues.constBegin(); it != uniqueValues.constEnd(); ++it )
144 fileName = u
"%1NULL.%2"_s.arg( baseName ).arg( outputFormat );
146 else if ( ( *it ).toString().isEmpty() )
148 fileName = u
"%1EMPTY.%2"_s.arg( baseName ).arg( outputFormat );
154 feedback->
pushInfo( QObject::tr(
"Creating layer: %1" ).arg( fileName ) );
171 feedback->
pushInfo( QObject::tr(
"Added %n feature(s) to layer",
nullptr, count ) );
172 outputLayers << fileName;
179 outputs.insert( u
"OUTPUT"_s, outputDir );
180 outputs.insert( u
"OUTPUT_LAYERS"_s, outputLayers );
@ Vector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
WkbType
The WKB type describes the number of dimensions a geometry has.
@ Advanced
Parameter is an advanced parameter which should be hidden from users by default.
Represents a coordinate reference system (CRS).
static QString createFieldEqualityExpression(const QString &fieldName, const QVariant &value, QMetaType::Type fieldType=QMetaType::Type::UnknownType)
Create an expression allowing to evaluate if a field is equal to a value.
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.
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...
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
bool isCanceled() const
Tells whether the operation has been canceled already.
void setProgress(double progress)
Sets the current progress for the feedback object.
Container of fields for a vector layer.
Q_INVOKABLE int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
static QString stringToSafeFilename(const QString &string)
Converts a string to a safe filename, replacing characters which are not safe for filenames with an '...
Contains information about the context in which a processing algorithm is executed.
QString preferredVectorFormat() const
Returns the preferred vector format to use for vector outputs.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
virtual void pushInfo(const QString &info)
Pushes a general informational message from the algorithm.
A multi-layer output for processing algorithms which create map layers, when the number and nature of...
An input feature source (such as vector layers) parameter for processing algorithms.
A vector layer or feature source field parameter for processing algorithms.
A folder destination parameter, for specifying the destination path for a folder created by the algor...
static QgsFeatureSink * createFeatureSink(QString &destination, QgsProcessingContext &context, const QgsFields &fields, Qgis::WkbType geometryType, const QgsCoordinateReferenceSystem &crs, const QVariantMap &createOptions=QVariantMap(), const QStringList &datasourceOptions=QStringList(), const QStringList &layerOptions=QStringList(), QgsFeatureSink::SinkFlags sinkFlags=QgsFeatureSink::SinkFlags(), QgsRemappingSinkDefinition *remappingDefinition=nullptr)
Creates a feature sink ready for adding features.
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.
static QStringList supportedFormatExtensions(VectorFormatOptions options=SortRecommended)
Returns a list of file extensions for supported formats, e.g "shp", "gpkg".