I have a point layer and I wish to extract a random sample of these points across the dataset based on a given attribute.
For example I would like to select randomly 25 points with attribute "A"
, 50 points with attribute "B"
, 40 points that are attribute "C"
.
The selected samples must also be well distributed spatially and not all clustered together. How to achieve it in QGIS?
-
3Does this answer your question? Is the QGIS "Random selection within subset" selecting spatially random or randomly from the attribute table?e-shirt– e-shirt2022年03月11日 07:26:28 +00:00Commented Mar 11, 2022 at 7:26
-
1A stratified random sample cannot be achieved with a simple random selection. If you want a two-variable stratified selection you'd need organize for that.Vince– Vince2022年03月11日 12:22:48 +00:00Commented Mar 11, 2022 at 12:22
2 Answers 2
To select randomly points you find the function "random selection" and "random selection within subsets" in the toolbox under Vector/Research tools (or just search after random selection) --> no new layer is generated by this algorith.
Another function is "random extract", in this case you can extract a number of feature out of the selected features --> new layer is generated by this algorith.
Solution attempt for QGIS
Is the QGIS "Random selection within subset" selecting spatially random or randomly from the attribute table? shows, that the selection is not made spatially, but randomly from the attribute table, so features will not be equally spatially distributed. There might be solutions using plugins or some neat Python code, but I cannot provide that. If a random selection by attribute does suffice, continue the following steps.
There are different methods to get your sample data: random extraction or random selection by attributes or expressions. You can find them in the toobox under 'vector selection'.
You have to decide wether you want to extract your sample to a new layer and continue your work there or want to keep all data together and just work with a selection within your layer.
To achieve your differentiation by attribute, I'd suggest to first select all features with attribute "A" using 'select features by value' (shortcut key F3 should be assigned by default).
Next you use 'random extract' from the toolbox. Choose your layer and make sure you check the "use only selected features" checkbox:
enter image description here
Then choose Method 'Number Of Features' and type in your desired number.
Then run your process.