Downloading observations by region

Regions shown in the Biodiversité Québec atlas portal can be accessed through the ratlas package and be used to filter downloaded observations. Regions are organized by type (ie. Ecological Region, Administrative Region, and Hexagonal Grid) and each region type is organized by scale. It is recommended to explore regions using the Biodiversité Québec Atlas web application to determine the region type and scale of interest.

For this exercices we want to only return the Municipalities of Quebec. Specifically, we will extract observations for the city of Sherbrooke in 2002.

library(ratlas)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following object is masked from 'package:ratlas':
#> 
#>     db_write_table
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

List regions available in the atlas portal

The get_regions() function returns a data frame with the regions available in the atlas portal. The function returns a data frame with the following columns:

  • fid: the unique identifier of the region
  • type: the type of region. Can be one of cadre_eco, admin, and hex
  • scale: integer for the scale of the region. Example : 1 for the province of Quebec, 2 for the administrative regions, 3 for MRC, 4 for the Municipalities
  • name: name of the region. Example : “Quebec”, “Montreal”, “Laurentides”
  • scale_desc: description of the scale.

Running the function without any parameters will return all regions in the atlas. This can be a very large dataset and may take a long time to download. To explore the regions, it is recommended to use the id parameter to return a specific region or the type parameter to return a specific type of region. It is also possible to exclude the geometry from the results by setting the geometry parameter to FALSE.

regions_desc <- get_regions(geometry = FALSE)
head(regions_desc, 20) |> kable()
fid type scale scale_desc name
17 admin 2 Région administrative Montérégie
633273 hex 5 5 km Nord-du-Québec
698412 hex 5 5 km Nord-du-Québec
858059 cadre_eco 4 Niveau 4 Buttons du réservoir Gouin
858060 cadre_eco 2 Niveau 2 Basses-terres du lac Témiscamingue
858086 cadre_eco 4 Niveau 4 Buttons du réservoir Robert-Bourassa
134 admin 4 Territoire autochtone Whapmagoostui
698426 hex 5 5 km Nord-du-Québec
858113 cadre_eco 3 Niveau 3 Plateau septentrional de la Péninsule gaspésienne
859302 cadre_eco 2 Niveau 2 Collines La Vérendrye
859305 cadre_eco 2 Niveau 2 Péninsule de la Gaspésie
698427 hex 5 5 km Nord-du-Québec
698428 hex 5 5 km Nord-du-Québec
698429 hex 5 5 km Nord-du-Québec
859733 cadre_eco 4 Niveau 4 Buttes du lac Mantouchiche
859734 cadre_eco 4 Niveau 4 Buttons du lac Hore
860092 cadre_eco 4 Niveau 4 Plaine bosselée du lac Atachikami
693183 hex 5 5 km Nord-du-Québec
698430 hex 5 5 km Nord-du-Québec
698431 hex 5 5 km Nord-du-Québec

Let’s filter the results to only return the Municipalities of Quebec. And search the results for the city of Sherbrooke.

municipalities_desc <- get_regions(type = "admin", scale = 4, geometry = FALSE)
sherbrooke <- municipalities_desc |> filter(name == "Sherbrooke")
sherbrooke |> kable()
fid type scale scale_desc name
749 admin 4 Municipalité Sherbrooke

Count observations for a region

The database obs_regions_taxa_datasets_counts view contains the number of observations for each region, taxa and datasets. We can query it directly with the db_read_table() function.

sherbrooke_count_response <- db_read_table(
  table_name = "obs_regions_taxa_datasets_counts",
  schema = "atlas_api",
  fid = sherbrooke$fid
  # id_taxa_obs = 735,
  )

# Total number of observations for the city of Sherbrooke can be summed
sherbrooke_count <- sum(sherbrooke_count_response$count_obs)
print (paste("Number of observations in Sherbrooke:", sherbrooke_count))
#> [1] "Number of observations in Sherbrooke: 348534"

Download observations for a region

We can now use the fid value for the city of Sherbrooke to download observations for the year 2002. The get_observations() function returns a data frame with the observations for the specified region. Other filter parameters can be used to further filter the results. For example, we can filter the results to only return observations for a specific year.

sherbrooke_observations <- get_observations(region_fid = sherbrooke$fid, year = 2002)

# Print number of observations
print (paste("Number of observations in Sherbrooke in 2002:", nrow(sherbrooke_observations)))
#> [1] "Number of observations in Sherbrooke in 2002: 2764"