## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  eval = requireNamespace("sf", quietly = TRUE)
)

## ----setup, message = FALSE---------------------------------------------------
library(redlist)

## ----data---------------------------------------------------------------------
occ <- data.frame(
  longitude = c(2.10, 2.62, 3.01, 2.44, 2.90, 1.83, 2.25, 3.14, 1.97, 2.71),
  latitude = c(9.12, 9.53, 9.04, 9.81, 9.33, 9.62, 10.10, 9.45, 9.90, 9.20)
)
head(occ)

## ----eoo----------------------------------------------------------------------
rl_eoo(occ)

## ----eoo-few------------------------------------------------------------------
rl_eoo(data.frame(longitude = c(2.1, 2.6), latitude = c(9.1, 9.5)))

## ----aoo----------------------------------------------------------------------
rl_aoo(occ)

## ----sf-----------------------------------------------------------------------
pts <- sf::st_as_sf(occ, coords = c("longitude", "latitude"), crs = 4326)
rl_aoo(pts)

## ----map, fig.width = 6, fig.height = 5---------------------------------------
eoo_poly <- rl_eoo(occ)
aoo_poly <- rl_aoo(occ)
pts <- sf::st_as_sf(occ, coords = c("longitude", "latitude"), crs = 4326)

plot(sf::st_geometry(eoo_poly), border = "steelblue", lwd = 2,
     main = "EOO hull and AOO cells")
plot(sf::st_geometry(aoo_poly), col = "#f4a58255", border = "tomato", add = TRUE)
plot(sf::st_geometry(pts), pch = 20, add = TRUE)

## ----combine------------------------------------------------------------------
eoo <- rl_eoo(occ)
aoo <- rl_aoo(occ)
data.frame(
  metric = c("EOO", "AOO"),
  area_km2 = c(eoo$area_km2, aoo$area_km2),
  category = c(eoo$category_b1, aoo$category_b2)
)

