Compute distance to native ranges

RAVeM

Author

Adrian García-Rodríguez, David Umhauer & Marcelo Araya-Salas

Published

October 27, 2025

Purpose

  • Compute distance to native ranges for species occurrences in mountains

First we load the required packages:

Code
# install packages if not installed
if (!requireNamespace("sketchy", quietly = TRUE)) {
  install.packages("sketchy")
}

packages <- c("terra", "data.table", "doParallel", "sf", "sp", "dplyr", "rgdal")

# install/ load packages
sketchy::load_packages(packages = packages)
Warning: package 'terra' was built under R version 4.4.3
Warning: package 'data.table' was built under R version 4.4.3
Warning: package 'sf' was built under R version 4.4.3

1 Get native ranges

This module subsets IUCN species range maps to retain only those species with mountain occurrence records. We will work on the amphibian occurrences data from previous tutorials as an example.

2 Load data

We read the amphibian occurrences data to extract the species names:

Code
# Load occurrences to extract spp names
# Read only needed columns (Here we only need the species names)
amphibians_in_mountains_df <- fread(file.path(local_folder, "amphibians_in_mountains.csv"),
  select = c("taxon"))  

# Keep only valid names Remove NA values (total 82 spp) 
spp.names <- unique(na.omit(amphibians_in_mountains_df$taxon))  

3 Load IUCN species distribution maps

Here we load the amphibian distribution maps from IUCN. Note that amphibian maps are split into two shapefiles, so we will need to process both files and then combine them:

Code
amphs1 <- vect(paste0("../INPUTS/IUCNpolygsRAW/AMPHIBIANS_PART1.shp"))
amphs2 <- vect(paste0("../INPUTS/IUCNpolygsRAW/AMPHIBIANS_PART1.shp"))

filtered_amphs1 <- amphs1[amphs1$sci_name %in% spp.names, ]
filtered_amphs2 <- amphs2[amphs2$sci_name %in% spp.names, ]

filtered_amphs <- list(filtered_amphs1, filtered_amphs2)

We now keep only the species with mountain occurrence records and homogenize the attribute tables in order to combine them:

Code
# Identify all unique column names across all SpatVectors
all_colnames <- unique(unlist(lapply(filtered_amphs, names)))

# Find intersection of column names
common_cols <- Reduce(intersect, lapply(filtered_amphs, names))

# Subset all vectors to common columns and combine
filtered_amphs <- do.call(rbind, lapply(filtered_amphs, function(v) v[, common_cols]))

spp_polygs <- unique(filtered_amphs$sci_name)

3.1 Save results

The results can be saved as shapefiles for further analysis. Here we save the amphibian polygons:

Code
# Save as shapefile (creates 3 files: .shp, .shx, .dbf)
writeVector(filtered_amphs, "../INPUTS/amphibian_distribution_polygons.shp",
            filetype = "ESRI Shapefile", overwrite=TRUE)

save.image("3_SubsetIUCNpolygs.RData")

4 Estimate distance to native ranges

This section calculates the minimum distance from each occurrence record to the species’ native range polygon. First we need to load the function ‘distance_to_native’ to calculate distances. The function is hosted at the figshare repository of this project:

Code
source("https://figshare.com/ndownloader/files/58805263")

Now we can estimate the distance to native ranges for all species in the dataset. This process can take some time depending on the number of species and records:

Code
# Adjust object names for analysis
occs.df <- Dasco.occs2
sp.names <- unique(occs.df$taxon)

dist_to_native_list <- pblapply(1:length(sp.names), distance_to_native)

# Remove NULL entries from failed processing
dist_to_native_list <- dist_to_native_list[!sapply(dist_to_native_list, is.null)]

# Combine successful results
dist_to_native_df <- do.call(rbind, dist_to_native_list)

The output data frame ‘dist_to_native_df’ contains the original occurrence data along with a new column ‘min_distance_nat_range’ indicating the minimum distance (in kilometers) from each record to the nearest native range polygon.

4.1 Extract alien records by distance thresholds

Users can now set a distance threshold to determine which records to consider as alien. For example, to consider records beyond 50 km from the native range as alien:

Code
# Alien records beyond 50 km from native range
dist_to_native_df$status <- ifelse(dist_to_native_df$min_distance_nat_range <= 50,
                                "native",
                                "non-native")

Reference

García-Rodríguez, A., Lenzner, B., Velasco, J.A. et al. The global distribution patterns of alien vertebrate richness in mountains. Nat Commun 16, 1977 (2025). https://doi.org/10.1038/s41467-025-57214-w

Session information

Click to see
Warning in system2("quarto", "-V", stdout = TRUE, env = paste0("TMPDIR=", :
running command '"quarto"
TMPDIR=C:/Users/garciaa/AppData/Local/Temp/Rtmp6XHWjJ/file16cc57daa20 -V' had
status 1
─ Session info ───────────────────────────────────────────────────────────────
 setting  value
 version  R version 4.4.2 (2024-10-31 ucrt)
 os       Windows 11 x64 (build 22631)
 system   x86_64, mingw32
 ui       RTerm
 language (EN)
 collate  English_Austria.utf8
 ctype    English_Austria.utf8
 tz       Europe/Vienna
 date     2025-10-27
 pandoc   3.2 @ C:/Program Files/RStudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown)
 quarto   NA @ C:\\PROGRA~1\\RStudio\\RESOUR~1\\app\\bin\\quarto\\bin\\quarto.exe

─ Packages ───────────────────────────────────────────────────────────────────
 package       * version date (UTC) lib source
 cachem          1.1.0   2024-05-16 [2] CRAN (R 4.4.2)
 class           7.3-22  2023-05-03 [2] CRAN (R 4.4.2)
 classInt        0.4-11  2025-01-08 [2] CRAN (R 4.4.2)
 cli             3.6.3   2024-06-21 [1] CRAN (R 4.4.2)
 codetools       0.2-20  2024-03-31 [2] CRAN (R 4.4.2)
 crayon          1.5.3   2024-06-20 [2] CRAN (R 4.4.2)
 data.table    * 1.17.8  2025-07-10 [1] CRAN (R 4.4.3)
 DBI             1.2.3   2024-06-02 [2] CRAN (R 4.4.2)
 devtools        2.4.5   2022-10-11 [1] CRAN (R 4.4.2)
 digest          0.6.37  2024-08-19 [2] CRAN (R 4.4.2)
 doParallel    * 1.0.17  2022-02-07 [2] CRAN (R 4.4.2)
 dplyr         * 1.1.4   2023-11-17 [2] CRAN (R 4.4.2)
 e1071           1.7-16  2024-09-16 [2] CRAN (R 4.4.2)
 ellipsis        0.3.2   2021-04-29 [1] CRAN (R 4.4.2)
 evaluate        1.0.3   2025-01-10 [2] CRAN (R 4.4.2)
 fastmap         1.2.0   2024-05-15 [2] CRAN (R 4.4.2)
 foreach       * 1.5.2   2022-02-02 [2] CRAN (R 4.4.2)
 fs              1.6.5   2024-10-30 [2] CRAN (R 4.4.2)
 generics        0.1.3   2022-07-05 [2] CRAN (R 4.4.2)
 glue            1.8.0   2024-09-30 [2] CRAN (R 4.4.2)
 htmltools       0.5.8.1 2024-04-04 [1] CRAN (R 4.4.3)
 htmlwidgets     1.6.4   2023-12-06 [1] CRAN (R 4.4.3)
 httpuv          1.6.15  2024-03-26 [1] CRAN (R 4.4.2)
 iterators     * 1.0.14  2022-02-05 [2] CRAN (R 4.4.2)
 jsonlite        2.0.0   2025-03-27 [1] CRAN (R 4.4.3)
 KernSmooth      2.23-24 2024-05-17 [2] CRAN (R 4.4.2)
 knitr           1.49    2024-11-08 [2] CRAN (R 4.4.2)
 later           1.4.1   2024-11-27 [1] CRAN (R 4.4.2)
 lattice         0.22-6  2024-03-20 [2] CRAN (R 4.4.2)
 lifecycle       1.0.4   2023-11-07 [2] CRAN (R 4.4.2)
 magrittr        2.0.3   2022-03-30 [2] CRAN (R 4.4.2)
 memoise         2.0.1   2021-11-26 [2] CRAN (R 4.4.2)
 mime            0.13    2025-03-17 [1] CRAN (R 4.4.3)
 miniUI          0.1.1.1 2018-05-18 [1] CRAN (R 4.4.2)
 packrat         0.9.3   2025-06-16 [1] CRAN (R 4.4.2)
 pillar          1.11.0  2025-07-04 [1] CRAN (R 4.4.3)
 pkgbuild        1.4.6   2025-01-16 [1] CRAN (R 4.4.2)
 pkgconfig       2.0.3   2019-09-22 [2] CRAN (R 4.4.2)
 pkgload         1.4.0   2024-06-28 [1] CRAN (R 4.4.2)
 profvis         0.4.0   2024-09-20 [1] CRAN (R 4.4.2)
 promises        1.3.2   2024-11-28 [1] CRAN (R 4.4.2)
 proxy           0.4-27  2022-06-09 [2] CRAN (R 4.4.2)
 purrr           1.0.2   2023-08-10 [2] CRAN (R 4.4.2)
 R6              2.6.1   2025-02-15 [1] CRAN (R 4.4.2)
 Rcpp            1.1.0   2025-07-02 [1] CRAN (R 4.4.3)
 remotes         2.5.0   2024-03-17 [2] CRAN (R 4.4.2)
 rgdal         * 1.6-7   2023-05-31 [1] R-Forge (R 4.3.0)
 rlang           1.1.5   2025-01-17 [1] CRAN (R 4.4.3)
 rmarkdown       2.29    2024-11-04 [2] CRAN (R 4.4.2)
 rstudioapi      0.17.1  2024-10-22 [2] CRAN (R 4.4.2)
 sessioninfo     1.2.3   2025-02-05 [1] CRAN (R 4.4.2)
 sf            * 1.0-21  2025-05-15 [1] CRAN (R 4.4.3)
 shiny           1.11.1  2025-07-03 [1] CRAN (R 4.4.3)
 sketchy         1.0.5   2025-01-16 [1] CRAN (R 4.4.3)
 sp            * 2.2-0   2025-02-01 [1] CRAN (R 4.4.2)
 stringi         1.8.4   2024-05-06 [2] CRAN (R 4.4.0)
 stringr         1.5.1   2023-11-14 [2] CRAN (R 4.4.2)
 terra         * 1.8-29  2025-02-26 [1] CRAN (R 4.4.3)
 tibble          3.3.0   2025-06-08 [1] CRAN (R 4.4.3)
 tidyselect      1.2.1   2024-03-11 [2] CRAN (R 4.4.2)
 units           0.8-5   2023-11-28 [2] CRAN (R 4.4.2)
 urlchecker      1.0.1   2021-11-30 [1] CRAN (R 4.4.2)
 usethis         3.1.0   2024-11-26 [1] CRAN (R 4.4.2)
 vctrs           0.6.5   2023-12-01 [2] CRAN (R 4.4.2)
 xaringanExtra   0.8.0   2024-05-19 [1] CRAN (R 4.4.3)
 xfun            0.50    2025-01-07 [2] CRAN (R 4.4.2)
 xtable          1.8-4   2019-04-21 [1] CRAN (R 4.4.2)
 yaml            2.3.10  2024-07-26 [2] CRAN (R 4.4.2)

 [1] C:/Users/garciaa/AppData/Local/R/win-library/4.4
 [2] C:/Program Files/R/R-4.4.2/library
 * ── Packages attached to the search path.

──────────────────────────────────────────────────────────────────────────────