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, QVariantList() << 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 %1 features to layer" ).arg( count ) );
152 outputLayers << fileName;
159 outputs.insert( QStringLiteral(
"OUTPUT" ), outputDir );
160 outputs.insert( QStringLiteral(
"OUTPUT_LAYERS" ), outputLayers );
This class represents a coordinate reference system (CRS).
static QString createFieldEqualityExpression(const QString &fieldName, const QVariant &value)
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)
This class 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 SIP_HOLDGIL
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.
int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
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...
@ FlagAdvanced
Parameter is an advanced parameter which should be hidden from users by default.
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, QgsWkbTypes::Type 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 QStringList supportedFormatExtensions(VectorFormatOptions options=SortRecommended)
Returns a list of file extensions for supported formats, e.g "shp", "gpkg".
Type
The WKB type describes the number of dimensions a geometry has.
const QgsCoordinateReferenceSystem & crs