Week 6: Tasks and Inputs

library("readr")
library("sf")
Warning: Paket 'sf' wurde unter R Version 4.5.3 erstellt
Linking to GEOS 3.14.1, GDAL 3.12.1, PROJ 9.7.1; sf_use_s2() is TRUE
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.2.1     ✔ purrr     1.2.2
✔ forcats   1.0.1     ✔ stringr   1.6.0
✔ ggplot2   4.0.3     ✔ tibble    3.3.1
✔ lubridate 1.9.5     ✔ tidyr     1.3.2
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(ggplot2)
library(lubridate)


wildschwein_BE <- read_delim("wildschwein_BE_2056.csv", ",") |>
    st_as_sf(coords = c("E", "N"), crs = 2056, remove = FALSE)
Rows: 51246 Columns: 6
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr  (2): TierID, TierName
dbl  (3): CollarID, E, N
dttm (1): DatetimeUTC

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

Tasks 1: Import and visualize spatial data

Since Feldaufnahmen_Fanel.gpkg is a vector dataset, you can import it using read_sf(). Explore this dataset in R to answer the following questions:

  • What information does the dataset contain?

  • What is the geometry type of the dataset (possible types are: Point, Lines and Polygons)? Polygons

  • What are the data types of the other columns? nummeric, character

  • What is the coordinate system of the dataset? CH1903+ / LV95

fanel <- read_sf("Feldaufnahmen_Fanel.gpkg")

Task 2: Annotate Trajectories from vector data

We would like to know what crop was visited by which wild boar, and at what time. Since the crop data is most relevant in summer, filter your wild boar data to the months may to june first and save the output to a new variable. Overlay the filtered dataset with your fanel data to verify the spatial overlap.

To sematically annotate each wild boar location with crop information, you can use a spatial join with the function st_join(). Do this and explore your annotated dataset.

min(wildschwein_BE$DatetimeUTC)
[1] "2014-08-22 21:00:12 UTC"
max(wildschwein_BE$DatetimeUTC)
[1] "2015-07-27 11:00:14 UTC"
from <- as.POSIXct("2015-05-01", tz = "UCT")
to <- as.POSIXct("2015-06-30", tz = "UCT")

wildschwein_filter <- wildschwein_BE |> 
  filter(DatetimeUTC >= from,
         DatetimeUTC < to)

To sematically annotate each wild boar location with crop information, you can use a spatial join with the function st_join(). Do this and explore your annotated dataset.

wildschwein_filter <- st_join(wildschwein_filter, fanel)

Task 3: Explore annotated trajectories

Think of ways you could visually explore the spatio-temporal patterns of wild boar in relation to the crops. In our example below we visualize the percentage of samples in a given crop per hour.

wildschwein_filter <- wildschwein_filter |> 
  mutate(time_rounded = round_date(DatetimeUTC, "hour")) |> 
  mutate(time = hour(time_rounded)) 

wildschwein_filter$Frucht <- as.character(wildschwein_filter$Frucht)


keep <- c("Rueben", "Gerste", "Feuchtgebiet", "Wald")

wildschwein_filter$Frucht[!wildschwein_filter$Frucht %in% keep] <- "other"

ggplot(wildschwein_filter, aes(x = time, fill = Frucht)) +
  geom_bar(position = "fill", colour = NA, width = 1) +
  scale_y_continuous(labels = scales::percent) +
  labs(y = "Percentage", x = "Time (rounded to the nearest hour)") +
  facet_wrap(~TierName) +
  theme_bw()

Task 4: Import and visualize vegetationindex (raster data)

In terms of raster data, we have prepared the Vegetation Height Model provided by the Swiss National Forest Inventory (NFI). This dataset contains high resolution information (1x1 Meter) on the vegetation height, which is determined from the difference between the digital surface models DSM and the digital terrain model by swisstopo (swissAlti3D). Buildings are eliminated using a combination of the ground areas of the swisstopo topographic landscape model (TLM) and spectral information from the stereo aerial photos.

Import this dataset using the function rast from the package terra. Visualize the raster data using the base plot function and tmap. We do not recommend using ggplot2 in this case, as is very slow with raster data.

library(terra)
terra 1.9.27

Attache Paket: 'terra'
Das folgende Objekt ist maskiert 'package:tidyr':

    extract
library(tmap)

# import
vegetation <- rast("vegetationshoehe_LFI.tif")
# base plot function
plot(vegetation)

# tmap
tm_shape(vegetation) + 
  tm_raster() + 
  tm_compass() +
  tm_scalebar()
SpatRaster object downsampled to 2753 by 3634 cells.

Task 5: Annotate Trajectories from raster data

Semantically annotate your wild boar locations with the vegetation index . Since you are annotating a vector dataset with information from a raster dataset, you cannot use st_join but need the function extract from the terra package. Read the help on the extract function to see what the function expects.

vegetation_extract <- extract(vegetation, wildschwein_filter)

# join extracted values back to your point attribute table
wildschwein_filter$vegetationshoehe_LFI <- vegetation_extract[,2]

wildschwein_filter |> 
  select(TierID, DatetimeUTC, vegetationshoehe_LFI, geometry) |>
  head(10)
Simple feature collection with 10 features and 3 fields
Geometry type: POINT
Dimension:     XY
Bounding box:  xmin: 2570025 ymin: 1205212 xmax: 2570096 ymax: 1205256
Projected CRS: CH1903+ / LV95
# A tibble: 10 × 4
   TierID DatetimeUTC         vegetationshoehe_LFI          geometry
   <chr>  <dttm>                             <dbl>       <POINT [m]>
 1 002A   2015-05-01 00:00:17                 8.74 (2570093 1205256)
 2 002A   2015-05-01 00:15:25                20.6  (2570093 1205249)
 3 002A   2015-05-01 00:30:15                11.1  (2570091 1205253)
 4 002A   2015-05-01 00:45:15                26.1  (2570059 1205242)
 5 002A   2015-05-01 01:00:30                17.5  (2570078 1205246)
 6 002A   2015-05-01 01:15:43                13.2  (2570096 1205256)
 7 002A   2015-05-01 01:30:17                15.9  (2570079 1205247)
 8 002A   2015-05-01 01:45:18                20.4  (2570094 1205248)
 9 002A   2015-05-01 02:00:45                21.9  (2570044 1205212)
10 002A   2015-05-01 02:15:22                21.5  (2570025 1205217)