Exercises

library("readr")
library("sf")


wildschwein_BE <- read_delim("data/wildschwein_BE_2056.csv", ",") |>
    st_as_sf(coords = c("E", "N"), crs = 2056, remove = FALSE)

task 1

What information does the dataset contain? information on different occupation (crops) of fields

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? characters (frucht) and numeric (fieldID)

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

fanel <- read_sf("data/Feldaufnahmen_Fanel.gpkg")

head(fanel)
Simple feature collection with 6 features and 2 fields
Geometry type: POLYGON
Dimension:     XY
Bounding box:  xmin: 2570249 ymin: 1202176 xmax: 2571719 ymax: 1203436
Projected CRS: CH1903+ / LV95
# A tibble: 6 × 3
  FieldID Frucht                                                            geom
    <dbl> <chr>                                                    <POLYGON [m]>
1       1 Roggen ((2570914 1202743, 2570917 1202749, 2570985 1202833, 2571294 1…
2       0 <NA>   ((2570893 1202758, 2570893 1202758, 2570959 1202845, 2570985 1…
3       0 <NA>   ((2570868 1202776, 2570872 1202781, 2570913 1202828, 2570946 1…
4       2 Wiese  ((2570882 1203234, 2570641 1202974, 2570630 1202983, 2570606 1…
5       3 Weide  ((2570249 1203116, 2570371 1203328, 2570481 1203197, 2570390 1…
6       5 Weide  ((2570378 1203320, 2570466 1203436, 2570552 1203289, 2570481 1…

task 2

library(lubridate)
library(dplyr)
library(ggplot2)

wildschwein_summer <- wildschwein_BE |> 
  filter(month(DatetimeUTC) >= 5,
    month(DatetimeUTC) <= 6)

ggplot() +
  geom_sf(data = fanel, aes(fill = Frucht)) +
  geom_sf(data = wildschwein_summer,
          color = "black",
          size = 0.3) 

wildschwein_fanel <- st_join(wildschwein_summer, fanel)

head(wildschwein_fanel)
Simple feature collection with 6 features and 8 fields
Geometry type: POINT
Dimension:     XY
Bounding box:  xmin: 2570059 ymin: 1205242 xmax: 2570096 ymax: 1205256
Projected CRS: CH1903+ / LV95
# A tibble: 6 × 9
  TierID TierName CollarID DatetimeUTC                E        N
  <chr>  <chr>       <dbl> <dttm>                 <dbl>    <dbl>
1 002A   Sabi        12275 2015-05-01 00:00:17 2570093. 1205256.
2 002A   Sabi        12275 2015-05-01 00:15:25 2570093. 1205249.
3 002A   Sabi        12275 2015-05-01 00:30:15 2570091. 1205253.
4 002A   Sabi        12275 2015-05-01 00:45:15 2570059. 1205242.
5 002A   Sabi        12275 2015-05-01 01:00:30 2570078. 1205246.
6 002A   Sabi        12275 2015-05-01 01:15:43 2570096. 1205256.
# ℹ 3 more variables: geometry <POINT [m]>, FieldID <dbl>, Frucht <chr>

task 3

wildschwein_fanel <- wildschwein_fanel |>
  mutate(hour = hour(DatetimeUTC))

crop_hour <- wildschwein_fanel |>
  filter(!is.na(Frucht)) |>
  count(TierName, hour, Frucht) |>
  group_by(TierName,hour) |>
  mutate(percent = n / sum(n) * 100)


ggplot(crop_hour, aes(x = hour, y = percent, fill = Frucht)) +
  geom_col(position = "stack") +
  facet_wrap(~ TierName) +
  labs(y = "Percent of locations", x = "Hour of day") +
  theme_minimal()

task 4

library(terra)
library(tmap)

vhm <- rast("data/vegetationshoehe_LFI.tif")

plot(vhm)

tm_shape(vhm) +
  tm_raster(title = "Vegetation height") +
  tm_layout(legend.outside = TRUE)

task 5

wild_vect <- vect(wildschwein_summer)
vhm_values <- extract(vhm, wild_vect)

wildschwein_vhm <- cbind(wildschwein_summer, vhm_values)

head(wildschwein_vhm)
Simple feature collection with 6 features and 8 fields
Geometry type: POINT
Dimension:     XY
Bounding box:  xmin: 2570059 ymin: 1205242 xmax: 2570096 ymax: 1205256
Projected CRS: CH1903+ / LV95
  TierID TierName CollarID         DatetimeUTC       E       N ID
1   002A     Sabi    12275 2015-05-01 00:00:17 2570093 1205256  1
2   002A     Sabi    12275 2015-05-01 00:15:25 2570093 1205249  2
3   002A     Sabi    12275 2015-05-01 00:30:15 2570091 1205253  3
4   002A     Sabi    12275 2015-05-01 00:45:15 2570059 1205242  4
5   002A     Sabi    12275 2015-05-01 01:00:30 2570078 1205246  5
6   002A     Sabi    12275 2015-05-01 01:15:43 2570096 1205256  6
  vegetationshoehe_LFI                geometry
1                 8.74 POINT (2570093 1205256)
2                20.62 POINT (2570093 1205249)
3                11.06 POINT (2570091 1205253)
4                26.15 POINT (2570059 1205242)
5                17.48 POINT (2570078 1205246)
6                13.17 POINT (2570096 1205256)
plot(wildschwein_vhm["vegetationshoehe_LFI"])

wildschwein_vhm <- wildschwein_vhm |>
  mutate(veg_class = cut(vegetationshoehe_LFI,
                         breaks = c(0, 2, 5, 10, 20, Inf),
                         labels = c("0-2", "2-5", "5-10", "10-20", ">20")))

vhm_percent <- wildschwein_vhm |>
  filter(!is.na(veg_class)) |>
  count(TierName, veg_class) |>
  group_by(TierName) |>
  mutate(percent = n / sum(n) * 100)


ggplot(vhm_percent, aes(x = veg_class, y = percent, fill = veg_class)) +
  geom_col() +
  facet_wrap(~ TierName) +
  theme_minimal() +
  labs(x = "Vegetation height class",
       y = "Percent of locations") +
  guides(fill = "none")