task_6

library("sf")
library("readr")
library("tidyverse")
library("dplyr")

wildschwein <- read_delim("wildschwein_BE_2056 (3).csv")

wildschwein_BE <- wildschwein |> 
  st_as_sf(coords = c("E", "N"), crs = 2056, remove = FALSE)

Task 1

# Import Feldaufnahmen_Fanel.gpkg

Feldaufnahmen_Fanel <- read_sf("Feldaufnahmen_Fanel.gpkg")
summary(Feldaufnahmen_Fanel) 
    FieldID         Frucht                     geom    
 Min.   :  0.0   Length:975         POLYGON      :975  
 1st Qu.:112.5   Class :character   epsg:2056    :  0  
 Median :354.0   Mode  :character   +proj=some...:  0  
 Mean   :399.4                                         
 3rd Qu.:722.5                                         
 Max.   :968.0                                         
# Dataset ist ein Polygon und das Koordinatensystem ist epsg: 2056

Feldaufnahmen_Fanel |> ggplot() + 
  geom_sf(aes(color = Frucht)) 

Task 2

# 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 wildboar data to the month may to june first and save the output to a new variable. 

wildschwein_BE

wildschwein_BE |> 
  filter(month(DatetimeUTC) == 5 | month(DatetimeUTC) == 6)

# Overlay the filtered dataset with your fanel data to verify the spatual overlap

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

?st_join()

wildschwein_feldaufnahme <- st_join(wildschwein_BE, Feldaufnahmen_Fanel)

wildschwein_feldaufnahme

Task 3

# Think of what you could viyually 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_feldaufnahme
Simple feature collection with 51246 features and 8 fields
Geometry type: POINT
Dimension:     XY
Bounding box:  xmin: 2568153 ymin: 1202306 xmax: 2575154 ymax: 1207609
Projected CRS: CH1903+ / LV95
# A tibble: 51,246 × 9
   TierID TierName CollarID DatetimeUTC                E        N
 * <chr>  <chr>       <dbl> <dttm>                 <dbl>    <dbl>
 1 002A   Sabi        12275 2014-08-22 21:00:12 2570409. 1204752.
 2 002A   Sabi        12275 2014-08-22 21:15:16 2570402. 1204863.
 3 002A   Sabi        12275 2014-08-22 21:30:43 2570394. 1204826.
 4 002A   Sabi        12275 2014-08-22 21:46:07 2570379. 1204817.
 5 002A   Sabi        12275 2014-08-22 22:00:22 2570390. 1204818.
 6 002A   Sabi        12275 2014-08-22 22:15:10 2570390. 1204825.
 7 002A   Sabi        12275 2014-08-22 22:30:13 2570387. 1204831.
 8 002A   Sabi        12275 2014-08-22 22:45:11 2570381. 1204840.
 9 002A   Sabi        12275 2014-08-22 23:00:27 2570316. 1204935.
10 002A   Sabi        12275 2014-08-22 23:15:41 2570393. 1204815.
# ℹ 51,236 more rows
# ℹ 3 more variables: geometry <POINT [m]>, FieldID <dbl>, Frucht <chr>
wildschwein_feldaufnahme |>
  st_drop_geometry() |>
  mutate(stunde = hour(DatetimeUTC)) |>
  filter(!is.na(Frucht)) |>
  count(stunde, Frucht, name = "samples") |>
  ggplot(aes(x = stunde, y = samples, fill = Frucht)) +
  geom_col() 

Task 4

library("terra")
? rast

vegetationshoehe <- rast("vegetationshoehe_LFI.tif")
vegetationshoehe

View(vegetationshoehe)

plot(vegetationshoehe)
# tmap: 

library("tmap")

tm_shape(vegetationshoehe) + 
  tm_raster()

Task 5

# Semantically annotate your wildboar locations with the vegetation index (similar as you did with the crop data). > st_join > 

# 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. 

? extract


veg_values <- extract(
  vegetationshoehe,wildschwein_BE)

wildschwein_vegetationsindex <- dplyr::bind_cols(
  wildschwein_BE,
  veg_values |> dplyr::select(-ID)
)

wildschwein_vegetationsindex
Simple feature collection with 51246 features and 7 fields
Geometry type: POINT
Dimension:     XY
Bounding box:  xmin: 2568153 ymin: 1202306 xmax: 2575154 ymax: 1207609
Projected CRS: CH1903+ / LV95
# A tibble: 51,246 × 8
   TierID TierName CollarID DatetimeUTC                E        N
 * <chr>  <chr>       <dbl> <dttm>                 <dbl>    <dbl>
 1 002A   Sabi        12275 2014-08-22 21:00:12 2570409. 1204752.
 2 002A   Sabi        12275 2014-08-22 21:15:16 2570402. 1204863.
 3 002A   Sabi        12275 2014-08-22 21:30:43 2570394. 1204826.
 4 002A   Sabi        12275 2014-08-22 21:46:07 2570379. 1204817.
 5 002A   Sabi        12275 2014-08-22 22:00:22 2570390. 1204818.
 6 002A   Sabi        12275 2014-08-22 22:15:10 2570390. 1204825.
 7 002A   Sabi        12275 2014-08-22 22:30:13 2570387. 1204831.
 8 002A   Sabi        12275 2014-08-22 22:45:11 2570381. 1204840.
 9 002A   Sabi        12275 2014-08-22 23:00:27 2570316. 1204935.
10 002A   Sabi        12275 2014-08-22 23:15:41 2570393. 1204815.
# ℹ 51,236 more rows
# ℹ 2 more variables: geometry <POINT [m]>, vegetationshoehe_LFI <dbl>
wildschwein_vegetationsindex |>
  st_drop_geometry() |>
  mutate(stunde = hour(DatetimeUTC)) |>
  ggplot(aes(x = stunde, y = vegetationshoehe_LFI)) +
  geom_boxplot(aes(group = stunde)) 
Warning: Removed 11 rows containing non-finite outside the scale range
(`stat_boxplot()`).

Uebung