25QString QgsRandomExtractAlgorithm::name()
const
27 return QStringLiteral(
"randomextract" );
30QString QgsRandomExtractAlgorithm::displayName()
const
32 return QObject::tr(
"Random extract" );
35QStringList QgsRandomExtractAlgorithm::tags()
const
37 return QObject::tr(
"extract,filter,random,number,percentage" ).split(
',' );
40QString QgsRandomExtractAlgorithm::group()
const
42 return QObject::tr(
"Vector selection" );
45QString QgsRandomExtractAlgorithm::groupId()
const
47 return QStringLiteral(
"vectorselection" );
50QString QgsRandomExtractAlgorithm::shortDescription()
const
52 return QObject::tr(
"Generates a vector layer that contains only a random subset of the features in an input layer." );
55QString QgsRandomExtractAlgorithm::shortHelpString()
const
57 return QObject::tr(
"This algorithm takes a vector layer and generates a new one that contains only a subset "
58 "of the features in the input layer.\n\n"
59 "The subset is defined randomly, using a percentage or count value to define the total number "
60 "of features in the subset." );
68QgsRandomExtractAlgorithm *QgsRandomExtractAlgorithm::createInstance()
const
70 return new QgsRandomExtractAlgorithm();
73void QgsRandomExtractAlgorithm::initAlgorithm(
const QVariantMap & )
76 addParameter(
new QgsProcessingParameterEnum( QStringLiteral(
"METHOD" ), QObject::tr(
"Method" ), QStringList() << QObject::tr(
"Number of features" ) << QObject::tr(
"Percentage of features" ),
false, 0 ) );
84 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, QStringLiteral(
"INPUT" ), context ) );
89 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, QStringLiteral(
"OUTPUT" ), context, dest, source->fields(), source->wkbType(), source->sourceCrs(),
QgsFeatureSink::RegeneratePrimaryKey ) );
93 const int method = parameterAsEnum( parameters, QStringLiteral(
"METHOD" ), context );
94 int number = parameterAsInt( parameters, QStringLiteral(
"NUMBER" ), context );
95 const long count = source->featureCount();
100 if ( number > count )
101 throw QgsProcessingException( QObject::tr(
"Selected number is greater than feature count. Choose a lower value and try again." ) );
107 throw QgsProcessingException( QObject::tr(
"Percentage can't be greater than 100. Choose a lower value and try again." ) );
109 number =
static_cast<int>( std::ceil( number * count / 100 ) );
115 .setNoAttributes() );
116 std::vector<QgsFeatureId> allFeats;
117 allFeats.reserve( count );
119 feedback->
pushInfo( QObject::tr(
"Building list of all features..." ) );
123 return QVariantMap();
124 allFeats.push_back( f.
id() );
126 feedback->
pushInfo( QObject::tr(
"Done." ) );
129 std::random_device randomDevice;
130 std::mt19937 mersenneTwister( randomDevice() );
131 std::uniform_int_distribution<size_t> fidsDistribution;
135 size_t actualFeatureCount = allFeats.size();
136 size_t shuffledFeatureCount = number;
137 bool invertSelection =
static_cast<size_t>( number ) > actualFeatureCount / 2;
138 if ( invertSelection )
139 shuffledFeatureCount = actualFeatureCount - number;
141 size_t nb = actualFeatureCount;
144 feedback->
pushInfo( QObject::tr(
"Randomly select %1 features" ).arg( number ) );
145 auto cursor = allFeats.begin();
146 using difference_type = std::vector<QgsFeatureId>::difference_type;
147 while ( shuffledFeatureCount-- )
150 return QVariantMap();
153 fidsDistribution.param( std::uniform_int_distribution<size_t>::param_type( 0, nb - 1 ) );
155 std::swap( *cursor, *( cursor +
static_cast<difference_type
>( fidsDistribution( mersenneTwister ) ) ) );
165 if ( invertSelection )
166 for (
auto it = cursor; it != allFeats.end(); ++it )
167 selected.insert( *it );
169 for (
auto it = allFeats.begin(); it != cursor; ++it )
170 selected.insert( *it );
172 feedback->
pushInfo( QObject::tr(
"Adding selected features" ) );
177 return QVariantMap();
186 outputs.insert( QStringLiteral(
"OUTPUT" ), dest );
@ 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.
@ 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.
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...
bool isCanceled() const
Tells whether the operation has been canceled already.
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