26using namespace Qt::StringLiterals;
38 QHash<QVariant, QgsFeatureIds> idsHash;
49 feedback->
pushInfo( QObject::tr(
"Building features subsets…" ) );
56 idsHash[f.
attribute( fieldIndex )].insert( f.
id() );
59 feedback->
setProgress(
static_cast<double>( i ) * step );
63 std::random_device randomDevice;
64 std::mt19937 mersenneTwister( randomDevice() );
65 std::uniform_int_distribution<std::size_t> fidsDistribution;
67 feedback->
pushInfo( QObject::tr(
"Randomly selecting features within subsets…" ) );
69 step = !idsHash.isEmpty() ? 50.0 /
static_cast<double>( idsHash.size() ) : 1;
71 for (
auto hashIt = idsHash.constBegin(); hashIt != idsHash.constEnd(); ++hashIt )
79 const long long total = subsetIds.size();
80 const long long count = mMethod == 0 ?
static_cast<long long>( value ) : static_cast<long long>( std::ceil( static_cast<double>( total ) * value / 100 ) );
84 feedback->
reportError( QObject::tr(
"Subset '%1' is smaller than requested number of features." ).arg( hashIt.key().toString() ) );
85 mSelectedFeatureIds.unite( subsetIds );
89 std::vector<QgsFeatureId> allSubsetIds( subsetIds.begin(), subsetIds.end() );
90 bool invertSelection = count > total / 2;
91 long long shuffledFeatureCount = invertSelection ? total - count : count;
92 std::size_t nb = allSubsetIds.size();
94 using difference_type = std::vector<QgsFeatureId>::difference_type;
95 auto cursor = allSubsetIds.begin();
96 for (
long long j = 0; j < shuffledFeatureCount; ++j )
103 fidsDistribution.param( std::uniform_int_distribution<std::size_t>::param_type( 0, nb - 1 ) );
104 std::swap( *cursor, *( cursor +
static_cast<difference_type
>( fidsDistribution( mersenneTwister ) ) ) );
109 if ( invertSelection )
111 for (
auto selectIt = cursor; selectIt != allSubsetIds.end(); ++selectIt )
113 mSelectedFeatureIds.insert( *selectIt );
118 for (
auto selectIt = allSubsetIds.begin(); selectIt != cursor; ++selectIt )
120 mSelectedFeatureIds.insert( *selectIt );
126 feedback->
setProgress( 50.0 + (
static_cast<double>( i ) * step ) );
130QString QgsRandomExtractWithinSubsetsAlgorithmBase::group()
const
132 return QObject::tr(
"Vector selection" );
135QString QgsRandomExtractWithinSubsetsAlgorithmBase::groupId()
const
137 return u
"vectorselection"_s;
142QString QgsRandomExtractWithinSubsetsAlgorithm::name()
const
144 return u
"randomextractwithinsubsets"_s;
147QString QgsRandomExtractWithinSubsetsAlgorithm::displayName()
const
149 return QObject::tr(
"Random extract within subsets" );
152QStringList QgsRandomExtractWithinSubsetsAlgorithm::tags()
const
154 return QObject::tr(
"extract,filter,random,number,percentage,subset" ).split(
',' );
157QString QgsRandomExtractWithinSubsetsAlgorithm::shortDescription()
const
159 return QObject::tr(
"Generates a new vector layer that contains only a subset of the features in the input layer." );
162QString QgsRandomExtractWithinSubsetsAlgorithm::shortHelpString()
const
165 "This algorithm takes a vector layer and generates a new one that "
166 "contains only a subset of the features in the input layer.\n\n"
167 "The subset is defined randomly, using a percentage or count value "
168 "to define the total number of features in the subset.\n\n"
169 "The percentage/count value is not applied to the whole layer, but "
170 "instead to each category. Categories are defined according to a "
171 "given attribute, which is also specified as an input parameter "
181QgsRandomExtractWithinSubsetsAlgorithm *QgsRandomExtractWithinSubsetsAlgorithm::createInstance()
const
183 return new QgsRandomExtractWithinSubsetsAlgorithm();
186void QgsRandomExtractWithinSubsetsAlgorithm::initAlgorithm(
const QVariantMap & )
190 addParameter(
new QgsProcessingParameterEnum( u
"METHOD"_s, QObject::tr(
"Method" ), QStringList() << QObject::tr(
"Number of features" ) << QObject::tr(
"Percentage of features" ),
false, 0 ) );
198 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, u
"INPUT"_s, context ) );
202 const QString fieldName = parameterAsString( parameters, u
"FIELD"_s, context );
203 mMethod = parameterAsEnum( parameters, u
"METHOD"_s, context );
204 const double number = parameterAsDouble( parameters, u
"NUMBER"_s, context );
216 if ( number >
static_cast<double>( count ) )
217 throw QgsProcessingException( QObject::tr(
"Selected number is greater than feature count. Choose a lower value and try again." ) );
223 throw QgsProcessingException( QObject::tr(
"Percentage can't be greater than 100. Choose a lower value and try again." ) );
226 sampleFeatureIds( source.get(), number, fieldName, feedback );
228 feedback->
pushInfo( QObject::tr(
"Adding selected features" ) );
231 double step = mSelectedFeatureIds.size() > 0 ? 100.0 /
static_cast<double>( mSelectedFeatureIds.size() ) : 1;
236 return QVariantMap();
239 feedback->
setProgress(
static_cast<double>( i ) * step );
248 outputs.insert( u
"OUTPUT"_s, dest );
254QString QgsRandomSelectionWithinSubsetsAlgorithm::name()
const
256 return u
"randomselectionwithinsubsets"_s;
259QString QgsRandomSelectionWithinSubsetsAlgorithm::displayName()
const
261 return QObject::tr(
"Random selection within subsets" );
264QStringList QgsRandomSelectionWithinSubsetsAlgorithm::tags()
const
266 return QObject::tr(
"select,random,number,percentage,subset" ).split(
',' );
269QString QgsRandomSelectionWithinSubsetsAlgorithm::shortDescription()
const
271 return QObject::tr(
"Randomly selects features from a subset of a vector layer." );
274QString QgsRandomSelectionWithinSubsetsAlgorithm::shortHelpString()
const
277 "This algorithm takes a vector layer and selects a subset of its features. "
278 "No new layer is generated by this algorithm.\n\n"
279 "The subset is defined randomly, using a percentage or count value to define "
280 "the total number of features in the subset.\n\n"
281 "The percentage/count value is not applied to the whole layer, but instead to each category. "
282 "Categories are defined according to a given attribute, which is also specified "
283 "as an input parameter for the algorithm."
287QgsRandomSelectionWithinSubsetsAlgorithm *QgsRandomSelectionWithinSubsetsAlgorithm::createInstance()
const
289 return new QgsRandomSelectionWithinSubsetsAlgorithm();
292void QgsRandomSelectionWithinSubsetsAlgorithm::initAlgorithm(
const QVariantMap & )
296 addParameter(
new QgsProcessingParameterEnum( u
"METHOD"_s, QObject::tr(
"Method" ), QStringList() << QObject::tr(
"Number of features" ) << QObject::tr(
"Percentage of features" ),
false, 0 ) );
304 mInput = parameters.value( u
"INPUT"_s );
305 mTargetLayer = parameterAsVectorLayer( parameters, u
"INPUT"_s, context );
310 const QString fieldName = parameterAsString( parameters, u
"FIELD"_s, context );
311 mMethod = parameterAsEnum( parameters, u
"METHOD"_s, context );
312 const double number = parameterAsDouble( parameters, u
"NUMBER"_s, context );
313 const long long count = mTargetLayer->featureCount() > 0 ? mTargetLayer->featureCount() : 0;
318 if ( number >
static_cast<double>( count ) )
319 throw QgsProcessingException( QObject::tr(
"Selected number is greater than feature count. Choose a lower value and try again." ) );
325 throw QgsProcessingException( QObject::tr(
"Percentage can't be greater than 100. Choose a lower value and try again." ) );
328 sampleFeatureIds( mTargetLayer, number, fieldName, feedback );
330 return QVariantMap();
335 mTargetLayer->selectByIds( mSelectedFeatureIds );
338 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...
@ Double
Double/float values.
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).
QgsFeatureRequest & setFlags(Qgis::FeatureRequestFlags flags)
Sets flags that affect how features will be fetched.
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that 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...
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...
Q_INVOKABLE QVariant attribute(const QString &name) const
Lookup attribute value by attribute name.
bool isCanceled() const
Tells whether the operation has been canceled already.
void setProgress(double progress)
Sets the current progress for the feedback object.
Q_INVOKABLE int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
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.
virtual void reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.
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 vector layer or feature source field parameter for processing algorithms.
A numeric parameter for processing algorithms.
A vector layer (with or without geometry) parameter for processing algorithms.
QSet< QgsFeatureId > QgsFeatureIds