There are various options to create a vector or raster layers with random sample points, including `v.random`

, `r.random`

and `r.random.cells`

. The first two generate random points within the defined region. However, only `r.random`

will respect the mask if set. The `r.random.cells`

creates random points with spatial dependency.

But how to carry out stratified random sample points with e.g. an equal sample sizes per strata? You would need to divide your sample area in the desired strata, and sample each stratum separately. Below are two possible approaches, one using `r.random`

and another using the GRASS add-on `v.random.cover`

.

**Using r.random**

Suppose we want to divide our area of interest in three different strata. On the raster layer ‘STRATA, these are mapped and numbered as 1, 2 and 3. We would then create a mask of stratum 1, create a layer with X random sample points, create a new mask for stratum 2, create a random point layer again, etc. If you have to deal with many strata, this can best be done using a loop.

STRATA = ( 1 2 3 ) for ST in ${STRATA[@]}; do r.mask -o input=a maskcats=$ST r.random input=STRATA vector_output=samplepoints_$ST n=100 done v.patch -e input=`g.mlist type=vect pattern=samplepoints_* \ separator=,` output=samplepoints

In the example, the output is given a s a vector layer. Depending on your further analysis, you can also opt for a raster layerÂ instead of a vector point layer, or both.

~~One problem you are likely to encounter when using r.sample is that it will often return less then the requested number of sample points. One option, if you are creating a vector layer, is to create more points then you need, and later select as sub-set with the required number of sample points~~. Edit: this problem has been solved, the function should return the exact number of points now.

The other option is to (1) create the random sample points, (2) check how many sample points you have and if these are less then the required number, (3) create an additional set of sample points, (4) repeat steps 2 and 3 till you have enough points. This is, if I am correct, the solution the `v.random.cover`

add-on follows.

**Using v.random.cover**

You can find the `v.random.cover`

add-on in the GRASS addon repositories or install it using the `g.extension`

function. It allows you to create random sample points within selected polygons of a vector layer. Strata should be identified by the vector category values. In the example below, I am using the STRATA vector layer.

STRATA = ( 1 2 3 ) for ST in ${STRATA[@]}; do r.mask -o input=a maskcats=$ST v.random.cover input=STRATA output=samplepoints_$ST n=100 done v.patch -e input=`g.mlist type=vect pattern=samplepoints_* \ separator=,` output=samplepoints

An advantage of this script is that it checks the number of sample points created, and will automatically add sample points till the desired number of points have been created.

If you have a map where the strata are identified in one of the columns of your attribute table rather then the vector category values, use `v.reclass`

first to create a new layer where the category values represent the strata.

See my next post for a short explanation how to carry out stratified random sampling in QGIS.

## One thought on “Stratified random sampling in GRASS GIS”