Skip to content

Commit 072d27d

Browse files
committedMay 11, 2020
[processing] port Split vector layer algorithm to C++
1 parent b2890f0 commit 072d27d

File tree

6 files changed

+191
-134
lines changed

6 files changed

+191
-134
lines changed
 

‎python/plugins/processing/algs/qgis/QgisAlgorithmProvider.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@
9595
from .VectorLayerHistogram import VectorLayerHistogram
9696
from .VectorLayerScatterplot import VectorLayerScatterplot
9797
from .VectorLayerScatterplot3D import VectorLayerScatterplot3D
98-
from .VectorSplit import VectorSplit
9998
from .VoronoiPolygons import VoronoiPolygons
10099

101100

@@ -177,7 +176,6 @@ def getAlgs(self):
177176
VectorLayerHistogram(),
178177
VectorLayerScatterplot(),
179178
VectorLayerScatterplot3D(),
180-
VectorSplit(),
181179
VoronoiPolygons(),
182180
]
183181

‎python/plugins/processing/algs/qgis/VectorSplit.py

Lines changed: 0 additions & 131 deletions
This file was deleted.

‎src/analysis/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ SET(QGIS_ANALYSIS_SRCS
155155
processing/qgsalgorithmsplitfeaturesbyattributecharacter.cpp
156156
processing/qgsalgorithmsplitlineantimeridian.cpp
157157
processing/qgsalgorithmsplitlinesbylength.cpp
158+
processing/qgsalgorithmsplitvectorlayer.cpp
158159
processing/qgsalgorithmsplitwithlines.cpp
159160
processing/qgsalgorithmstringconcatenation.cpp
160161
processing/qgsalgorithmswapxy.cpp
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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 &parameters, 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
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/***************************************************************************
2+
qgsalgorithmsplitvectorlayer.h
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+
#ifndef QGSALGORITHMSPLITVECTORLAYER_H
19+
#define QGSALGORITHMSPLITVECTORLAYER_H
20+
21+
#define SIP_NO_FILE
22+
23+
#include "qgis_sip.h"
24+
#include "qgsapplication.h"
25+
#include "qgsprocessingalgorithm.h"
26+
27+
///@cond PRIVATE
28+
29+
/**
30+
* Native split vector layer algorithm.
31+
*/
32+
class QgsSplitVectorLayerAlgorithm : public QgsProcessingAlgorithm
33+
{
34+
35+
public:
36+
37+
QgsSplitVectorLayerAlgorithm() = default;
38+
void initAlgorithm( const QVariantMap &configuration = QVariantMap() ) override;
39+
QString name() const override;
40+
QString displayName() const override;
41+
QStringList tags() const override;
42+
QString group() const override;
43+
QString groupId() const override;
44+
QString shortHelpString() const override;
45+
QIcon icon() const override { return QgsApplication::getThemeIcon( QStringLiteral( "/algorithms/mAlgorithmSplitLayer.svg" ) ); }
46+
QString svgIconPath() const override { return QgsApplication::iconPath( QStringLiteral( "/algorithms/mAlgorithmSplitLayer.svg" ) ); }
47+
QgsSplitVectorLayerAlgorithm *createInstance() const override SIP_FACTORY;
48+
49+
protected:
50+
51+
QVariantMap processAlgorithm( const QVariantMap &parameters,
52+
QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;
53+
};
54+
55+
///@endcond PRIVATE
56+
57+
#endif // QGSALGORITHMSPLITVECTORLAYER_H

‎src/analysis/processing/qgsnativealgorithms.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,11 @@
146146
#include "qgsalgorithmsmooth.h"
147147
#include "qgsalgorithmsnaptogrid.h"
148148
#include "qgsalgorithmspatialindex.h"
149+
#include "qgsalgorithmsplitfeaturesbyattributecharacter.h"
149150
#include "qgsalgorithmsplitlineantimeridian.h"
150151
#include "qgsalgorithmsplitlinesbylength.h"
152+
#include "qgsalgorithmsplitvectorlayer.h"
151153
#include "qgsalgorithmsplitwithlines.h"
152-
#include "qgsalgorithmsplitfeaturesbyattributecharacter.h"
153154
#include "qgsalgorithmstringconcatenation.h"
154155
#include "qgsalgorithmsubdivide.h"
155156
#include "qgsalgorithmsumlinelength.h"
@@ -369,6 +370,7 @@ void QgsNativeAlgorithms::loadAlgorithms()
369370
addAlgorithm( new QgsSplitFeaturesByAttributeCharacterAlgorithm() );
370371
addAlgorithm( new QgsSplitGeometryAtAntimeridianAlgorithm() );
371372
addAlgorithm( new QgsSplitLinesByLengthAlgorithm() );
373+
addAlgorithm( new QgsSplitVectorLayerAlgorithm() );
372374
addAlgorithm( new QgsSplitWithLinesAlgorithm() );
373375
addAlgorithm( new QgsStringConcatenationAlgorithm() );
374376
addAlgorithm( new QgsStyleFromProjectAlgorithm() );

0 commit comments

Comments
 (0)
Please sign in to comment.