The goal of grtsdb is to create a spatially balanced sample based on the ‘Generalised Random Tesselation Stratified’ strategy. We store the base schema in an SQLite database to make the sampling reproducible. Sampling the same database with the same parameters yields a stable sample.

Installation

To install the latest stable version use

# activate the INBO r-universe
options(
  repos = c(
    INBO = 'https://inbo.r-universe.dev', CRAN = 'https://cloud.r-project.org'
  )
)
install.packages("grtsdb")

You can install the development version from GitHub with:

# install.packages("remotes")
remotes::install_github("inbo/grtsdb")

Example

This is a basic example.

Connect to a database.

tmp_copy <- tempfile(pattern = "grts", fileext = ".sqlite")
if (system.file("grts.sqlite", package = "grtsdb") != "") {
  file.copy(system.file("grts.sqlite", package = "grtsdb"), tmp_copy)
}
#> [1] TRUE
library(grtsdb)
db <- connect_db(tmp_copy)

To extract a sample, you’ll need to specify the bounding box in projected coordinates and the size of the grid cells.

bbox <- rbind(
  c(0, 32),
  c(0, 32)
)
extract_sample(grtsdb = db, samplesize = 10, bbox = bbox, cellsize = 1)
#>     x1c  x2c ranking
#> 1  22.5 21.5       0
#> 2  26.5  1.5       1
#> 3   3.5 23.5       2
#> 4  10.5  8.5       3
#> 5  24.5 18.5       4
#> 6  16.5  2.5       5
#> 7  10.5 30.5       6
#> 8   9.5  5.5       7
#> 9  24.5 31.5       8
#> 10 27.5 12.5       9

Repeating the sample yields the same results.

extract_sample(grtsdb = db, samplesize = 10, bbox = bbox, cellsize = 1)
#>     x1c  x2c ranking
#> 1  22.5 21.5       0
#> 2  26.5  1.5       1
#> 3   3.5 23.5       2
#> 4  10.5  8.5       3
#> 5  24.5 18.5       4
#> 6  16.5  2.5       5
#> 7  10.5 30.5       6
#> 8   9.5  5.5       7
#> 9  24.5 31.5       8
#> 10 27.5 12.5       9

You can add legacy sites to the sampling scheme.

legacy <- rbind(
  c(4, 4),
  c(17, 6)
)
add_legacy_sites(legacy, bbox = bbox, cellsize = 1, grtsdb = db)
extract_legacy_sample(grtsdb = db, samplesize = 10, bbox = bbox, cellsize = 1)
#> Creating index for legacy level 5. May take some time... Done.
#>     x1c  x2c ranking
#> 1  16.5  6.5       0
#> 2   4.5  4.5       1
#> 3   3.5 23.5       2
#> 4  22.5 21.5       3
#> 5  26.5  1.5       4
#> 6   9.5  5.5       5
#> 7  10.5 30.5       6
#> 8  24.5 18.5       7
#> 9  27.5 12.5       8
#> 10  2.5 15.5       9

You can compact the database for storage.

Disconnect the database when done.