---
title: "Maritime Pirate Attacks"
author: "François Pelletier"
format: pdf
---

```{r}
library(tidyverse)
library(lubridate)
library(sf)
library(leaflet)
# library(dataReporter) -- not available in this environment
```
```{r}
source("importer_donnees_csv.R")
source("pirateIcon.R")
```

```{r}
glimpse(pirate_attacks_sf, width = 67)
```
```{r}
pirate_attacks_sf_ss <- pirate_attacks_sf %>%
  filter(data_source == "imb") %>%
select(
  date,
  attack_type,
  shore_distance,
  vessel_type,
  vessel_status
) %>%
  replace_na(list(vessel_status="Underway")) %>%
  mutate(month_event = month(date),
         year_event = year(date))
```

## Statistiques descriptives

```{r}
# makeDataReport(pirate_attacks_sf_ss, replace = TRUE) -- dataReporter not available
```
## Exporter les types de navires

```{r}
pirate_attacks_sf_ss %>%
  group_by(vessel_type) %>%
  st_set_geometry(NULL) %>%
  count() %>%
  arrange(desc(n)) %>%
  write_csv("vessel_types.csv")
```

## Traitement manuel
Je crée manuellement des catégories plus larges, puis je réimporte

```{r}
custom_vessel_categories <-
  read_csv(
    "data/csv/custom_vessel_categories.csv",
    col_types = cols(vessel_type = col_character(),
                     vessel_category = col_character())
  )
```


```{r}
pirate_attacks_sf_ss2 <- pirate_attacks_sf_ss %>% 
  left_join(custom_vessel_categories)
```

```{r}
pirate_attacks_sf_ss2 %>% sf::write_sf("data/geojson/pirate_attacks_sf_ss2.geojson")
```

## Filtrage

```{r}
pirate_attacks_sf_maps <- pirate_attacks_sf_ss2 %>%
  filter(year_event == 2015, month_event==1)
```

```{r, eval=FALSE}
m <- leaflet(pirate_attacks_sf_maps) %>%
  addTiles() %>%
  addMarkers(
    popup = paste0(
      "Type: ",
      pirate_attacks_sf_maps$vessel_category,
      "<br>",
      "Statut: ",
      pirate_attacks_sf_maps$vessel_status,
      "<br>",
      "Type d'attaque: ",
      pirate_attacks_sf_maps$attack_type,
      "<br>",
      "Distance de la côte: ",
      round(pirate_attacks_sf_maps$shore_distance, 2),
      "km",
      "<br>",
      "Date: ",
      pirate_attacks_sf_maps$date
    ),
    icon = pirateIcon,
    clusterOptions = markerClusterOptions()
  )
```
