24 QString QgsRandomExtractAlgorithm::name()
const 26 return QStringLiteral(
"randomextract" );
29 QString QgsRandomExtractAlgorithm::displayName()
const 31 return QObject::tr(
"Random extract" );
34 QStringList QgsRandomExtractAlgorithm::tags()
const 36 return QObject::tr(
"extract,filter,random,number,percentage" ).split(
',' );
39 QString QgsRandomExtractAlgorithm::group()
const 41 return QObject::tr(
"Vector selection" );
44 QString QgsRandomExtractAlgorithm::groupId()
const 46 return QStringLiteral(
"vectorselection" );
49 QString QgsRandomExtractAlgorithm::shortHelpString()
const 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." );
57 QgsRandomExtractAlgorithm *QgsRandomExtractAlgorithm::createInstance()
const 59 return new QgsRandomExtractAlgorithm();
62 void QgsRandomExtractAlgorithm::initAlgorithm(
const QVariantMap & )
66 addParameter(
new QgsProcessingParameterEnum( QStringLiteral(
"METHOD" ), QObject::tr(
"Method" ), QStringList() << QObject::tr(
"Number of features" ) << QObject::tr(
"Percentage of features" ),
false, 0 ) );
75 std::unique_ptr< QgsProcessingFeatureSource > source( parameterAsSource( parameters, QStringLiteral(
"INPUT" ), context ) );
80 std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral(
"OUTPUT" ), context, dest, source->fields(),
85 int method = parameterAsEnum( parameters, QStringLiteral(
"METHOD" ), context );
86 int number = parameterAsInt( parameters, QStringLiteral(
"NUMBER" ), context );
88 long count = source->featureCount();
94 throw QgsProcessingException( QObject::tr(
"Selected number is greater than feature count. Choose a lower value and try again." ) );
100 throw QgsProcessingException( QObject::tr(
"Percentage can't be greater than 100. Choose a lower value and try again." ) );
102 number =
static_cast< int >( std::ceil( number * count / 100 ) );
106 std::random_device randomDevice;
107 std::mt19937 mersenneTwister( randomDevice() );
108 std::uniform_int_distribution<int> fidsDistribution( 0, count );
110 QVector< QgsFeatureId > fids( number );
111 std::generate( fids.begin(), fids.end(), bind( fidsDistribution, mersenneTwister ) );
113 QHash< QgsFeatureId, int > idsCount;
124 QgsFeatureIds ids = QSet< QgsFeatureId >::fromList( idsCount.keys() );
128 while ( fit.nextFeature( f ) )
135 const int count = idsCount.value( f.
id() );
136 for (
int i = 0; i < count; ++i )
143 outputs.insert( QStringLiteral(
"OUTPUT" ), dest );
Wrapper for iterator of features from vector data provider or vector layer.
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
QSet< QgsFeatureId > QgsFeatureIds
Base class for providing feedback from a processing algorithm.
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
A feature sink output for processing algorithms.
This flag indicates, that a primary key field cannot be guaranteed to be unique and the sink should i...
An enum based parameter for processing algorithms, allowing for selection from predefined values...
This class wraps a request for features to a vector layer (or directly its vector data provider)...
Custom exception class for processing related exceptions.
A numeric parameter for processing algorithms.
bool isCanceled() const
Tells whether the operation has been canceled already.
An input feature source (such as vector layers) parameter for processing algorithms.
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
Contains information about the context in which a processing algorithm is executed.