Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[processing] port Split vector layer algorithm to C++
- Loading branch information
Showing
6 changed files
with
191 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
src/analysis/processing/qgsalgorithmsplitvectorlayer.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/*************************************************************************** | ||
qgsalgorithmsplitvectorlayer.cpp | ||
--------------------- | ||
begin : May 2020 | ||
copyright : (C) 2020 by Alexander Bruy | ||
email : alexander dot bruy at gmail dot com | ||
***************************************************************************/ | ||
|
||
/*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#include "qgsalgorithmsplitvectorlayer.h" | ||
#include "qgsvectorfilewriter.h" | ||
|
||
///@cond PRIVATE | ||
|
||
QString QgsSplitVectorLayerAlgorithm::name() const | ||
{ | ||
return QStringLiteral( "splitvectorlayer" ); | ||
} | ||
|
||
QString QgsSplitVectorLayerAlgorithm::displayName() const | ||
{ | ||
return QObject::tr( "Split vector layer" ); | ||
} | ||
|
||
QStringList QgsSplitVectorLayerAlgorithm::tags() const | ||
{ | ||
return QObject::tr( "vector,split,field,unique" ).split( ',' ); | ||
} | ||
|
||
QString QgsSplitVectorLayerAlgorithm::group() const | ||
{ | ||
return QObject::tr( "Vector general" ); | ||
} | ||
|
||
QString QgsSplitVectorLayerAlgorithm::groupId() const | ||
{ | ||
return QStringLiteral( "vectorgeneral" ); | ||
} | ||
|
||
QString QgsSplitVectorLayerAlgorithm::shortHelpString() const | ||
{ | ||
return QObject::tr( "Splits input vector layer into multiple layers by specified unique ID field." ); | ||
} | ||
|
||
QgsSplitVectorLayerAlgorithm *QgsSplitVectorLayerAlgorithm::createInstance() const | ||
{ | ||
return new QgsSplitVectorLayerAlgorithm(); | ||
} | ||
|
||
void QgsSplitVectorLayerAlgorithm::initAlgorithm( const QVariantMap & ) | ||
{ | ||
addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ) ) ); | ||
addParameter( new QgsProcessingParameterField( QStringLiteral( "FIELD" ), QObject::tr( "Unique ID field" ), | ||
QVariant(), QStringLiteral( "INPUT" ) ) ); | ||
addParameter( new QgsProcessingParameterFolderDestination( QStringLiteral( "OUTPUT" ), QObject::tr( "Output directory" ) ) ); | ||
addOutput( new QgsProcessingOutputMultipleLayers( QStringLiteral( "OUTPUT_LAYERS" ), QObject::tr( "Output layers" ) ) ); | ||
} | ||
|
||
QVariantMap QgsSplitVectorLayerAlgorithm::processAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) | ||
{ | ||
std::unique_ptr< QgsFeatureSource > source( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) ); | ||
if ( !source ) | ||
throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "INPUT" ) ) ); | ||
|
||
QString fieldName = parameterAsString( parameters, QStringLiteral( "FIELD" ), context ); | ||
QString outputDir = parameterAsString( parameters, QStringLiteral( "OUTPUT" ), context ); | ||
|
||
if ( !QDir().mkpath( outputDir ) ) | ||
throw QgsProcessingException( QStringLiteral( "Failed to create output directory." ) ); | ||
|
||
QgsFields fields = source->fields(); | ||
QgsCoordinateReferenceSystem crs = source->sourceCrs(); | ||
QgsWkbTypes::Type geometryType = source->wkbType(); | ||
int fieldIndex = fields.lookupField( fieldName ); | ||
QSet< QVariant > uniqueValues = source->uniqueValues( fieldIndex ); | ||
QString baseName = outputDir + QDir::separator() + fieldName; | ||
QString outputFormat = context.preferredVectorFormat(); | ||
if ( !QgsVectorFileWriter::supportedFormatExtensions().contains( outputFormat, Qt::CaseInsensitive ) ) | ||
outputFormat = QStringLiteral( "gpkg" ); | ||
|
||
int current = 0; | ||
double step = uniqueValues.size() > 0 ? 100.0 / uniqueValues.size() : 1; | ||
|
||
int count = 0; | ||
QgsFeature feat; | ||
QStringList outputLayers; | ||
std::unique_ptr< QgsFeatureSink > sink; | ||
|
||
for ( auto it = uniqueValues.constBegin(); it != uniqueValues.constEnd(); ++it ) | ||
{ | ||
if ( feedback->isCanceled() ) | ||
break; | ||
|
||
QString fileName = QStringLiteral( "%1_%2.%3" ).arg( baseName ).arg( current ).arg( outputFormat ); | ||
feedback->pushInfo( QObject::tr( "Creating layer: %1" ).arg( fileName ) ); | ||
|
||
sink.reset( QgsProcessingUtils::createFeatureSink( fileName, context, fields, geometryType, crs ) ); | ||
QString expr = QStringLiteral( "%1 = %2" ).arg( QgsExpression::quotedColumnRef( fieldName ), QgsExpression::quotedValue( *it ) ); | ||
QgsFeatureIterator features = source->getFeatures( QgsFeatureRequest().setFilterExpression( expr ) ); | ||
while ( features.nextFeature( feat ) ) | ||
{ | ||
if ( feedback->isCanceled() ) | ||
break; | ||
|
||
sink->addFeature( feat, QgsFeatureSink::FastInsert ); | ||
count += 1; | ||
} | ||
|
||
feedback->pushInfo( QObject::tr( "Added %1 features to layer" ).arg( count ) ); | ||
outputLayers << fileName; | ||
|
||
current += 1; | ||
feedback->setProgress( current * step ); | ||
} | ||
|
||
QVariantMap outputs; | ||
outputs.insert( QStringLiteral( "OUTPUT" ), outputDir ); | ||
outputs.insert( QStringLiteral( "OUTPUT_LAYERS" ), outputLayers ); | ||
return outputs; | ||
} | ||
|
||
///@endcond |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/*************************************************************************** | ||
qgsalgorithmsplitvectorlayer.h | ||
------------------------------ | ||
begin : May 2020 | ||
copyright : (C) 2020 by Alexander Bruy | ||
email : alexander dot bruy at gmail dot com | ||
***************************************************************************/ | ||
|
||
/*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#ifndef QGSALGORITHMSPLITVECTORLAYER_H | ||
#define QGSALGORITHMSPLITVECTORLAYER_H | ||
|
||
#define SIP_NO_FILE | ||
|
||
#include "qgis_sip.h" | ||
#include "qgsapplication.h" | ||
#include "qgsprocessingalgorithm.h" | ||
|
||
///@cond PRIVATE | ||
|
||
/** | ||
* Native split vector layer algorithm. | ||
*/ | ||
class QgsSplitVectorLayerAlgorithm : public QgsProcessingAlgorithm | ||
{ | ||
|
||
public: | ||
|
||
QgsSplitVectorLayerAlgorithm() = default; | ||
void initAlgorithm( const QVariantMap &configuration = QVariantMap() ) override; | ||
QString name() const override; | ||
QString displayName() const override; | ||
QStringList tags() const override; | ||
QString group() const override; | ||
QString groupId() const override; | ||
QString shortHelpString() const override; | ||
QIcon icon() const override { return QgsApplication::getThemeIcon( QStringLiteral( "/algorithms/mAlgorithmSplitLayer.svg" ) ); } | ||
QString svgIconPath() const override { return QgsApplication::iconPath( QStringLiteral( "/algorithms/mAlgorithmSplitLayer.svg" ) ); } | ||
QgsSplitVectorLayerAlgorithm *createInstance() const override SIP_FACTORY; | ||
|
||
protected: | ||
|
||
QVariantMap processAlgorithm( const QVariantMap ¶meters, | ||
QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override; | ||
}; | ||
|
||
///@endcond PRIVATE | ||
|
||
#endif // QGSALGORITHMSPLITVECTORLAYER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters