library(tidyverse)
library(lubridate)
library(sf)
library(leaflet)
library(dataReporter)
source("importer_donnees_csv.R")
Avis : One or more parsing issues, see `problems()` for details
glimpse(pirate_attacks_sf, width = 67)
Rows: 7,511
Columns: 15
$ date                 <date> 1993-01-02, 1993-01-04, 1993-01-06,…
$ time                 <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, …
$ attack_type          <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, …
$ location_description <chr> "Hong Kong - Luzon - Hainan", "Hong …
$ nearest_country      <chr> "CHN", "CHN", "CHN", "CHN", "PHL", "…
$ eez_country          <chr> "TWN", "CHN", "TWN", "CHN", "PHL", "…
$ shore_distance       <dbl> 357.5023726, 47.4315725, 280.8118709…
$ shore_longitude      <dbl> 115.825956, 115.825956, 114.302501, …
$ shore_latitude       <dbl> 22.746644, 22.746644, 22.044867, 29.…
$ attack_description   <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, …
$ vessel_name          <chr> "Mv Cosmic Leader", "Mv Tricolor Sta…
$ vessel_type          <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, …
$ vessel_status        <chr> NA, NA, NA, NA, NA, NA, NA, NA, "Anc…
$ data_source          <chr> "mappingpiracy", "mappingpiracy", "m…
$ geometry             <POINT> POINT (116.9667 19.7), POINT (116 …
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

makeDataReport(pirate_attacks_sf_ss, replace = TRUE)
Data report generation is finished. Please wait while your output file is being rendered.

Exporter les types de navires

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

custom_vessel_categories <-
  read_csv(
    "data/csv/custom_vessel_categories.csv",
    col_types = cols(vessel_type = col_character(),
                     vessel_category = col_character())
  )
pirate_attacks_sf_ss2 <- pirate_attacks_sf_ss %>% 
  left_join(custom_vessel_categories)
Joining, by = "vessel_type"
pirate_attacks_sf_ss2 %>% sf::write_sf("data/geojson/pirate_attacks_sf_ss2.geojson")
Avis dans CPL_write_ogr(obj, dsn, layer, driver, as.character(dataset_options),  :
  GDAL Error 6: DeleteLayer() not supported by this dataset.

Filtrage

pirate_attacks_sf_maps <- pirate_attacks_sf_ss2 %>%
  filter(year_event == 2015, month_event==1)
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()
  )
m
LS0tCnRpdGxlOiAiUiBOb3RlYm9vayIKb3V0cHV0OiBodG1sX25vdGVib29rCi0tLQoKYGBge3J9CmxpYnJhcnkodGlkeXZlcnNlKQpsaWJyYXJ5KGx1YnJpZGF0ZSkKbGlicmFyeShzZikKbGlicmFyeShsZWFmbGV0KQpsaWJyYXJ5KGRhdGFSZXBvcnRlcikKYGBgCmBgYHtyfQpzb3VyY2UoImltcG9ydGVyX2Rvbm5lZXNfY3N2LlIiKQpgYGAKCmBgYHtyfQpnbGltcHNlKHBpcmF0ZV9hdHRhY2tzX3NmLCB3aWR0aCA9IDY3KQpgYGAKYGBge3J9CnBpcmF0ZV9hdHRhY2tzX3NmX3NzIDwtIHBpcmF0ZV9hdHRhY2tzX3NmICU+JQogIGZpbHRlcihkYXRhX3NvdXJjZSA9PSAiaW1iIikgJT4lCnNlbGVjdCgKIC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