26using namespace Qt::StringLiterals;
34 std::vector<QgsFeatureId> allFeats;
35 allFeats.reserve( count );
37 feedback->
pushInfo( QObject::tr(
"Building list of all features..." ) );
42 allFeats.push_back( f.
id() );
44 feedback->
pushInfo( QObject::tr(
"Done." ) );
47 std::random_device randomDevice;
48 std::mt19937 mersenneTwister( randomDevice() );
49 std::uniform_int_distribution<size_t> fidsDistribution;
53 const std::size_t actualFeatureCount = allFeats.size();
54 std::size_t shuffledFeatureCount = count;
55 bool invertSelection =
static_cast<std::size_t
>( count ) > actualFeatureCount / 2;
56 if ( invertSelection )
57 shuffledFeatureCount = actualFeatureCount - count;
59 std::size_t nb = actualFeatureCount;
62 feedback->
pushInfo( QObject::tr(
"Randomly selecting %1 features" ).arg( count ) );
63 auto cursor = allFeats.begin();
64 using difference_type = std::vector<QgsFeatureId>::difference_type;
65 while ( shuffledFeatureCount-- )
71 fidsDistribution.param( std::uniform_int_distribution<size_t>::param_type( 0, nb - 1 ) );
73 std::swap( *cursor, *( cursor +
static_cast<difference_type
>( fidsDistribution( mersenneTwister ) ) ) );
82 if ( invertSelection )
83 for (
auto it = cursor; it != allFeats.end(); ++it )
84 mSelectedFeatureIds.insert( *it );
86 for (
auto it = allFeats.begin(); it != cursor; ++it )
87 mSelectedFeatureIds.insert( *it );
90QString QgsRandomExtractSelectAlgorithmBase::group()
const
92 return QObject::tr(
"Vector selection" );
95QString QgsRandomExtractSelectAlgorithmBase::groupId()
const
97 return u
"vectorselection"_s;
102QString QgsRandomExtractAlgorithm::name()
const
104 return u
"randomextract"_s;
107QString QgsRandomExtractAlgorithm::displayName()
const
109 return QObject::tr(
"Random extract" );
112QStringList QgsRandomExtractAlgorithm::tags()
const
114 return QObject::tr(
"extract,filter,random,number,percentage" ).split(
',' );
117QString QgsRandomExtractAlgorithm::shortDescription()
const
119 return QObject::tr(
"Generates a vector layer that contains only a random subset of the features in an input layer." );
122QString QgsRandomExtractAlgorithm::shortHelpString()
const
125 "This algorithm takes a vector layer and generates a new one that contains only a subset "
126 "of the features in the input layer.\n\n"
127 "The subset is defined randomly, using a percentage or count value to define the total number "
128 "of features in the subset."
137QgsRandomExtractAlgorithm *QgsRandomExtractAlgorithm::createInstance()
const
139 return new QgsRandomExtractAlgorithm();
142void QgsRandomExtractAlgorithm::initAlgorithm(
const QVariantMap & )
145 addParameter(
new QgsProcessingParameterEnum( u
"METHOD"_s, QObject::tr(
"Method" ), QStringList() << QObject::tr(
"Number of features" ) << QObject::tr(
"Percentage of features" ),
false, 0 ) );
153 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, u
"INPUT"_s, context ) );
162 const int method = parameterAsEnum( parameters, u
"METHOD"_s, context );
163 long long number = parameterAsInt( parameters, u
"NUMBER"_s, context );
169 if ( number > count )
170 throw QgsProcessingException( QObject::tr(
"Selected number is greater than feature count. Choose a lower value and try again." ) );
176 throw QgsProcessingException( QObject::tr(
"Percentage can't be greater than 100. Choose a lower value and try again." ) );
178 number =
static_cast<long long>( std::ceil( number * count / 100 ) );
181 sampleFeatureIds( source.get(), number, feedback );
183 feedback->
pushInfo( QObject::tr(
"Adding selected features" ) );
189 return QVariantMap();
198 outputs.insert( u
"OUTPUT"_s, dest );
204QString QgsRandomSelectionAlgorithm::name()
const
206 return u
"randomselection"_s;
209QString QgsRandomSelectionAlgorithm::displayName()
const
211 return QObject::tr(
"Random selection" );
214QStringList QgsRandomSelectionAlgorithm::tags()
const
216 return QObject::tr(
"select,random,number,percentage" ).split(
',' );
219QString QgsRandomSelectionAlgorithm::shortDescription()
const
221 return QObject::tr(
"Randomly selects features from a vector layer." );
224QString QgsRandomSelectionAlgorithm::shortHelpString()
const
227 "This algorithm takes a vector layer and selects a subset of its features. "
228 "No new layer is generated by this algorithm.\n\n"
229 "The subset is defined randomly, using a percentage or count value to define "
230 "the total number of features in the subset."
234QgsRandomSelectionAlgorithm *QgsRandomSelectionAlgorithm::createInstance()
const
236 return new QgsRandomSelectionAlgorithm();
239void QgsRandomSelectionAlgorithm::initAlgorithm(
const QVariantMap & )
242 addParameter(
new QgsProcessingParameterEnum( u
"METHOD"_s, QObject::tr(
"Method" ), QStringList() << QObject::tr(
"Number of features" ) << QObject::tr(
"Percentage of features" ),
false, 0 ) );
250 mInput = parameters.value( u
"INPUT"_s );
251 mTargetLayer = parameterAsVectorLayer( parameters, u
"INPUT"_s, context );
256 const int method = parameterAsEnum( parameters, u
"METHOD"_s, context );
257 long long number = parameterAsInt( parameters, u
"NUMBER"_s, context );
258 const long long count = mTargetLayer->featureCount();
263 if ( number > count )
264 throw QgsProcessingException( QObject::tr(
"Selected number is greater than feature count. Choose a lower value and try again." ) );
270 throw QgsProcessingException( QObject::tr(
"Percentage can't be greater than 100. Choose a lower value and try again." ) );
272 number =
static_cast<long long>( std::ceil( number * count / 100 ) );
276 sampleFeatureIds( mTargetLayer, number, feedback );
278 return QVariantMap();
283 mTargetLayer->selectByIds( mSelectedFeatureIds );
286 outputs.insert( u
"OUTPUT"_s, mInput );
@ 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...
An interface for objects which provide features via a getFeatures method.
virtual QgsFields fields() const =0
Returns the fields associated with features in the source.
virtual QgsCoordinateReferenceSystem sourceCrs() const =0
Returns the coordinate reference system for features in the source.
virtual Qgis::WkbType wkbType() const =0
Returns the geometry type for features returned by this source.
virtual QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const =0
Returns an iterator for the features in the source.
virtual long long featureCount() const =0
Returns the number of features contained in the source, or -1 if the feature count is unknown.
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.
A vector layer output for processing algorithms.
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.
A vector layer (with or without geometry) parameter for processing algorithms.