helpers (copy).R 3.13 KB
Newer Older
Nicolas Lambert's avatar
Nicolas Lambert committed
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
library(sf)
library(geojsonsf)
library(flowmapper)
library(shiny)
library(shinyjs)
library(r2d3)


  # IMPORT
  
  migr <- read.csv("data/migr2019_T.csv") 
  ctr <- st_read("data/countries.gpkg")
  ctr <- ctr[,c("adm0_a3_is" ,"label","geom")]
  colnames(ctr) <- c("id","name","geom")
  
  # List countries

  isolist <- as.character(ctr$id)
  names(isolist) <- ctr$name
  dfctr <- data.frame(isolist,names(isolist))
  colnames(dfctr) <- c("id","label")
  
# VARS
  # # 
  iso = "FIN"
  type = "from" # or to
  threshold <- 100000
  k = 0.3

  # PROJ
  # crs <- "+proj=aeqd +lat_0=90 +lon_0=0 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs"


  # GET FLOWS
  

  getflows <- function(iso, type, threshold, k) {
  

  center <- st_centroid(ctr[ctr$id==iso,], of_largest_polygon = TRUE)
  coords <- st_coordinates(center)

  crs <- paste0("+proj=aeqd +lat_0=",coords[2]," +lon_0=",coords[1]," +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs")
  countries <- st_transform(ctr,crs)

  
  if (type == "from"){
    m <- migr[migr$i == iso,]
    flowcol <- "#ff6200"
    sentence <- paste0("people from ",dfctr[dfctr == iso,"label"]," are living abroad in 2019.")
    }
  if (type == "to"){
    m <- migr[migr$j == iso,]
    flowcol <- "#c93e7d"
    sentence <- paste0("migrants are living in ",dfctr[dfctr == iso,"label"] , " in 2019.")
  }
  
  getunit <- function(t){
  
  if(t >= 0){
    total <- t
    unit <- ""
    }

  if(t >= 1000){
  total <- round(t/1000,2)
  if(total >= 2){unit <- "thousands"} else {unit <- "thousand"}
  }
  
  if(t >= 1000000 ){
    total <- round(t/1000000,2)
    if(total >= 2){unit <- "millions"} else {unit <- "million"}
  }
  return(paste0(total," ",unit))  
  }
  
  total <- getunit(sum(m$fij))
  total <- paste0("<span class='enhance'>",total,"</span><br/>",sentence)

  m <- m[m$fij >= threshold,]  

  total <- paste0(total," <span class='minimize'><br/>(",getunit(sum(m$fij))," on the map)</span>")
    
  m$i <- as.character(m$i)
  m$j <- as.character(m$j)
  
  if(is.null(k)){
    k <- 700000 / max(m$fi)
  }
  
  if (nrow(m) == 1){
    m <- rbind(m,m)
  }
  
  if (nrow(m > 0)){
  
  flows <- plotflows(
    x = countries,
    xid = "id",
    df = m,
    dfid = c("i","j"),
    dfvar = "fij",
    k = k,
    plot = FALSE
  )[[3]]

  
  
  ctr <- ctr[ctr$id == iso, ] %>% st_transform(4326)
  flows <- smoothr::densify(flows, n = 50) %>% st_transform(4326)
  flows <- st_wrap_dateline(flows, c("WRAPDATELINE=YES","DATELINEOFFSET=180"), quiet = TRUE)  
  flows <- st_buffer(flows,0.01)
  flows <- merge(flows, dfctr, by.x = "i", by.y = "id")
  flows <- merge(flows, dfctr, by.x = "j", by.y = "id")
  flows <- flows[order(flows$height, decreasing = TRUE),]
  colnames(flows)[14:15] <- c("label1","label2")
  flows.json <- sf_geojson(flows, simplify = TRUE)
  } else {flows.json <- 0}
  
  
  
  countries.json <- sf_geojson(ctr, simplify = FALSE)
  center.json <- sf_geojson(center, simplify = FALSE)
  
  if(coords[1] >= 0){coords[1] <- coords[1] - 20} else {coords[1] <- coords[1] + 20}
  if(coords[2] >= 0){coords[2] <- coords[2] - 20} else {coords[2] <- coords[2] + 20}
  
  return(list(flows.json, countries.json, center.json, flowcol, coords[1], coords[2],total))

  }