|
| 1 | +/*************************************************************************** |
| 2 | + qgsalgorithmsplitvectorlayer.cpp |
| 3 | + --------------------- |
| 4 | + begin : May 2020 |
| 5 | + copyright : (C) 2020 by Alexander Bruy |
| 6 | + email : alexander dot bruy at gmail dot com |
| 7 | + ***************************************************************************/ |
| 8 | + |
| 9 | +/*************************************************************************** |
| 10 | + * * |
| 11 | + * This program is free software; you can redistribute it and/or modify * |
| 12 | + * it under the terms of the GNU General Public License as published by * |
| 13 | + * the Free Software Foundation; either version 2 of the License, or * |
| 14 | + * (at your option) any later version. * |
| 15 | + * * |
| 16 | + ***************************************************************************/ |
| 17 | + |
| 18 | +#include "qgsalgorithmsplitvectorlayer.h" |
| 19 | +#include "qgsvectorfilewriter.h" |
| 20 | + |
| 21 | +///@cond PRIVATE |
| 22 | + |
| 23 | +QString QgsSplitVectorLayerAlgorithm::name() const |
| 24 | +{ |
| 25 | + return QStringLiteral( "splitvectorlayer" ); |
| 26 | +} |
| 27 | + |
| 28 | +QString QgsSplitVectorLayerAlgorithm::displayName() const |
| 29 | +{ |
| 30 | + return QObject::tr( "Split vector layer" ); |
| 31 | +} |
| 32 | + |
| 33 | +QStringList QgsSplitVectorLayerAlgorithm::tags() const |
| 34 | +{ |
| 35 | + return QObject::tr( "vector,split,field,unique" ).split( ',' ); |
| 36 | +} |
| 37 | + |
| 38 | +QString QgsSplitVectorLayerAlgorithm::group() const |
| 39 | +{ |
| 40 | + return QObject::tr( "Vector general" ); |
| 41 | +} |
| 42 | + |
| 43 | +QString QgsSplitVectorLayerAlgorithm::groupId() const |
| 44 | +{ |
| 45 | + return QStringLiteral( "vectorgeneral" ); |
| 46 | +} |
| 47 | + |
| 48 | +QString QgsSplitVectorLayerAlgorithm::shortHelpString() const |
| 49 | +{ |
| 50 | + return QObject::tr( "Splits input vector layer into multiple layers by specified unique ID field." ); |
| 51 | +} |
| 52 | + |
| 53 | +QgsSplitVectorLayerAlgorithm *QgsSplitVectorLayerAlgorithm::createInstance() const |
| 54 | +{ |
| 55 | + return new QgsSplitVectorLayerAlgorithm(); |
| 56 | +} |
| 57 | + |
| 58 | +void QgsSplitVectorLayerAlgorithm::initAlgorithm( const QVariantMap & ) |
| 59 | +{ |
| 60 | + addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ) ) ); |
| 61 | + addParameter( new QgsProcessingParameterField( QStringLiteral( "FIELD" ), QObject::tr( "Unique ID field" ), |
| 62 | + QVariant(), QStringLiteral( "INPUT" ) ) ); |
| 63 | + addParameter( new QgsProcessingParameterFolderDestination( QStringLiteral( "OUTPUT" ), QObject::tr( "Output directory" ) ) ); |
| 64 | + addOutput( new QgsProcessingOutputMultipleLayers( QStringLiteral( "OUTPUT_LAYERS" ), QObject::tr( "Output layers" ) ) ); |
| 65 | +} |
| 66 | + |
| 67 | +QVariantMap QgsSplitVectorLayerAlgorithm::processAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) |
| 68 | +{ |
| 69 | + std::unique_ptr< QgsFeatureSource > source( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) ); |
| 70 | + if ( !source ) |
| 71 | + throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "INPUT" ) ) ); |
| 72 | + |
| 73 | + QString fieldName = parameterAsString( parameters, QStringLiteral( "FIELD" ), context ); |
| 74 | + QString outputDir = parameterAsString( parameters, QStringLiteral( "OUTPUT" ), context ); |
| 75 | + |
| 76 | + if ( !QDir().mkpath( outputDir ) ) |
| 77 | + throw QgsProcessingException( QStringLiteral( "Failed to create output directory." ) ); |
| 78 | + |
| 79 | + QgsFields fields = source->fields(); |
| 80 | + QgsCoordinateReferenceSystem crs = source->sourceCrs(); |
| 81 | + QgsWkbTypes::Type geometryType = source->wkbType(); |
| 82 | + int fieldIndex = fields.lookupField( fieldName ); |
| 83 | + QSet< QVariant > uniqueValues = source->uniqueValues( fieldIndex ); |
| 84 | + QString baseName = outputDir + QDir::separator() + fieldName; |
| 85 | + QString outputFormat = context.preferredVectorFormat(); |
| 86 | + if ( !QgsVectorFileWriter::supportedFormatExtensions().contains( outputFormat, Qt::CaseInsensitive ) ) |
| 87 | + outputFormat = QStringLiteral( "gpkg" ); |
| 88 | + |
| 89 | + int current = 0; |
| 90 | + double step = uniqueValues.size() > 0 ? 100.0 / uniqueValues.size() : 1; |
| 91 | + |
| 92 | + int count = 0; |
| 93 | + QgsFeature feat; |
| 94 | + QStringList outputLayers; |
| 95 | + std::unique_ptr< QgsFeatureSink > sink; |
| 96 | + |
| 97 | + for ( auto it = uniqueValues.constBegin(); it != uniqueValues.constEnd(); ++it ) |
| 98 | + { |
| 99 | + if ( feedback->isCanceled() ) |
| 100 | + break; |
| 101 | + |
| 102 | + QString fileName = QStringLiteral( "%1_%2.%3" ).arg( baseName ).arg( current ).arg( outputFormat ); |
| 103 | + feedback->pushInfo( QObject::tr( "Creating layer: %1" ).arg( fileName ) ); |
| 104 | + |
| 105 | + sink.reset( QgsProcessingUtils::createFeatureSink( fileName, context, fields, geometryType, crs ) ); |
| 106 | + QString expr = QStringLiteral( "%1 = %2" ).arg( QgsExpression::quotedColumnRef( fieldName ), QgsExpression::quotedValue( *it ) ); |
| 107 | + QgsFeatureIterator features = source->getFeatures( QgsFeatureRequest().setFilterExpression( expr ) ); |
| 108 | + while ( features.nextFeature( feat ) ) |
| 109 | + { |
| 110 | + if ( feedback->isCanceled() ) |
| 111 | + break; |
| 112 | + |
| 113 | + sink->addFeature( feat, QgsFeatureSink::FastInsert ); |
| 114 | + count += 1; |
| 115 | + } |
| 116 | + |
| 117 | + feedback->pushInfo( QObject::tr( "Added %1 features to layer" ).arg( count ) ); |
| 118 | + outputLayers << fileName; |
| 119 | + |
| 120 | + current += 1; |
| 121 | + feedback->setProgress( current * step ); |
| 122 | + } |
| 123 | + |
| 124 | + QVariantMap outputs; |
| 125 | + outputs.insert( QStringLiteral( "OUTPUT" ), outputDir ); |
| 126 | + outputs.insert( QStringLiteral( "OUTPUT_LAYERS" ), outputLayers ); |
| 127 | + return outputs; |
| 128 | +} |
| 129 | + |
| 130 | +///@endcond |
0 commit comments