Working with spatial data"

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
#| 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.

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.

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.

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.

m <- add_sites(m, sites)
m

Again our output is as a spatial data set.

Let's continue by adding times to sunrise/sunset.

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.

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.

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!



Try the ARUtools package in your browser

Any scripts or data that you put into this service are public.

ARUtools documentation built on Oct. 9, 2024, 1:07 a.m.