---
title: "Quickstart example"
author: "Georg Rüppel"
date: "`r Sys.Date()`"
output:
  rmarkdown::html_vignette:
    toc: yes
vignette: |
  %\VignetteIndexEntry{Quickstart example}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

This is a quickstart guide to model and visualize animal movements from radio-telemetry data using the `movetrack` package.

```{r, message=FALSE}
library(movetrack)
library(ggplot2)
theme_set(theme_bw(base_size = 15))

# Load example data
data(motusData)
```	

## Estimate locations
The first step involves estimating animal locations based on antenna bearings and signal strength at two-minute intervals using the `locate` function.

```{r locate}
loc <- locate(motusData, dTime = 2)
```

## Model flight paths
Next, we model the animal's flight paths using Markov Chain Monte Carlo (MCMC) with four parallel chains utilising the `track` function and return a summary of the results.

```{r model, warning=FALSE, message=FALSE}
fit <- track(loc, parallel_chains = 4, refresh = 1e3)
```

```{r, eval=FALSE}
fit
```

<details>
<summary>Click to expand</summary>
```{r, echo=FALSE}
fit
```
</details> 

## Plot flight speed

We can plot the results per individual and output variable using the `plot` function:

```{r plot, warning=FALSE}
plot(fit, id = 49237)
```

## Plot flight paths

We can visualise the results on a map using the `mapTrack` function:

```{r map, fig.cap="Modelled movement trajectories per individual. Posterior means are shown together with 50 posterior draws, circles on the map indicate receiver locations with detections of the animals."}
mapTrack(fit) +
    geom_point(aes(recvDeployLon, recvDeployLat), data = motusData)
```

We could also create a Leaflet map using the following code:

```{r leaflet}
library(sfheaders)
library(leaflet)

# Extract draws
draws <- getDraws(fit) |>
    sf_linestring("lon", "lat", linestring_id = "tID")

# Leaflet map
fit |>
    as.data.frame() |>
    sf_linestring("lon", "lat", linestring_id = "ID") |>
    leaflet() |>
    addTiles() |>
    addPolylines(data = draws, color = "grey", weight = 1, opacity = 0.2) |>
    addPolylines(color = ~ c("orange", "blue"))
```
