QGIS API Documentation 3.28.0-Firenze (ed3ad0430f)
qgsalgorithmrandomextract.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmrandomextract.cpp
3 ---------------------
4 begin : December 2019
5 copyright : (C) 2019 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
19#include <random>
20#include <functional>
21
23
24QString QgsRandomExtractAlgorithm::name() const
25{
26 return QStringLiteral( "randomextract" );
27}
28
29QString QgsRandomExtractAlgorithm::displayName() const
30{
31 return QObject::tr( "Random extract" );
32}
33
34QStringList QgsRandomExtractAlgorithm::tags() const
35{
36 return QObject::tr( "extract,filter,random,number,percentage" ).split( ',' );
37}
38
39QString QgsRandomExtractAlgorithm::group() const
40{
41 return QObject::tr( "Vector selection" );
42}
43
44QString QgsRandomExtractAlgorithm::groupId() const
45{
46 return QStringLiteral( "vectorselection" );
47}
48
49QString QgsRandomExtractAlgorithm::shortHelpString() const
50{
51 return QObject::tr( "This algorithm takes a vector layer and generates a new one that contains only a subset "
52 "of the features in the input layer.\n\n"
53 "The subset is defined randomly, using a percentage or count value to define the total number "
54 "of features in the subset." );
55}
56
57QgsRandomExtractAlgorithm *QgsRandomExtractAlgorithm::createInstance() const
58{
59 return new QgsRandomExtractAlgorithm();
60}
61
62void QgsRandomExtractAlgorithm::initAlgorithm( const QVariantMap & )
63{
64 addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ),
65 QList< int >() << QgsProcessing::TypeVector ) );
66 addParameter( new QgsProcessingParameterEnum( QStringLiteral( "METHOD" ), QObject::tr( "Method" ), QStringList() << QObject::tr( "Number of features" ) << QObject::tr( "Percentage of features" ), false, 0 ) );
67 addParameter( new QgsProcessingParameterNumber( QStringLiteral( "NUMBER" ), QObject::tr( "Number/percentage of features" ),
69
70 addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Extracted (random)" ) ) );
71}
72
73QVariantMap QgsRandomExtractAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
74{
75 std::unique_ptr< QgsProcessingFeatureSource > source( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
76 if ( !source )
77 throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "INPUT" ) ) );
78
79 QString dest;
80 std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, source->fields(),
81 source->wkbType(), source->sourceCrs(), QgsFeatureSink::RegeneratePrimaryKey ) );
82 if ( !sink )
83 throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "OUTPUT" ) ) );
84
85 const int method = parameterAsEnum( parameters, QStringLiteral( "METHOD" ), context );
86 int number = parameterAsInt( parameters, QStringLiteral( "NUMBER" ), context );
87
88 const long count = source->featureCount();
89
90 if ( method == 0 )
91 {
92 // number of features
93 if ( number > count )
94 throw QgsProcessingException( QObject::tr( "Selected number is greater than feature count. Choose a lower value and try again." ) );
95 }
96 else
97 {
98 // percentage of features
99 if ( number > 100 )
100 throw QgsProcessingException( QObject::tr( "Percentage can't be greater than 100. Choose a lower value and try again." ) );
101
102 number = static_cast< int >( std::ceil( number * count / 100 ) );
103 }
104
105 // initialize random engine
106 std::random_device randomDevice;
107 const std::mt19937 mersenneTwister( randomDevice() );
108 const std::uniform_int_distribution<int> fidsDistribution( 0, count );
109
110 QVector< QgsFeatureId > fids( number );
111 std::generate( fids.begin(), fids.end(), bind( fidsDistribution, mersenneTwister ) );
112
113 QHash< QgsFeatureId, int > idsCount;
114 for ( const QgsFeatureId id : fids )
115 {
116 if ( feedback->isCanceled() )
117 {
118 break;
119 }
120
121 idsCount[ id ] += 1;
122 }
123
124 const QgsFeatureIds ids = qgis::listToSet( idsCount.keys() );
126
127 QgsFeature f;
128 while ( fit.nextFeature( f ) )
129 {
130 if ( feedback->isCanceled() )
131 {
132 break;
133 }
134
135 const int count = idsCount.value( f.id() );
136 for ( int i = 0; i < count; ++i )
137 {
138 if ( !sink->addFeature( f, QgsFeatureSink::FastInsert ) )
139 throw QgsProcessingException( writeFeatureError( sink.get(), parameters, QStringLiteral( "OUTPUT" ) ) );
140 }
141 }
142
143 QVariantMap outputs;
144 outputs.insert( QStringLiteral( "OUTPUT" ), dest );
145 return outputs;
146}
147
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...
@ RegeneratePrimaryKey
This flag indicates, that a primary key field cannot be guaranteed to be unique and the sink should i...
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:56
Q_GADGET QgsFeatureId id
Definition: qgsfeature.h:64
bool isCanceled() const SIP_HOLDGIL
Tells whether the operation has been canceled already.
Definition: qgsfeedback.h:54
Contains information about the context in which a processing algorithm is executed.
Custom exception class for processing related exceptions.
Definition: qgsexception.h:83
@ FlagSkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Base class for providing feedback from a processing algorithm.
An enum based parameter for processing algorithms, allowing for selection from predefined values.
A feature sink output for processing algorithms.
An input feature source (such as vector layers) parameter for processing algorithms.
A numeric parameter for processing algorithms.
@ TypeVector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
Definition: qgsprocessing.h:54
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeatureid.h:37
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
Definition: qgsfeatureid.h:28