-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_prep.R
76 lines (57 loc) · 1.45 KB
/
data_prep.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
library(icesTAF)
library(data.table)
library(dplyr)
library(icesVMS)
library(sf)
library(ggplot2)
source("utilities.R")
mkdir("data")
metier_lookup <- read.taf(taf.data.path("metiers", "metier_lookup.csv"))
# get vms (remember to run vms.R first)
vms_all <-
lapply(
dir(taf.data.path("vms"), full = TRUE),
fread
) %>%
do.call(what = "rbind")
vms_all <-
vms_all %>%
filter(country != "NO") %>%
rename(
c_square = "cSquare", LE_MET_level6 = "leMetLevel6",
AnonVessels = "anonVessels", UniqueVessels = "uniqueVessels",
gear_code = "gearCode", vessel_length_category = "vesselLengthCategory"
)
dim(vms_all)
# load shape
bannana <- read_sf("bootstrap/data/shapes/area.csv", )
st_crs(bannana) <- 4326
c_squares <-
tibble(
c_square = unique(vms_all[["c_square"]])
) %>%
vms_add_spatial()
ggplot() +
geom_sf(data = c_squares) +
geom_sf(data = bannana, fill = NA)
# spatial filter
int <- st_contains(bannana, c_squares, sparse = FALSE)
dim(int)
any(int)
c_squares_bananna <- c_squares[which(int),]
ggplot() +
geom_sf(data = c_squares_bananna) +
geom_sf(data = bannana, fill = NA)
# join and filter
vms <-
vms_all %>%
filter(c_square %in% c_squares_bananna$c_square) %>%
left_join(
metier_lookup,
by = c(LE_MET_level6 = "leMetLevel6")
)
# maybe have a vms_clean...
# set to 3
vms$AnonVessels[is.na(vms$UniqueVessels)] <- ""
vms$UniqueVessels[is.na(vms$UniqueVessels)] <- 3
fwrite(vms, file = "data/vms.csv")