Skip to contents

Install and load packages

# pak::pkg_install("makinohub/habistats")
library(habistats)

Set options

Directory containing IUCN shape files:

# Set
options(habistats.iucn_source = "path/to/your/data")

# Check
habistats::iucn_source_path()

You can leave this unset if you want to use the example data included in the package, which was randomly generated to be similar to the IUCN data.

Intermediate data files will be stored in the cache directory:

options(habistats.cache_dir = "~/.cache/habistats-example")

Read IUCN shape files

Non-geometry columns are extracted as a pure data.frame. It may take a while to run them for the first time, but subsequent runs will be very quick because the results are cached.

features = habistats::iucn_spatial_features() |> print()
#> # A tibble: 5 × 28
#>      id_no sci_name       presence origin seasonal compiler yrcompiled citation 
#>      <int> <chr>             <int>  <int>    <int> <chr>         <int> <chr>    
#> 1 19990904 Strato arctica        1      1        1 NWOBHM         2025 habistats
#> 2 20010425 Diva satanica         1      1        1 NWOBHM         2025 habistats
#> 3 20010425 Diva satanica         1      1        1 NWOBHM         2025 habistats
#> 4 19851118 Kai kiske             1      1        1 NWOBHM         2025 habistats
#> 5 19710604 Elp tarkus            5      1        1 NWOBHM         2025 habistats
#> # ℹ 20 more variables: subspecies <chr>, subpop <chr>, source <lgl>,
#> #   island <chr>, tax_comm <chr>, dist_comm <chr>, generalisd <int>,
#> #   legend <chr>, kingdom <chr>, phylum <chr>, class <chr>, order_ <chr>,
#> #   family <chr>, genus <chr>, category <chr>, marine <lgl>, terrestria <lgl>,
#> #   freshwater <lgl>, SHAPE_Leng <dbl>, SHAPE_Area <dbl>

species = habistats::iucn_spatial_species() |> print()
#> # A tibble: 4 × 11
#>   kingdom   phylum class order_ family genus sci_name category marine terrestria
#>   <chr>     <chr>  <chr> <chr>  <chr>  <chr> <chr>    <chr>    <lgl>  <lgl>     
#> 1 METALLICA CHORD… MAMM… CARNI… CANID… Stra… Strato … LC       FALSE  TRUE      
#> 2 METALLICA CHORD… AVES  PASSE… CORVI… Diva  Diva sa… LC       FALSE  TRUE      
#> 3 METALLICA TRACH… MAGN… CUCUR… CUCUR… Kai   Kai kis… LC       FALSE  TRUE      
#> 4 METALLICA CHORD… MAMM… CINGU… CHLAM… Elp   Elp tar… EW       FALSE  TRUE      
#> # ℹ 1 more variable: freshwater <lgl>

Use them to filter the species you want to evaluate.

sn = species |>
  dplyr::filter(phylum == "CHORDATA") |>
  dplyr::pull(sci_name) |>
  print()
#> [1] "Strato arctica" "Diva satanica"  "Elp tarkus"

Preprocess shape files

Split IUCN shape files into separate gpkg files for efficient processing. On a slow storage, it may take a while to run this even for the non-first time.

species_gpkg = habistats::iucn_species_gpkg() |> print()
#> # A tibble: 4 × 2
#>   sci_name       source                                                         
#>   <chr>          <fs::path>                                                     
#> 1 Strato arctica …A/MAMMALIA/CARNIVORA/CANIDAE/Strato/Strato_arctica/source.gpkg
#> 2 Diva satanica  …ATA/AVES/PASSERIFORMES/CORVIDAE/Diva/Diva_satanica/source.gpkg
#> 3 Kai kiske      …OLIOPSIDA/CUCURBITALES/CUCURBITACEAE/Kai/Kai_kiske/source.gpkg
#> 4 Elp tarkus     …A/MAMMALIA/CINGULATA/CHLAMYPHORIDAE/Elp/Elp_tarkus/source.gpkg

Evaluate species ranges

out = species_gpkg |>
  dplyr::filter(sci_name %in% sn) |>
  dplyr::pull("source", "sci_name") |>
  quantify_vector_kgc() |>
  print()
#> # A tibble: 3 × 8
#>   name           perimeter       area abundance richness idx_shannon idx_simpson
#>   <chr>              <dbl>      <dbl>     <int>    <int>       <dbl>       <dbl>
#> 1 Strato arctica  3201738.    3.39e11      9157        3       0.486       0.703
#> 2 Diva satanica   6784114.    8.04e11     18038        5       0.932       0.443
#> 3 Elp tarkus      3854483.    2.49e11      4875        3       0.239       0.881
#> # ℹ 1 more variable: idx_brillouin <dbl>

You may want to save the result to a file.

readr::write_tsv(out, "iucn_range.tsv.gz")