QGIS API Documentation 3.39.0-Master (d85f3c2a281)
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" ),
70 QList< int >() << static_cast< int >( Qgis::ProcessingSourceType::Vector ) ) );
71 addParameter( new QgsProcessingParameterEnum( QStringLiteral( "METHOD" ), QObject::tr( "Method" ), QStringList() << QObject::tr( "Number of features" ) << QObject::tr( "Percentage of features" ), false, 0 ) );
72 addParameter( new QgsProcessingParameterNumber( QStringLiteral( "NUMBER" ), QObject::tr( "Number/percentage of features" ),
74
75 addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Extracted (random)" ) ) );
76}
77
78QVariantMap QgsRandomExtractAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
79{
80 std::unique_ptr< QgsProcessingFeatureSource > source( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
81 if ( !source )
82 throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "INPUT" ) ) );
83
84 QString dest;
85 std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, source->fields(),
86 source->wkbType(), source->sourceCrs(), QgsFeatureSink::RegeneratePrimaryKey ) );
87 if ( !sink )
88 throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "OUTPUT" ) ) );
89
90 const int method = parameterAsEnum( parameters, QStringLiteral( "METHOD" ), context );
91 int number = parameterAsInt( parameters, QStringLiteral( "NUMBER" ), context );
92 const long count = source->featureCount();
93
94 if ( method == 0 )
95 {
96 // number of features
97 if ( number > count )
98 throw QgsProcessingException( QObject::tr( "Selected number is greater than feature count. Choose a lower value and try again." ) );
99 }
100 else
101 {
102 // percentage of features
103 if ( number > 100 )
104 throw QgsProcessingException( QObject::tr( "Percentage can't be greater than 100. Choose a lower value and try again." ) );
105
106 number = static_cast< int >( std::ceil( number * count / 100 ) );
107 }
108
109 // Build a list of all feature ids
110 QgsFeatureIterator fit = source->getFeatures( QgsFeatureRequest()
112 .setNoAttributes() );
113 std::vector< QgsFeatureId > allFeats;
114 allFeats.reserve( count );
115 QgsFeature f;
116 feedback->pushInfo( QObject::tr( "Building list of all features..." ) );
117 while ( fit.nextFeature( f ) )
118 {
119 if ( feedback->isCanceled() )
120 return QVariantMap();
121 allFeats.push_back( f.id() );
122 }
123 feedback->pushInfo( QObject::tr( "Done." ) );
124
125 // initialize random engine
126 std::random_device randomDevice;
127 std::mt19937 mersenneTwister( randomDevice() );
128 std::uniform_int_distribution<size_t> fidsDistribution;
129
130 // If the number of features to select is greater than half the total number of features
131 // we will instead randomly select features to *exclude* from the output layer
132 size_t actualFeatureCount = allFeats.size();
133 size_t shuffledFeatureCount = number;
134 bool invertSelection = static_cast< size_t>( number ) > actualFeatureCount / 2;
135 if ( invertSelection )
136 shuffledFeatureCount = actualFeatureCount - number;
137
138 size_t nb = actualFeatureCount;
139
140 // Shuffle <number> features at the start of the iterator
141 feedback->pushInfo( QObject::tr( "Randomly select %1 features" ).arg( number ) );
142 auto cursor = allFeats.begin();
143 using difference_type = std::vector<QgsFeatureId>::difference_type;
144 while ( shuffledFeatureCount-- )
145 {
146 if ( feedback->isCanceled() )
147 return QVariantMap();
148
149 // Update the distribution to match the number of unshuffled features
150 fidsDistribution.param( std::uniform_int_distribution<size_t>::param_type( 0, nb - 1 ) );
151 // Swap the current feature with a random one
152 std::swap( *cursor, *( cursor + static_cast<difference_type>( fidsDistribution( mersenneTwister ) ) ) );
153 // Move the cursor to the next feature
154 ++cursor;
155
156 // Decrement the number of unshuffled features
157 --nb;
158 }
159
160 // Insert the selected features into a QgsFeatureIds set
161 QgsFeatureIds selected;
162 if ( invertSelection )
163 for ( auto it = cursor; it != allFeats.end(); ++it )
164 selected.insert( *it );
165 else
166 for ( auto it = allFeats.begin(); it != cursor; ++it )
167 selected.insert( *it );
168
169 feedback->pushInfo( QObject::tr( "Adding selected features" ) );
171 while ( fit.nextFeature( f ) )
172 {
173 if ( feedback->isCanceled() )
174 return QVariantMap();
175
176 if ( !sink->addFeature( f, QgsFeatureSink::FastInsert ) )
177 throw QgsProcessingException( writeFeatureError( sink.get(), parameters, QStringLiteral( "OUTPUT" ) ) );
178 }
179
180 QVariantMap outputs;
181 outputs.insert( QStringLiteral( "OUTPUT" ), dest );
182 return outputs;
183}
184
@ 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:3337
@ 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