Übung 6

Datenimport

library("readr")
library("sf")
Linking to GEOS 3.12.2, GDAL 3.9.3, PROJ 9.4.1; sf_use_s2() is TRUE
library("lubridate")

Attache Paket: 'lubridate'
Die folgenden Objekte sind maskiert von 'package:base':

    date, intersect, setdiff, union
library("dplyr")

Attache Paket: 'dplyr'
Die folgenden Objekte sind maskiert von 'package:stats':

    filter, lag
Die folgenden Objekte sind maskiert von 'package:base':

    intersect, setdiff, setequal, union
library("ggplot2")
library("terra")
terra 1.7.83
library("tmap")
Breaking News: tmap 3.x is retiring. Please test v4, e.g. with
remotes::install_github('r-tmap/tmap')
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.

Aufgabe 1

fanel = read_sf("Feldaufnahmen_Fanel.gpkg")
head(fanel)
str(fanel)
summary(fanel)
st_geometry_type(fanel)
st_crs(fanel)

Der Datensatz enthält FieldID, Frucht und Geometrie (Polygon)

Der Geometrietyp ist “Polygon”. Die anderen Columns haben den Datentyp numeric (FieldID) und character (Frucht).

Der Datensatz hat das Koordinatensystem CH1903+ / LV95

Aufgabe 2

wildschwein_sommer = wildschwein_BE |> 
  filter(month(DatetimeUTC) %in% c(5,6))

wildschwein_annotate = st_join(wildschwein_sommer, fanel, join = st_intersects)

str(wildschwein_annotate)
sf [15,559 × 9] (S3: sf/tbl_df/tbl/data.frame)
 $ TierID     : chr [1:15559] "002A" "002A" "002A" "002A" ...
 $ TierName   : chr [1:15559] "Sabi" "Sabi" "Sabi" "Sabi" ...
 $ CollarID   : num [1:15559] 12275 12275 12275 12275 12275 ...
 $ DatetimeUTC: POSIXct[1:15559], format: "2015-05-01 00:00:17" "2015-05-01 00:15:25" ...
 $ E          : num [1:15559] 2570093 2570093 2570091 2570059 2570078 ...
 $ N          : num [1:15559] 1205256 1205249 1205253 1205242 1205246 ...
 $ geometry   :sfc_POINT of length 15559; first list element:  'XY' num [1:2] 2570093 1205256
 $ FieldID    : num [1:15559] 0 0 0 0 0 0 0 0 0 0 ...
 $ Frucht     : chr [1:15559] "Wald" "Wald" "Wald" "Wald" ...
 - attr(*, "sf_column")= chr "geometry"
 - attr(*, "agr")= Factor w/ 3 levels "constant","aggregate",..: NA NA NA NA NA NA NA NA
  ..- attr(*, "names")= chr [1:8] "TierID" "TierName" "CollarID" "DatetimeUTC" ...
summary(wildschwein_annotate)
    TierID            TierName            CollarID    
 Length:15559       Length:15559       Min.   :12275  
 Class :character   Class :character   1st Qu.:12275  
 Mode  :character   Mode  :character   Median :13972  
                                       Mean   :13334  
                                       3rd Qu.:13974  
                                       Max.   :13974  
                                                      
  DatetimeUTC                           E                 N          
 Min.   :2015-05-01 00:00:07.00   Min.   :2568174   Min.   :1203130  
 1st Qu.:2015-05-19 06:22:55.50   1st Qu.:2569777   1st Qu.:1203888  
 Median :2015-06-02 08:15:36.00   Median :2570226   Median :1205023  
 Mean   :2015-06-01 21:01:33.67   Mean   :2571037   Mean   :1204942  
 3rd Qu.:2015-06-16 12:30:08.50   3rd Qu.:2572657   3rd Qu.:1205926  
 Max.   :2015-06-30 23:45:16.00   Max.   :2574285   Max.   :1207596  
                                                                     
          geometry        FieldID          Frucht         
 POINT        :15559   Min.   :  0.00   Length:15559      
 epsg:2056    :    0   1st Qu.:  0.00   Class :character  
 +proj=some...:    0   Median :  0.00   Mode  :character  
                       Mean   : 29.69                     
                       3rd Qu.:  0.00                     
                       Max.   :926.00                     
                       NA's   :16                         

Aufgabe 3

wildschwein_annotate = wildschwein_annotate |> 
  mutate(hour = hour(DatetimeUTC))

crop_hour_dist = wildschwein_annotate |> 
  group_by(TierID, hour, Frucht) |> 
  summarise(n = n(), .groups = "drop")

crop_hour_percent = crop_hour_dist |> 
  group_by(TierID, hour) |> 
  mutate(percent = 100* n/sum(n)) |> 
  ungroup()

ggplot(crop_hour_percent, aes(x = hour, y = percent, fill = Frucht)) +
  geom_col(position = "stack", width = 0.9) +
  facet_wrap(~ TierID, nrow = 1) +
  scale_y_continuous(labels = scales::percent_format(scale = 1)) +
  labs(
    title = "Prozentuale Nutzung verschiedener Flächen durch Wildschweine",
    x = "Zeit (gerundet auf die Stunde)",
    y = "Prozent",
    fill = "Flächentyp"
  ) +
  theme_minimal()

Aufgabe 4

vegetation = rast("vegetationshoehe_LFI.tif")
plot(vegetation, main = "Vegetationshöhe")

tm_shape(vegetation) +
  tm_raster(style = "quantile", palette = "Greens", title = "Vegetationshöhe") +
  tm_layout(main.title ="Vegetationshöhe", legend.outside = TRUE)
stars object downsampled to 1149 by 871 cells. See tm_shape manual (argument raster.downsample)

Aufgabe 5

veg_werte = extract(vegetation, wildschwein_annotate)
wildschwein_annotate_veg = wildschwein_annotate |> 
  mutate(vegetation_LFI = veg_werte$vegetationshoehe_LFI)

wildschwein_classified <- wildschwein_annotate_veg |>
  mutate(
    hour = hour(DatetimeUTC),
    veg_height_class = cut(
      vegetation_LFI,
      breaks = c(-Inf, 1, 5, 10, 20, Inf),
      labels = c("<1 m", "1–5 m", "5–10 m", "10–20 m", ">20 m")
    )
  )

veg_hour_percent <- wildschwein_classified |>
  group_by(TierID, hour, veg_height_class) |>
  summarise(n = n(), .groups = "drop") |>
  group_by(TierID, hour) |>
  mutate(percent = 100 * n / sum(n)) |>
  ungroup()

ggplot(veg_hour_percent, aes(x = hour, y = percent, fill = veg_height_class)) +
  geom_col(position = "stack", width = 0.95) +
  facet_wrap(~ TierID, nrow = 1) +
  scale_y_continuous(labels = scales::percent_format(scale = 1)) +
  scale_fill_brewer(palette = "YlGnBu", name = "Vegetationshöhe") +
  labs(
    title = "Aufenthalt von Wildschweinen in Vegetationshöhenklassen über den Tagesverlauf",
    x = "Zeit (Stunde)",
    y = "Prozentualer Anteil pro Stunde"
  ) +
  theme_minimal()