---
title: "Working with spatial data"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Working with spatial data}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

```{r setup}
#| message: false
library(ARUtools)
library(sf)
library(dplyr)
```

Here we'll cover some workflows using spatial data. For a general workflow, best
take a look at the "Getting started with ARUtools" first.


We'll start with our metadata data frame.

```{r}
m <- clean_metadata(project_files = example_files)
m
```

This isn't spatial because we don't actually know where the sites are located.
But our next step is to get our site coordinates.

Let's assume we have a spatial data frame containing our sites and where they 
are located.
```{r}
s <- st_as_sf(example_sites, coords = c("lon", "lat"), crs = 4326)
s
```

Similar to a non-spatial workflow, we'll clean up this list so we can add these sites to our metadata.

```{r}
sites <- clean_site_index(s,
  name_aru_id = "ARU",
  name_site_id = "Sites",
  name_date_time = c("Date_set_out", "Date_removed")
)
sites
```

Note that we still have a spatial data set.

Now let's add this site-related information to our metadata.

```{r}
m <- add_sites(m, sites)
m
```

Again our output is as a spatial data set.

Let's continue by adding times to sunrise/sunset.
```{r}
m <- calc_sun(m)
m
```

All done! And we've retained a spatial data set the entire way.

## Problems
However, sometimes spatial data sets might be trickier to use.

For example, sf spatial data sets cannot have missing coordinates, meaning
that when using the `add_sites()` function, you'll get a warning and a data frame
back if you try to add an incomplete list of sites.

```{r}
m <- clean_metadata(project_files = example_files)

sites <- st_as_sf(example_sites, coords = c("lon", "lat"), crs = 4326) |>
  clean_site_index(
    name_aru_id = "ARU",
    name_site_id = "Sites",
    name_date_time = c("Date_set_out", "Date_removed")
  )

sites <- sites[-1, ] # Omit that first site

m <- add_sites(m, sites)
m
```

To resolve this, either add in the missing site information, 
or omit the files before joining.


```{r}
m <- clean_metadata(project_files = example_files) |>
  filter(date > "2020-05-03") # Filter out recordings that don't match a site

m <- add_sites(m, sites)
m
```

Fixed!

