QGIS API Documentation 3.41.0-Master (af5edcb665c)
Loading...
Searching...
No Matches
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
57Qgis::ProcessingAlgorithmDocumentationFlags QgsRandomExtractAlgorithm::documentationFlags() const
58{
60}
61
62QgsRandomExtractAlgorithm *QgsRandomExtractAlgorithm::createInstance() const
63{
64 return new QgsRandomExtractAlgorithm();
65}
66
67void QgsRandomExtractAlgorithm::initAlgorithm( const QVariantMap & )
68{
69 addParameter( new QgsProcessingParameterFeatureSource( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ), QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::Vector ) ) );
70 addParameter( new QgsProcessingParameterEnum( QStringLiteral( "METHOD" ), QObject::tr( "Method" ), QStringList() << QObject::tr( "Number of features" ) << QObject::tr( "Percentage of features" ), false, 0 ) );
71 addParameter( new QgsProcessingParameterNumber( QStringLiteral( "NUMBER" ), QObject::tr( "Number/percentage of features" ), Qgis::ProcessingNumberParameterType::Integer, 10, false, 0 ) );
72
73 addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Extracted (random)" ) ) );
74}
75
76QVariantMap QgsRandomExtractAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
77{
78 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
79 if ( !source )
80 throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "INPUT" ) ) );
81
82 QString dest;
83 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, source->fields(), source->wkbType(), source->sourceCrs(), QgsFeatureSink::RegeneratePrimaryKey ) );
84 if ( !sink )
85 throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "OUTPUT" ) ) );
86
87 const int method = parameterAsEnum( parameters, QStringLiteral( "METHOD" ), context );
88 int number = parameterAsInt( parameters, QStringLiteral( "NUMBER" ), context );
89 const long count = source->featureCount();
90
91 if ( method == 0 )
92 {
93 // number of features
94 if ( number > count )
95 throw QgsProcessingException( QObject::tr( "Selected number is greater than feature count. Choose a lower value and try again." ) );
96 }
97 else
98 {
99 // percentage of features
100 if ( number > 100 )
101 throw QgsProcessingException( QObject::tr( "Percentage can't be greater than 100. Choose a lower value and try again." ) );
102
103 number = static_cast<int>( std::ceil( number * count / 100 ) );
104 }
105
106 // Build a list of all feature ids
107 QgsFeatureIterator fit = source->getFeatures( QgsFeatureRequest()
109 .setNoAttributes() );
110 std::vector<QgsFeatureId> allFeats;
111 allFeats.reserve( count );
112 QgsFeature f;
113 feedback->pushInfo( QObject::tr( "Building list of all features..." ) );
114 while ( fit.nextFeature( f ) )
115 {
116 if ( feedback->isCanceled() )
117 return QVariantMap();
118 allFeats.push_back( f.id() );
119 }
120 feedback->pushInfo( QObject::tr( "Done." ) );
121
122 // initialize random engine
123 std::random_device randomDevice;
124 std::mt19937 mersenneTwister( randomDevice() );
125 std::uniform_int_distribution<size_t> fidsDistribution;
126
127 // If the number of features to select is greater than half the total number of features
128 // we will instead randomly select features to *exclude* from the output layer
129 size_t actualFeatureCount = allFeats.size();
130 size_t shuffledFeatureCount = number;
131 bool invertSelection = static_cast<size_t>( number ) > actualFeatureCount / 2;
132 if ( invertSelection )
133 shuffledFeatureCount = actualFeatureCount - number;
134
135 size_t nb = actualFeatureCount;
136
137 // Shuffle <number> features at the start of the iterator
138 feedback->pushInfo( QObject::tr( "Randomly select %1 features" ).arg( number ) );
139 auto cursor = allFeats.begin();
140 using difference_type = std::vector<QgsFeatureId>::difference_type;
141 while ( shuffledFeatureCount-- )
142 {
143 if ( feedback->isCanceled() )
144 return QVariantMap();
145
146 // Update the distribution to match the number of unshuffled features
147 fidsDistribution.param( std::uniform_int_distribution<size_t>::param_type( 0, nb - 1 ) );
148 // Swap the current feature with a random one
149 std::swap( *cursor, *( cursor + static_cast<difference_type>( fidsDistribution( mersenneTwister ) ) ) );
150 // Move the cursor to the next feature
151 ++cursor;
152
153 // Decrement the number of unshuffled features
154 --nb;
155 }
156
157 // Insert the selected features into a QgsFeatureIds set
158 QgsFeatureIds selected;
159 if ( invertSelection )
160 for ( auto it = cursor; it != allFeats.end(); ++it )
161 selected.insert( *it );
162 else
163 for ( auto it = allFeats.begin(); it != cursor; ++it )
164 selected.insert( *it );
165
166 feedback->pushInfo( QObject::tr( "Adding selected features" ) );
168 while ( fit.nextFeature( f ) )
169 {
170 if ( feedback->isCanceled() )
171 return QVariantMap();
172
173 if ( !sink->addFeature( f, QgsFeatureSink::FastInsert ) )
174 throw QgsProcessingException( writeFeatureError( sink.get(), parameters, QStringLiteral( "OUTPUT" ) ) );
175 }
176
177 sink->finalize();
178
179 QVariantMap outputs;
180 outputs.insert( QStringLiteral( "OUTPUT" ), dest );
181 return outputs;
182}
183
@ Vector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
@ NoGeometry
Geometry is not required. It may still be returned if e.g. required for a filter condition.
@ RegeneratesPrimaryKey
Algorithm always drops any existing primary keys or FID values and regenerates them in outputs.
QFlags< ProcessingAlgorithmDocumentationFlag > ProcessingAlgorithmDocumentationFlags
Flags describing algorithm behavior for documentation purposes.
Definition qgis.h:3430
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
Fetch next feature and stores in f, returns true on success.
This class wraps a request for features to a vector layer (or directly its vector data provider).
QgsFeatureRequest & setFlags(Qgis::FeatureRequestFlags flags)
Sets flags that affect how features will be fetched.
QgsFeatureRequest & setFilterFids(const QgsFeatureIds &fids)
Sets the feature IDs that should be fetched.
QgsFeatureRequest & setNoAttributes()
Set that no attributes will be fetched.
@ 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:58
QgsFeatureId id
Definition qgsfeature.h:66
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition qgsfeedback.h:53
Contains information about the context in which a processing algorithm is executed.
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.
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.
QSet< QgsFeatureId > QgsFeatureIds