Commit 8de15514 authored by Nicolas Lambert's avatar Nicolas Lambert
Browse files

debug & design

parent 6034adbc
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 = "USA"
type = "from" # or to
threshold <- 1000
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"
}
if (type == "to"){
m <- migr[migr$j == iso,]
flowcol <- "#c93e7d"
}
m <- m[m$fij >= threshold,]
m$i <- as.character(m$i)
m$j <- as.character(m$j)
m
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)
ctr <- st_wrap_dateline(ctr, options = "WRAPDATELINE=YES", quiet = TRUE)
flows <- smoothr::densify(flows, n = 50) %>% st_transform(4326)
# flows <- st_wrap_dateline(flows, options = "WRAPDATELINE=YES", quiet = TRUE)
flows <- st_wrap_dateline(flows, c("WRAPDATELINE=YES","DATELINEOFFSET=180"), quiet = TRUE)
flows <- st_buffer(flows,0.01)
plot(st_geometry(flows), col="red")
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)
# return(list(flows.json, countries.json, center.json, flowcol, coords[1], coords[2]))
# }
File deleted
GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137.0,298.257223563]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]]
\ No newline at end of file
File deleted
File deleted
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment