diff --git a/R/04.5-corrigir_ttmatrix.R b/R/04.5-corrigir_ttmatrix.R index 15e0549..d1c47fc 100644 --- a/R/04.5-corrigir_ttmatrix.R +++ b/R/04.5-corrigir_ttmatrix.R @@ -10,20 +10,31 @@ source('./R/fun/setup.R') # que nao tenha menos que 10 hex de acess. # sigla_muni <- 'spo'; ano <- 2017 -# sigla_muni <- 'poa'; ano <- 2019 +# sigla_muni <- 'poa'; ano <- 2017 # sigla_muni <- 'bel'; ano <- 2019 +# sigla_muni <- 'bho'; ano <- 2017 # sigla_muni <- 'nat'; ano <- 2017 -# sigla_muni <- 'man'; ano <- 2019 +# sigla_muni <- 'man'; ano <- 2017 # sigla_muni <- 'spo'; ano <- 2019 +# sigla_muni <- 'for'; ano <- 2019 -identificar_e_corrigir_extremos_acess_muni <- function(sigla_muni, ano) { +corrigir_ttmatrix <- function(sigla_muni, ano) { # status message message('Woking on city ', sigla_muni, ' at year ', ano) + ttmatrix_files <- dir(sprintf("E:/data/output_ttmatrix/%s/r5/", ano), + full.names = TRUE, pattern = sprintf("ttmatrix_%s_%s_r5", ano, sigla_muni)) + + if (length(ttmatrix_files) > 1) { + + ttmatrix_allmodes <- lapply(ttmatrix_files, fread) %>% rbindlist() + + } else ttmatrix_allmodes <- fread(ttmatrix_files) - ttmatrix_allmodes <- fread(sprintf("E:/data/output_ttmatrix/%s/r5/ttmatrix_%s_%s_r5.csv", - ano, ano, sigla_muni)) + + # rename columns + colnames(ttmatrix_allmodes) <- c("origin", "destination", "travel_time", "mode", "pico", "city","ano") # pegar so bike ttmatrix_teste <- ttmatrix_allmodes[mode == "bike"] @@ -31,11 +42,14 @@ identificar_e_corrigir_extremos_acess_muni <- function(sigla_muni, ano) { # ttmatrix_teste <- ttmatrix_allmodes # abrir os pontos da resolucao 09 ~~~~ - points_file <- sprintf("../../r5/points/%s/points_%s_09_%s.csv", ano, sigla_muni, ano) + points_file <- sprintf("../../data/acesso_oport/r5/points/%s/points_%s_09_%s.csv", ano, sigla_muni, ano) # points_file <- "../../data/avaliacao_intervencoes/r5/points/points_for_09_2019.csv" points <- fread(points_file) + # 2) make sure we dont have too many points ------------------------------- + ttmatrix_allmodes <- ttmatrix_allmodes[origin %in% points$id_hex] + # 1) Identificar quais pontos nao foram roteados -------------------- # checar os pontos na matrix ~~~~ @@ -46,6 +60,7 @@ identificar_e_corrigir_extremos_acess_muni <- function(sigla_muni, ano) { ## quais origens e destinos ficaram fora? ~~~~ origem_fora <- setdiff(points$id_hex, origem_matrix) + # setdiff(origem_matrix, points$id_hex) destino_fora <- setdiff(points$id_hex, destino_matrix) # quais pontos ficaram fora completamente? tanto a origem como o destino ~~ @@ -209,8 +224,7 @@ identificar_e_corrigir_extremos_acess_muni <- function(sigla_muni, ano) { ttmatrix_hex_fim[mode == "transit", travel_time := fifelse(origin == destination, 5.8, travel_time)] # salvar output corrigido - write_rds(ttmatrix_hex_fim, sprintf("E:/data/ttmatrix_fixed/%s/ttmatrix_fixed_%s_%s.rds", ano, ano, sigla_muni), - compress = "gz") + write_rds(ttmatrix_hex_fim, sprintf("E:/data/ttmatrix_fixed/%s/ttmatrix_fixed_%s_%s.rds", ano, ano, sigla_muni)) rm(ttmatrix_allmodes) rm(ttmatrix_allmodes_nprob) @@ -222,15 +236,15 @@ identificar_e_corrigir_extremos_acess_muni <- function(sigla_muni, ano) { # aplicar funcao ------------------------------------------------------------------------------ -plan(multiprocess, workers = 1) +plan(multiprocess, workers = 2) furrr::future_walk(munis_list$munis_metro[ano_metro == 2017]$abrev_muni, - identificar_e_corrigir_extremos_acess_muni, ano = 2017) + corrigir_ttmatrix, ano = 2017) furrr::future_walk(munis_list$munis_metro[ano_metro == 2018]$abrev_muni, - identificar_e_corrigir_extremos_acess_muni, ano = 2018) + corrigir_ttmatrix, ano = 2018) furrr::future_walk(munis_list$munis_metro[ano_metro == 2019]$abrev_muni, - identificar_e_corrigir_extremos_acess_muni, ano = 2019) + corrigir_ttmatrix, ano = 2019) walk(munis_list$munis_metro[ano_metro == 2019]$abrev_muni, - identificar_e_corrigir_extremos_acess_muni, ano = 2019) + corrigir_ttmatrix, ano = 2019) # falta spo, rio, goi, bsb @@ -238,11 +252,11 @@ walk(c("for", "cur","poa","bho", "sal","man","rec","bel", "gua","cam","slz","sgo", "mac","duq","cgr", 'nat'), - identificar_e_corrigir_extremos_acess_muni, ano = 2018) + corrigir_ttmatrix, ano = 2018) walk(c("rio","goi","bsb"), - identificar_e_corrigir_extremos_acess_muni, ano = 2019) + corrigir_ttmatrix, ano = 2019) walk(c("for", "cur","poa","bho", "sal","man","rec","bel", "gua","cam","slz","sgo", "mac","duq","cgr", 'nat'), - identificar_e_corrigir_extremos_acess_muni, ano = 2019) + corrigir_ttmatrix, ano = 2019) diff --git a/R/04.5-corrigir_ttmatrix_carro.R b/R/04.5-corrigir_ttmatrix_carro.R new file mode 100644 index 0000000..79271d3 --- /dev/null +++ b/R/04.5-corrigir_ttmatrix_carro.R @@ -0,0 +1,134 @@ +# carregar bibliotecas +source('./R/fun/setup.R') + + +# 1) Identificar e corrigir hexagonos que nao foram roteados ----- + +# Sao identificados hexagos que nao foram roteados pelo streetmap + +# sigla_muni <- 'spo' +# sigla_muni <- 'poa' +# sigla_muni <- 'bel' +# sigla_muni <- 'bho' +# sigla_muni <- 'nat' +# sigla_muni <- 'man' +# sigla_muni <- 'bel' +# sigla_muni <- 'spo' +# sigla_muni <- 'for' +# sigla_muni <- 'goi' +# sigla_muni <- 'rio' +# sigla_muni <- 'cgr' + +corrigir_ttmatrix <- function(sigla_muni) { + + # status message + message('Woking on city ', sigla_muni) + + # abrir matriz original + ttmatrix_allmodes <- fread(sprintf("E:/data/output_ttmatrix/car/OD_TI_%s.csv", sigla_muni)) + ttmatrix_allmodes <- ttmatrix_allmodes %>% + dplyr::rename(origin = 1, destination = 2) %>% + setDT() + + # abrir matriz extra + if (sigla_muni %nin% c('cur', 'goi')) { + + ttmatrix_extra <- fread(sprintf("E:/data/output_ttmatrix/car/OD_TI_%s_extra.csv", sigla_muni)) + # trazer pontos com ids + points_int <- fread(sprintf("../../git_kaue/acesso_oport/R/pontos_extras/ids/ids_%s.csv", sigla_muni)) + ttmatrix_extra[points_int, on = c("origin_hex" = "id_hex_int"), + c("id_hex_origin") := + list(i.id_hex)] + ttmatrix_extra[points_int, on = c("destination_hex" = "id_hex_int"), + c("id_hex_destination") := + list(i.id_hex)] + ttmatrix_extra$origin_hex <- NULL + ttmatrix_extra$destination_hex <- NULL + ttmatrix_extra <- ttmatrix_extra %>% rename(origin = id_hex_origin, destination = id_hex_destination) %>% setDT() + + # juntar matriz original com matriz extra + ttmatrix_allmodes <- rbind(ttmatrix_allmodes, ttmatrix_extra) + + } + + + # 1) initial setup -------------------------------------------------------- + + + # select only necessary columns + ttmatrix_allmodes <- ttmatrix_allmodes[, .(origin, destination, + median_morning_peak, median_afternoon_offpeak)] + ttmatrix_allmodes[, city := substr(sigla_muni, 1, 3)] + ttmatrix_allmodes[, mode := "car"] + + # add parking time + ttmatrix_allmodes[origin != destination, median_morning_peak := median_morning_peak + 2] + ttmatrix_allmodes[origin != destination, median_afternoon_offpeak := median_afternoon_offpeak + 2] + + # abrir os pontos da resolucao 09 ~~~~ + points_file <- sprintf("../../data/acesso_oport/r5/points/%s/points_%s_09_%s.csv", c(2017:2019), sigla_muni, c(2017:2019)) + # points_file <- "../../data/avaliacao_intervencoes/r5/points/points_for_09_2019.csv" + points <- lapply(points_file, fread) %>% rbindlist() %>% distinct(id_hex, .keep_all = TRUE) %>% setDT() + + + # 2) make sure we dont have too many points ------------------------------- + ttmatrix_allmodes <- ttmatrix_allmodes[origin %in% points$id_hex] + ttmatrix_allmodes <- ttmatrix_allmodes[destination %in% points$id_hex] + + + + + # salvar output corrigido --------------- + + # split + if (sigla_muni %in% c("bsb", "goi")) { + + # list origins + origins <- unique(ttmatrix_allmodes$origin) + # divide by 3 + a <- split(origins, rep_len(1:3, length(origins))) + # split ttmatrix + fwrite(ttmatrix_allmodes[origin %in% a[[1]]], sprintf("E:/data/ttmatrix_fixed/car/ttmatrix_fixed_%s_%s_origin1.csv", "2019", sigla_muni)) + fwrite(ttmatrix_allmodes[origin %in% a[[2]]], sprintf("E:/data/ttmatrix_fixed/car/ttmatrix_fixed_%s_%s_origin2.csv", "2019", sigla_muni)) + fwrite(ttmatrix_allmodes[origin %in% a[[3]]], sprintf("E:/data/ttmatrix_fixed/car/ttmatrix_fixed_%s_%s_origin3.csv", "2019", sigla_muni)) + # list dests + dests <- unique(ttmatrix_allmodes$destination) + # divide by 3 + a <- split(dests, rep_len(1:3, length(dests))) + # split ttmatrix + fwrite(ttmatrix_allmodes[destination %in% a[[1]]], sprintf("E:/data/ttmatrix_fixed/car/ttmatrix_fixed_%s_%s_dest1.csv", "2019", sigla_muni)) + fwrite(ttmatrix_allmodes[destination %in% a[[2]]], sprintf("E:/data/ttmatrix_fixed/car/ttmatrix_fixed_%s_%s_dest2.csv", "2019", sigla_muni)) + fwrite(ttmatrix_allmodes[destination %in% a[[3]]], sprintf("E:/data/ttmatrix_fixed/car/ttmatrix_fixed_%s_%s_dest3.csv", "2019", sigla_muni)) + + + } else { + + setorder(ttmatrix_allmodes, origin) + + fwrite(ttmatrix_allmodes, sprintf("E:/data/ttmatrix_fixed/car/ttmatrix_fixed_%s_%s.csv", "2019", sigla_muni)) + + } + + rm(ttmatrix_allmodes) + gc(TRUE) + +} + + + +# aplicar funcao ------------------------------------------------------------------------------ +walk(c("for", "cur","poa","bho", + "sal","rec","bel", + "gua","cam","slz","sgo", + "mac","duq", 'nat', 'rio', 'spo'), + corrigir_ttmatrix) +walk(c("rio","goi","bsb", "spo", "cgr","man"), + corrigir_ttmatrix) + +# corrigir_ttmatrix("rio") +corrigir_ttmatrix("spo") + +# need spliting +corrigir_ttmatrix("cgr") +corrigir_ttmatrix("bsb") +corrigir_ttmatrix("goi") diff --git a/R/05.1-calcular_acessibilidade.R b/R/05.1-calcular_acessibilidade.R index 9e6afb0..7133776 100644 --- a/R/05.1-calcular_acessibilidade.R +++ b/R/05.1-calcular_acessibilidade.R @@ -8,47 +8,64 @@ source('./R/fun/setup.R') -# sigla_muni <- "bho"; ano=2019 +# sigla_muni <- "bel"; ano=2017 # sigla_muni <- "spo"; ano=2019 # sigla_muni <- "for"; ano=2019 # sigla_muni <- "for"; ano=2017 +# sigla_muni <- "sal"; ano=2019; mode1 <- "all"; access <- "all" # sigla_muni <- "bel"; ano=2019; mode1 <- "all"; access <- "all" # sigla_muni <- "bho"; ano=2019; mode1 <- "all"; access <- "all" -# sigla_muni <- "for"; ano = 2019; mode1 <- "car" +# sigla_muni <- "for"; ano = 2019; mode_access <- "car" # sigla_muni <- "spo"; ano = 2019; mode1 <- "car" # sigla_muni <- "bsb_origin1"; ano = 2017; mode1 <- "car" # sigla_muni <- "bsb_dest1"; ano = 2019; mode1 <- "car" # sigla_muni <- "for"; ano = 2019; BFCA <- FALSE # sigla_muni <- "spo"; ano = 2019; BFCA <- FALSE +#' OS parametros de mode_access e indicator_access so foram adicionados para permitir +#' o calculo da acessibilidade de cidades que nao cabiam na memoria (especialmente matrizes de carro) +#' @param mode_access Identifica os modos de transporte para calcular acessibilidade. +#' Pode ser "all" (transporte publico + ativo) ou "car" (carro) +#' @param indicator_access Identifica a familia de indicadores de acessibilidade a calcular. +#' Pode ser "all" (indicadores de acessibilidade cumulativa ativa, passiva e tempo minimo), +#' "active" (somente indicatores de cumulativa ativa e tempo minimo), ou +#' "passive" (somente indicadores de cumulativa passiva) -calcular_acess_muni <- function(sigla_muni, ano, BFCA = FALSE, mode1 = "all", access = "all") { +calcular_acess_muni <- function(sigla_muni, ano, BFCA = FALSE, mode_access = "all", indicator_access = "all") { # status message message('Woking on city ', sigla_muni, ' at year ', ano, '\n') + # identifficar modo(s) de transportes + modo <- munis_list$munis_modo[abrev_muni == sigla_muni & ano_modo == ano]$modo + # 1) Abrir tttmatrix --------------------------------------------------- - if (mode1 == "all") { + if (mode_access == "all") { + ttmatrix <- read_rds(sprintf("E:/data/ttmatrix_fixed/%s/ttmatrix_fixed_%s_%s.rds", ano, ano, sigla_muni)) - } else if (mode1 == "car") { - if (sigla_muni %like% "bsb") { + if (modo == "ativo") ttmatrix <- ttmatrix[mode %in% c("bike", "walk")] else ttmatrix + + + } else if (mode_access == "car") { + + if (sigla_muni %like% c("bsb|goi")) { - path <- sprintf("../../data/acesso_oport/output_ttmatrix_ti/bsb_split/ttmatrix_car_%s.csv", sigla_muni) + path <- sprintf("E:/data/ttmatrix_fixed/car/ttmatrix_fixed_2019_%s.csv", sigla_muni) - } else {path <- sprintf("../../data/acesso_oport/output_ttmatrix_ti/OD_TI_%s.csv", sigla_muni)} + } else {path <- sprintf("E:/data/ttmatrix_fixed/car/ttmatrix_fixed_%s_%s.csv", "2019", sigla_muni)} ttmatrix <- fread(path) - setnames(ttmatrix, c("origin", "destination", "median_morning_peak", "median_afternoon_offpeak")) - ttmatrix[, city := substr(sigla_muni, 1, 3)] - ttmatrix[, mode := "car"] - - # add parking time - ttmatrix[origin != destination, median_morning_peak := median_morning_peak + 2] - ttmatrix[origin != destination, median_afternoon_offpeak := median_afternoon_offpeak + 2] + # setnames(ttmatrix, c("origin", "destination", "median_morning_peak", "median_afternoon_offpeak")) + # ttmatrix[, city := substr(sigla_muni, 1, 3)] + # ttmatrix[, mode := "car"] + # + # # add parking time + # ttmatrix[origin != destination, median_morning_peak := median_morning_peak + 2] + # ttmatrix[origin != destination, median_afternoon_offpeak := median_afternoon_offpeak + 2] # ttmatrix <- fread(sprintf("E:/data/ttmatrix_fixed/%s/car/ttmatrix_fixed_car_%s_%s.csv", # "2019", "2019", sigla_muni)) @@ -72,7 +89,7 @@ calcular_acess_muni <- function(sigla_muni, ano, BFCA = FALSE, mode1 = "all", ac # variaveis de populacao - idade matches("idade_"), # variaveis de renda - renda_total, renda_capita, quintil, decil) %>% setDT() + renda_total, renda_capita, renda_quintil, renda_decil) %>% setDT() # Filtrar apenas colunas com info de uso do solo no destino # corrigir para dplyr @@ -85,42 +102,82 @@ calcular_acess_muni <- function(sigla_muni, ano, BFCA = FALSE, mode1 = "all", ac # if (access %in% c("all", "active")) { + if (sigla_muni %like% "goi" & ano %in% c(2017, 2018)) { + + + ttmatrix <- ttmatrix[origin %in% hexagonos_sf$id_hex] + ttmatrix <- ttmatrix[destination %in% hexagonos_sf$id_hex] + + } - # Join1 - Merge dados de origem na matrix de tempo de viagem - ttmatrix <- ttmatrix[hex_orig, on = c("origin" = "id_hex"), - c('pop_total', 'pop_homens', 'pop_mulheres', 'cor_branca','cor_amarela','cor_indigena','cor_negra', - "idade_0a5", "idade_6a14", "idade_15a18", "idade_19a24", - "idade_25a39", "idade_40a69", "idade_70", - 'renda_total','renda_capita','quintil','decil') := - list(i.pop_total, i.pop_homens, i.pop_mulheres, i.cor_branca, i.cor_amarela, i.cor_indigena, i.cor_negra, - i.idade_0a5, i.idade_6a14, i.idade_15a18, i.idade_19a24, - i.idade_25a39, i.idade_40a69, i.idade_70, - i.renda_total, i.renda_capita, i.quintil, i.decil)] - - # Join2 - Merge dados de destino na matrix de tempo de viagem - ttmatrix <- ttmatrix[hex_dest, on = c("destination" = "id_hex"), - c("empregos_total", "empregos_baixa","empregos_media","empregos_alta", - "saude_total", "saude_baixa", "saude_media", "saude_alta", - "edu_total","edu_infantil","edu_fundamental","edu_medio", - "mat_total", "mat_infantil", "mat_fundamental", "mat_medio", - "cras_total") := - list(i.empregos_total, i.empregos_baixa,i.empregos_media,i.empregos_alta, - i.saude_total, i.saude_baixa, i.saude_media, i.saude_alta, - i.edu_total,i.edu_infantil,i.edu_fundamental,i.edu_medio, - i.mat_total, i.mat_infantil, i.mat_fundamental, i.mat_medio, - i.cras_total)] - - # Calcular emprego com match qualitativo de renda e nivel de escolaridade do emprego - # high income people = jobs with high and med education - # low income people = jobs with low and med education - # ttmatrix[, empregos_match_decil := fifelse(decil>5, - # empregos_alta + empregos_media, - # empregos_baixa + empregos_media)] - # - # ttmatrix[, empregos_match_quintil := fifelse(quintil>=3, - # empregos_alta + empregos_media, - # empregos_baixa + empregos_media)] + message("Joining data...") + a <- Sys.time() + + if (indicator_access == "all") { + + # Join1 - Merge dados de origem na matrix de tempo de viagem + ttmatrix <- ttmatrix[hex_orig, on = c("origin" = "id_hex"), + c('pop_total', 'pop_homens', 'pop_mulheres', 'cor_branca','cor_amarela','cor_indigena','cor_negra', + "idade_0a5", "idade_6a14", "idade_15a18", "idade_19a24", + "idade_25a39", "idade_40a69", "idade_70", + 'renda_total','renda_capita','renda_quintil','renda_decil') := + list(i.pop_total, i.pop_homens, i.pop_mulheres, i.cor_branca, i.cor_amarela, i.cor_indigena, i.cor_negra, + i.idade_0a5, i.idade_6a14, i.idade_15a18, i.idade_19a24, + i.idade_25a39, i.idade_40a69, i.idade_70, + i.renda_total, i.renda_capita, i.renda_quintil, i.renda_decil)] + + + # Join2 - Merge dados de destino na matrix de tempo de viagem + ttmatrix <- ttmatrix[hex_dest, on = c("destination" = "id_hex"), + c("empregos_total", "empregos_baixa","empregos_media","empregos_alta", + "saude_total", "saude_baixa", "saude_media", "saude_alta", + "edu_total","edu_infantil","edu_fundamental","edu_medio", + "mat_total", "mat_infantil", "mat_fundamental", "mat_medio", + "cras_total") := + list(i.empregos_total, i.empregos_baixa,i.empregos_media,i.empregos_alta, + i.saude_total, i.saude_baixa, i.saude_media, i.saude_alta, + i.edu_total,i.edu_infantil,i.edu_fundamental,i.edu_medio, + i.mat_total, i.mat_infantil, i.mat_fundamental, i.mat_medio, + i.cras_total)] + + } else if(indicator_access == "active") { + + + # Join2 - Merge dados de destino na matrix de tempo de viagem + ttmatrix <- ttmatrix[hex_dest, on = c("destination" = "id_hex"), + c("empregos_total", "empregos_baixa","empregos_media","empregos_alta", + "saude_total", "saude_baixa", "saude_media", "saude_alta", + "edu_total","edu_infantil","edu_fundamental","edu_medio", + "mat_total", "mat_infantil", "mat_fundamental", "mat_medio", + "cras_total") := + list(i.empregos_total, i.empregos_baixa,i.empregos_media,i.empregos_alta, + i.saude_total, i.saude_baixa, i.saude_media, i.saude_alta, + i.edu_total,i.edu_infantil,i.edu_fundamental,i.edu_medio, + i.mat_total, i.mat_infantil, i.mat_fundamental, i.mat_medio, + i.cras_total)] + + + + } else if(indicator_access == "passive") { + + + + # Join1 - Merge dados de origem na matrix de tempo de viagem + ttmatrix <- ttmatrix[hex_orig, on = c("origin" = "id_hex"), + c('pop_total', 'pop_homens', 'pop_mulheres', 'cor_branca','cor_amarela','cor_indigena','cor_negra', + "idade_0a5", "idade_6a14", "idade_15a18", "idade_19a24", + "idade_25a39", "idade_40a69", "idade_70", + 'renda_total','renda_capita','renda_quintil','renda_decil') := + list(i.pop_total, i.pop_homens, i.pop_mulheres, i.cor_branca, i.cor_amarela, i.cor_indigena, i.cor_negra, + i.idade_0a5, i.idade_6a14, i.idade_15a18, i.idade_19a24, + i.idade_25a39, i.idade_40a69, i.idade_70, + i.renda_total, i.renda_capita, i.renda_quintil, i.renda_decil)] + + + } + a1 <- Sys.time() + message("Joining data took ", as.integer(as.numeric(a1 - a, "secs")), " seconds") # Dicionario de variaveis: @@ -151,7 +208,7 @@ calcular_acess_muni <- function(sigla_muni, ano, BFCA = FALSE, mode1 = "all", ac # 3) Calcular acessibilidade cumulativa ativa ---------------------------------------------------- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - if (access %in% c("all", "active")) { + if (indicator_access %in% c("all", "active")) { acess_cma <- "CMA" atividade_cma <- c( @@ -257,18 +314,181 @@ calcular_acess_muni <- function(sigla_muni, ano, BFCA = FALSE, mode1 = "all", ac modo <- munis_list$munis_modo[abrev_muni == sigla_muni & ano_modo == ano]$modo message('Calculate access CMA') + a <- Sys.time() # se for carro.. - if (mode1 == "car") { - acess_cma_carro_pico <- ttmatrix[, lapply(to_make_cma_carro_pico, function(x) eval(parse(text = x))) - , by=.(city, mode, origin)] + if (mode_access == "car") { + + + # acess_cma_carro_pico2 <- ttmatrix[, lapply(to_make_cma_carro_pico, function(x) eval(parse(text = x))) + # , by=.(city, mode, origin)] + cols <- unique(grid_cma$atividade_nome) + acess_cma_carro_pico <- list( + ttmatrix[median_morning_peak <= 15, + lapply(.SD, sum, na.rm=TRUE), by=.(city, mode, origin), .SDcols=cols ], + ttmatrix[median_morning_peak <= 30, + lapply(.SD, sum, na.rm=TRUE), by=.(city, mode, origin), .SDcols=cols ], + ttmatrix[median_morning_peak <= 60, + lapply(.SD, sum, na.rm=TRUE), by=.(city, mode, origin), .SDcols=cols ], + ttmatrix[median_morning_peak <= 90, + lapply(.SD, sum, na.rm=TRUE), by=.(city, mode, origin), .SDcols=cols ], + ttmatrix[median_morning_peak <= 120, + lapply(.SD, sum, na.rm=TRUE), by=.(city, mode, origin), .SDcols=cols ] + ) + + acess_cma_carro_pico <- reduce(acess_cma_carro_pico, full_join, by = c("city", "mode", "origin")) %>% + setDT() + colnames(acess_cma_carro_pico) <- c("city", "mode", "origin", grid_cma$junto_tp) acess_cma_carro_pico[, pico := 1] - acess_cma_carro_fpico <- ttmatrix[, lapply(to_make_cma_carro_fpico, function(x) eval(parse(text = x))) - , by=.(city, mode, origin)] + # # access cma pico ----------------------------------------------------------------------------- + # acess_cma_carro_pico <- list( + # a1 <- ttmatrix[median_morning_peak <= 15, + # .(CMATT15 = (sum(empregos_total, na.rm = T)), + # CMATB15 = (sum(empregos_baixa, na.rm = T)), + # CMATM15 = (sum(empregos_media, na.rm = T)), + # CMATA15 = (sum(empregos_alta, na.rm = T)), + # CMAST15 = (sum(saude_total, na.rm = T)), + # CMASB15 = (sum(saude_baixa, na.rm = T)), + # CMASM15 = (sum(saude_media, na.rm = T)), + # CMASA15 = (sum(saude_alta, na.rm = T)), + # CMAET15 = (sum(edu_total, na.rm = T)), + # CMAEI15 = (sum(edu_infantil, na.rm = T)), + # CMAEF15 = (sum(edu_fundamental, na.rm = T)), + # CMAEM15 = (sum(edu_medio, na.rm = T)), + # CMAMT15 = (sum(mat_total, na.rm = T)), + # CMAMI15 = (sum(mat_infantil, na.rm = T)), + # CMAMF15 = (sum(mat_fundamental, na.rm = T)), + # CMAMM15 = (sum(mat_medio, na.rm = T)), + # CMACT15 = (sum(cras_total, na.rm = T))), + # by=.(city, mode, origin) + # + # ], + # ttmatrix[median_morning_peak <= 30, + # .(CMATT30 = (sum(empregos_total, na.rm = T)), + # CMATB30 = (sum(empregos_baixa, na.rm = T)), + # CMATM30 = (sum(empregos_media, na.rm = T)), + # CMATA30 = (sum(empregos_alta, na.rm = T)), + # CMAST30 = (sum(saude_total, na.rm = T)), + # CMASB30 = (sum(saude_baixa, na.rm = T)), + # CMASM30 = (sum(saude_media, na.rm = T)), + # CMASA30 = (sum(saude_alta, na.rm = T)), + # CMAET30 = (sum(edu_total, na.rm = T)), + # CMAEI30 = (sum(edu_infantil, na.rm = T)), + # CMAEF30 = (sum(edu_fundamental, na.rm = T)), + # CMAEM30 = (sum(edu_medio, na.rm = T)), + # CMAMT30 = (sum(mat_total, na.rm = T)), + # CMAMI30 = (sum(mat_infantil, na.rm = T)), + # CMAMF30 = (sum(mat_fundamental, na.rm = T)), + # CMAMM30 = (sum(mat_medio, na.rm = T)), + # CMACT30 = (sum(cras_total, na.rm = T))), + # by=.(city, mode, origin) + # + # ], + # ttmatrix[median_morning_peak <= 60, + # .(CMATT60 = (sum(empregos_total, na.rm = T)), + # CMATB60 = (sum(empregos_baixa, na.rm = T)), + # CMATM60 = (sum(empregos_media, na.rm = T)), + # CMATA60 = (sum(empregos_alta, na.rm = T)), + # CMAST60 = (sum(saude_total, na.rm = T)), + # CMASB60 = (sum(saude_baixa, na.rm = T)), + # CMASM60 = (sum(saude_media, na.rm = T)), + # CMASA60 = (sum(saude_alta, na.rm = T)), + # CMAET60 = (sum(edu_total, na.rm = T)), + # CMAEI60 = (sum(edu_infantil, na.rm = T)), + # CMAEF60 = (sum(edu_fundamental, na.rm = T)), + # CMAEM60 = (sum(edu_medio, na.rm = T)), + # CMAMT60 = (sum(mat_total, na.rm = T)), + # CMAMI60 = (sum(mat_infantil, na.rm = T)), + # CMAMF60 = (sum(mat_fundamental, na.rm = T)), + # CMAMM60 = (sum(mat_medio, na.rm = T)), + # CMACT60 = (sum(cras_total, na.rm = T))), + # by=.(city, mode, origin) + # + # + # ], + # ttmatrix[median_morning_peak <= 90, + # .(CMATT90 = (sum(empregos_total, na.rm = T)), + # CMATB90 = (sum(empregos_baixa, na.rm = T)), + # CMATM90 = (sum(empregos_media, na.rm = T)), + # CMATA90 = (sum(empregos_alta, na.rm = T)), + # CMAST90 = (sum(saude_total, na.rm = T)), + # CMASB90 = (sum(saude_baixa, na.rm = T)), + # CMASM90 = (sum(saude_media, na.rm = T)), + # CMASA90 = (sum(saude_alta, na.rm = T)), + # CMAET90 = (sum(edu_total, na.rm = T)), + # CMAEI90 = (sum(edu_infantil, na.rm = T)), + # CMAEF90 = (sum(edu_fundamental, na.rm = T)), + # CMAEM90 = (sum(edu_medio, na.rm = T)), + # CMAMT90 = (sum(mat_total, na.rm = T)), + # CMAMI90 = (sum(mat_infantil, na.rm = T)), + # CMAMF90 = (sum(mat_fundamental, na.rm = T)), + # CMAMM90 = (sum(mat_medio, na.rm = T)), + # CMACT90 = (sum(cras_total, na.rm = T))), + # by=.(city, mode, origin) + # + # + # ], + # ttmatrix[median_morning_peak <= 120, + # .(CMATT120 = (sum(empregos_total, na.rm = T)), + # CMATB120 = (sum(empregos_baixa, na.rm = T)), + # CMATM120 = (sum(empregos_media, na.rm = T)), + # CMATA120 = (sum(empregos_alta, na.rm = T)), + # CMAST120 = (sum(saude_total, na.rm = T)), + # CMASB120 = (sum(saude_baixa, na.rm = T)), + # CMASM120 = (sum(saude_media, na.rm = T)), + # CMASA120 = (sum(saude_alta, na.rm = T)), + # CMAET120 = (sum(edu_total, na.rm = T)), + # CMAEI120 = (sum(edu_infantil, na.rm = T)), + # CMAEF120 = (sum(edu_fundamental, na.rm = T)), + # CMAEM120 = (sum(edu_medio, na.rm = T)), + # CMAMT120 = (sum(mat_total, na.rm = T)), + # CMAMI120 = (sum(mat_infantil, na.rm = T)), + # CMAMF120 = (sum(mat_fundamental, na.rm = T)), + # CMAMM120 = (sum(mat_medio, na.rm = T)), + # CMACT120 = (sum(cras_total, na.rm = T))), + # by=.(city, mode, origin) + # + # + # ] + # + # + # + # + # + # ) + + + + # access cma fora pico ------------------------------------------------------------------------ + + + # acess_cma_carro_fpico <- ttmatrix[, lapply(to_make_cma_carro_fpico, function(x) eval(parse(text = x))) + # , by=.(city, mode, origin)] + + cols <- unique(grid_cma$atividade_nome) + acess_cma_carro_fpico <- list( + ttmatrix[median_afternoon_offpeak <= 15, + lapply(.SD, sum, na.rm=TRUE), by=.(city, mode, origin), .SDcols=cols ], + ttmatrix[median_afternoon_offpeak <= 30, + lapply(.SD, sum, na.rm=TRUE), by=.(city, mode, origin), .SDcols=cols ], + ttmatrix[median_afternoon_offpeak <= 60, + lapply(.SD, sum, na.rm=TRUE), by=.(city, mode, origin), .SDcols=cols ], + ttmatrix[median_afternoon_offpeak <= 90, + lapply(.SD, sum, na.rm=TRUE), by=.(city, mode, origin), .SDcols=cols ], + ttmatrix[median_afternoon_offpeak <= 120, + lapply(.SD, sum, na.rm=TRUE), by=.(city, mode, origin), .SDcols=cols ] + ) + + acess_cma_carro_fpico <- reduce(acess_cma_carro_fpico, full_join, by = c("city", "mode", "origin")) %>% + setDT() + + colnames(acess_cma_carro_fpico) <- c("city", "mode", "origin", grid_cma$junto_tp) acess_cma_carro_fpico[, pico := 0] + + # bind acess_cma <- rbind(acess_cma_carro_pico, acess_cma_carro_fpico) rm(acess_cma_carro_pico, acess_cma_carro_fpico) @@ -283,9 +503,27 @@ calcular_acess_muni <- function(sigla_muni, ano, BFCA = FALSE, mode1 = "all", ac # para transporte publico e (so 2019) carro if (modo == "todos") { - acess_cma_tp <- ttmatrix[mode %in% c("transit"), - lapply(to_make_cma_tp, function(x) eval(parse(text = x))) - , by=.(city, mode, origin, pico)] + # acess_cma_tp <- ttmatrix[mode %in% c("transit"), + # lapply(to_make_cma_tp, function(x) eval(parse(text = x))) + # , by=.(city, mode, origin, pico)] + + cols <- unique(grid_cma$atividade_nome) + acess_cma_tp <- list( + ttmatrix[travel_time <= 15 & mode %in% c("transit"), + lapply(.SD, sum, na.rm=TRUE), by=.(city, mode, origin, pico), .SDcols=cols ], + ttmatrix[travel_time <= 30 & mode %in% c("transit"), + lapply(.SD, sum, na.rm=TRUE), by=.(city, mode, origin, pico), .SDcols=cols ], + ttmatrix[travel_time <= 60 & mode %in% c("transit"), + lapply(.SD, sum, na.rm=TRUE), by=.(city, mode, origin, pico), .SDcols=cols ], + ttmatrix[travel_time <= 90 & mode %in% c("transit"), + lapply(.SD, sum, na.rm=TRUE), by=.(city, mode, origin, pico), .SDcols=cols ], + ttmatrix[travel_time <= 120 & mode %in% c("transit"), + lapply(.SD, sum, na.rm=TRUE), by=.(city, mode, origin, pico), .SDcols=cols ] + ) + + acess_cma_tp <- reduce(acess_cma_tp, full_join, by = c("city", "mode", "origin", "pico")) %>% + setDT() + colnames(acess_cma_tp) <- c("city", "mode", "origin", "pico", grid_cma$junto_tp) # juntar os cma acess_cma <- rbind(acess_cma, acess_cma_tp, @@ -298,7 +536,7 @@ calcular_acess_muni <- function(sigla_muni, ano, BFCA = FALSE, mode1 = "all", ac # 4) Calcular acessibilidade cumulativa passiva -------------------------------------------------- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - if (access %in% c("all", "passive")) { + if (indicator_access %in% c("all", "passive")) { acess_cmp <- "CMP" atividade_cmp <- c("PT", "PH", "PM", @@ -393,17 +631,56 @@ calcular_acess_muni <- function(sigla_muni, ano, BFCA = FALSE, mode1 = "all", ac message('Calculate access CMP') # se for carro.. - if (mode1 == "car") { - acess_cmp_carro_pico <- ttmatrix[, lapply(to_make_cmp_carro_pico, function(x) eval(parse(text = x))) - , by=.(city, mode, destination)] + if (mode_access == "car") { + # acess_cmp_carro_pico <- ttmatrix[, lapply(to_make_cmp_carro_pico, function(x) eval(parse(text = x))) + # , by=.(city, mode, destination)] + + cols <- unique(grid_cmp$atividade_nome) + acess_cmp_carro_pico <- list( + ttmatrix[median_morning_peak <= 15, + lapply(.SD, sum, na.rm=TRUE), by=.(city, mode, destination), .SDcols=cols ], + ttmatrix[median_morning_peak <= 30, + lapply(.SD, sum, na.rm=TRUE), by=.(city, mode, destination), .SDcols=cols ], + ttmatrix[median_morning_peak <= 60, + lapply(.SD, sum, na.rm=TRUE), by=.(city, mode, destination), .SDcols=cols ], + ttmatrix[median_morning_peak <= 90, + lapply(.SD, sum, na.rm=TRUE), by=.(city, mode, destination), .SDcols=cols ], + ttmatrix[median_morning_peak <= 120, + lapply(.SD, sum, na.rm=TRUE), by=.(city, mode, destination), .SDcols=cols ] + ) + acess_cmp_carro_pico <- reduce(acess_cmp_carro_pico, full_join, by = c("city", "mode", "destination")) %>% + setDT() + + colnames(acess_cmp_carro_pico) <- c("city", "mode", "destination", grid_cmp$junto_tp) acess_cmp_carro_pico[, pico := 1] - acess_cmp_carro_fpico <- ttmatrix[, lapply(to_make_cmp_carro_fpico, function(x) eval(parse(text = x))) - , by=.(city, mode, destination)] + # acess_cmp_carro_fpico <- ttmatrix[, lapply(to_make_cmp_carro_fpico, function(x) eval(parse(text = x))) + # , by=.(city, mode, destination)] + + cols <- unique(grid_cmp$atividade_nome) + acess_cmp_carro_fpico <- list( + ttmatrix[median_afternoon_offpeak <= 15, + lapply(.SD, sum, na.rm=TRUE), by=.(city, mode, destination), .SDcols=cols ], + ttmatrix[median_afternoon_offpeak <= 30, + lapply(.SD, sum, na.rm=TRUE), by=.(city, mode, destination), .SDcols=cols ], + ttmatrix[median_afternoon_offpeak <= 60, + lapply(.SD, sum, na.rm=TRUE), by=.(city, mode, destination), .SDcols=cols ], + ttmatrix[median_afternoon_offpeak <= 90, + lapply(.SD, sum, na.rm=TRUE), by=.(city, mode, destination), .SDcols=cols ], + ttmatrix[median_afternoon_offpeak <= 120, + lapply(.SD, sum, na.rm=TRUE), by=.(city, mode, destination), .SDcols=cols ] + ) + + acess_cmp_carro_fpico <- reduce(acess_cmp_carro_fpico, full_join, by = c("city", "mode", "destination")) %>% + setDT() + + colnames(acess_cmp_carro_fpico) <- c("city", "mode", "destination", grid_cmp$junto_tp) acess_cmp_carro_fpico[, pico := 0] + + # bind acess_cmp <- rbind(acess_cmp_carro_pico, acess_cmp_carro_fpico) rm(acess_cmp_carro_pico, acess_cmp_carro_fpico) @@ -417,9 +694,27 @@ calcular_acess_muni <- function(sigla_muni, ano, BFCA = FALSE, mode1 = "all", ac # para transporte publico e (so 2019) carro if (modo == "todos") { - acess_cmp_tp <- ttmatrix[mode %in% c("transit"), - lapply(to_make_cmp_tp, function(x) eval(parse(text = x))) - , by=.(city, mode, destination, pico)] + # acess_cmp_tp <- ttmatrix[mode %in% c("transit"), + # lapply(to_make_cmp_tp, function(x) eval(parse(text = x))) + # , by=.(city, mode, destination, pico)] + + cols <- unique(grid_cmp$atividade_nome) + acess_cmp_tp <- list( + ttmatrix[travel_time <= 15 & mode %in% c("transit"), + lapply(.SD, sum, na.rm=TRUE), by=.(city, mode, destination, pico), .SDcols=cols ], + ttmatrix[travel_time <= 30 & mode %in% c("transit"), + lapply(.SD, sum, na.rm=TRUE), by=.(city, mode, destination, pico), .SDcols=cols ], + ttmatrix[travel_time <= 60 & mode %in% c("transit"), + lapply(.SD, sum, na.rm=TRUE), by=.(city, mode, destination, pico), .SDcols=cols ], + ttmatrix[travel_time <= 90 & mode %in% c("transit"), + lapply(.SD, sum, na.rm=TRUE), by=.(city, mode, destination, pico), .SDcols=cols ], + ttmatrix[travel_time <= 120 & mode %in% c("transit"), + lapply(.SD, sum, na.rm=TRUE), by=.(city, mode, destination, pico), .SDcols=cols ] + ) + + acess_cmp_tp <- reduce(acess_cmp_tp, full_join, by = c("city", "mode", "destination", "pico")) %>% + setDT() + colnames(acess_cmp_tp) <- c("city", "mode", "destination", "pico", grid_cmp$junto_tp) # juntar os cmp acess_cmp <- rbind(acess_cmp, acess_cmp_tp, @@ -435,7 +730,7 @@ calcular_acess_muni <- function(sigla_muni, ano, BFCA = FALSE, mode1 = "all", ac # 5) Calcular acessibilidade tempo minimo --------------- # (aqui eh feito junto para os dois modos) # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - if (access %in% c("all", "active")) { + if (indicator_access %in% c("all", "active")) { acess_tmi <- "TMI" atividade_tmi <- c("ST", "SB", "SM", "SA", "ET", "EI", "EF", "EM", "CT") @@ -458,10 +753,10 @@ calcular_acess_muni <- function(sigla_muni, ano, BFCA = FALSE, mode1 = "all", ac codigo_tmi <- sprintf("%s = min(travel_time[which(%s >= 1)])", grid_tmi$junto, grid_tmi$atividade_nome) - codigo_tmi_carro_pico <- sprintf("%s = min(median_morning_peak[which(%s >= 1)])", + codigo_tmi_carro_pico <- sprintf("%s = min(median_morning_peak[which(%s >= 1)], na.rm = TRUE)", grid_tmi$junto, grid_tmi$atividade_nome) - codigo_tmi_carro_fpico <- sprintf("%s = min(median_afternoon_offpeak[which(%s >= 1)])", + codigo_tmi_carro_fpico <- sprintf("%s = min(median_afternoon_offpeak[which(%s >= 1)], na.rm = TRUE)", grid_tmi$junto, grid_tmi$atividade_nome) @@ -473,18 +768,82 @@ calcular_acess_muni <- function(sigla_muni, ano, BFCA = FALSE, mode1 = "all", ac message('Calculate access TMI') # calcular acessibilidade - if (mode1 == "car") { + if (mode_access == "car") { - acess_tmi_carro_pico <- ttmatrix[, lapply(to_make_tmi_carro_pico, function(x) eval(parse(text = x))) - , by=.(city, mode, origin)] + # tictoc::tic() + # acess_tmi_carro_pico <- ttmatrix[, lapply(to_make_tmi_carro_pico, function(x) eval(parse(text = x))) + # , by=.(city, mode, origin)] + # tictoc::toc() + # tictoc::tic() + acess_tmi_carro_pico <- ttmatrix[, .( + TMIST = min(median_morning_peak[which(saude_total >= 1)], na.rm = TRUE), + TMISB = min(median_morning_peak[which(saude_baixa >= 1)], na.rm = TRUE), + TMISM = min(median_morning_peak[which(saude_media >= 1)], na.rm = TRUE), + TMISA = min(median_morning_peak[which(saude_alta >= 1)], na.rm = TRUE), + TMIET = min(median_morning_peak[which(edu_total >= 1)], na.rm = TRUE), + TMIEI = min(median_morning_peak[which(edu_infantil >= 1)], na.rm = TRUE), + TMIEF = min(median_morning_peak[which(edu_fundamental >= 1)], na.rm = TRUE), + TMIEM = min(median_morning_peak[which(edu_medio >= 1)], na.rm = TRUE), + TMICT = min(median_morning_peak[which(cras_total >= 1)], na.rm = TRUE) + ), + by=.(city, mode, origin)] + # tictoc::toc() + + # tictoc::tic() + # acess_tmi_carro_pico <- list( + # + # ttmatrix[saude_total >= 1, .(TMIST = min(median_morning_peak, na.rm = TRUE)), by=.(city, mode, origin)], + # ttmatrix[saude_baixa >= 1, .(TMISB = min(median_morning_peak, na.rm = TRUE)), by=.(city, mode, origin)], + # ttmatrix[saude_media >= 1, .(TMISM = min(median_morning_peak, na.rm = TRUE)), by=.(city, mode, origin)], + # ttmatrix[saude_alta >= 1, .(TMISA = min(median_morning_peak, na.rm = TRUE)), by=.(city, mode, origin)], + # ttmatrix[edu_total >= 1, .(TMIET = min(median_morning_peak, na.rm = TRUE)), by=.(city, mode, origin)], + # ttmatrix[edu_infantil >= 1, .(TMIEI = min(median_morning_peak, na.rm = TRUE)), by=.(city, mode, origin)], + # ttmatrix[edu_fundamental >= 1, .(TMIEF = min(median_morning_peak, na.rm = TRUE)), by=.(city, mode, origin)], + # ttmatrix[edu_medio >= 1, .(TMIEM = min(median_morning_peak, na.rm = TRUE)), by=.(city, mode, origin)], + # ttmatrix[cras_total >= 1, .(TMICT = min(median_morning_peak, na.rm = TRUE)), by=.(city, mode, origin)] + # + # ) + # tictoc::toc() + # acess_tmi_carro_pico <- reduce(acess_tmi_carro_pico, full_join, by=c("city", "mode", "origin")) %>% setDT() acess_tmi_carro_pico[, pico := 1] - acess_tmi_carro_fpico <- ttmatrix[, lapply(to_make_tmi_carro_fpico, function(x) eval(parse(text = x))) - , by=.(city, mode, origin)] + # acess_tmi_carro_fpico <- ttmatrix[, lapply(to_make_tmi_carro_fpico, function(x) eval(parse(text = x))) + # , by=.(city, mode, origin)] + + acess_tmi_carro_fpico <- ttmatrix[, .( + TMIST = min(median_afternoon_offpeak[which(saude_total >= 1)], na.rm = TRUE), + TMISB = min(median_afternoon_offpeak[which(saude_baixa >= 1)], na.rm = TRUE), + TMISM = min(median_afternoon_offpeak[which(saude_media >= 1)], na.rm = TRUE), + TMISA = min(median_afternoon_offpeak[which(saude_alta >= 1)], na.rm = TRUE), + TMIET = min(median_afternoon_offpeak[which(edu_total >= 1)], na.rm = TRUE), + TMIEI = min(median_afternoon_offpeak[which(edu_infantil >= 1)], na.rm = TRUE), + TMIEF = min(median_afternoon_offpeak[which(edu_fundamental >= 1)], na.rm = TRUE), + TMIEM = min(median_afternoon_offpeak[which(edu_medio >= 1)], na.rm = TRUE), + TMICT = min(median_afternoon_offpeak[which(cras_total >= 1)], na.rm = TRUE) + ), + by=.(city, mode, origin)] + # tictoc::tic() + # acess_tmi_carro_fpico <- list( + # + # ttmatrix[saude_total >= 1, .(TMIST = min(median_afternoon_offpeak, na.rm = TRUE)), by=.(city, mode, origin)], + # ttmatrix[saude_baixa >= 1, .(TMISB = min(median_afternoon_offpeak, na.rm = TRUE)), by=.(city, mode, origin)], + # ttmatrix[saude_media >= 1, .(TMISM = min(median_afternoon_offpeak, na.rm = TRUE)), by=.(city, mode, origin)], + # ttmatrix[saude_alta >= 1, .(TMISA = min(median_afternoon_offpeak, na.rm = TRUE)), by=.(city, mode, origin)], + # ttmatrix[edu_total >= 1, .(TMIET = min(median_afternoon_offpeak, na.rm = TRUE)), by=.(city, mode, origin)], + # ttmatrix[edu_infantil >= 1, .(TMIEI = min(median_afternoon_offpeak, na.rm = TRUE)), by=.(city, mode, origin)], + # ttmatrix[edu_fundamental >= 1, .(TMIEF = min(median_afternoon_offpeak, na.rm = TRUE)), by=.(city, mode, origin)], + # ttmatrix[edu_medio >= 1, .(TMIEM = min(median_afternoon_offpeak, na.rm = TRUE)), by=.(city, mode, origin)], + # ttmatrix[cras_total >= 1, .(TMICT = min(median_afternoon_offpeak, na.rm = TRUE)), by=.(city, mode, origin)] + # + # ) + # tictoc::toc() + # acess_tmi_carro_fpico <- reduce(acess_tmi_carro_fpico, full_join, by=c("city", "mode", "origin")) %>% setDT() acess_tmi_carro_fpico[, pico := 0] + + # bind acess_tmi <- rbind(acess_tmi_carro_pico, acess_tmi_carro_fpico) rm(acess_tmi_carro_pico, acess_tmi_carro_fpico) @@ -492,9 +851,40 @@ calcular_acess_muni <- function(sigla_muni, ano, BFCA = FALSE, mode1 = "all", ac } else { - acess_tmi <- ttmatrix[, lapply(to_make_tmi, function(x) eval(parse(text = x))) - , by=.(city, mode, origin, pico)] + # tictoc::tic() + # acess_tmi <- ttmatrix[, lapply(to_make_tmi, function(x) eval(parse(text = x))) + # , by=.(city, mode, origin, pico)] + # tictoc::toc() + # tictoc::tic() + acess_tmi <- ttmatrix[, .(TMIST = min(travel_time[which(saude_total >= 1)]), + TMISB = min(travel_time[which(saude_baixa >= 1)]), + TMISM = min(travel_time[which(saude_media >= 1)]), + TMISA = min(travel_time[which(saude_alta >= 1)]), + TMIET = min(travel_time[which(edu_total >= 1)]), + TMIEI = min(travel_time[which(edu_infantil >= 1)]), + TMIEF = min(travel_time[which(edu_fundamental >= 1)]), + TMIEM = min(travel_time[which(edu_medio >= 1)]), + TMICT = min(travel_time[which(cras_total >= 1)])), + by=.(city, mode, origin, pico)] + # tictoc::toc() + +# acess_tmi1 <- list( +# +# ttmatrix[saude_total >= 1, .(TMIST = min(travel_time, na.rm = TRUE)), by=.(city, mode, origin, pico)], +# ttmatrix[saude_baixa >= 1, .(TMISB = min(travel_time, na.rm = TRUE)), by=.(city, mode, origin, pico)], +# ttmatrix[saude_media >= 1, .(TMISM = min(travel_time, na.rm = TRUE)), by=.(city, mode, origin, pico)], +# ttmatrix[saude_alta >= 1, .(TMISA = min(travel_time, na.rm = TRUE)), by=.(city, mode, origin, pico)], +# ttmatrix[edu_total >= 1, .(TMIET = min(travel_time, na.rm = TRUE)), by=.(city, mode, origin, pico)], +# ttmatrix[edu_infantil >= 1, .(TMIEI = min(travel_time, na.rm = TRUE)), by=.(city, mode, origin, pico)], +# ttmatrix[edu_fundamental >= 1, .(TMIEF = min(travel_time, na.rm = TRUE)), by=.(city, mode, origin, pico)], +# ttmatrix[edu_medio >= 1, .(TMIEM = min(travel_time, na.rm = TRUE)), by=.(city, mode, origin, pico)], +# ttmatrix[cras_total >= 1, .(TMICT = min(travel_time, na.rm = TRUE)), by=.(city, mode, origin, pico)] +# +# ) +# tictoc::toc() +# acess_tmi1 <- reduce(acess_tmi1, full_join, by=c("city", "mode", "origin", "pico")) %>% setDT() + } } @@ -687,7 +1077,7 @@ calcular_acess_muni <- function(sigla_muni, ano, BFCA = FALSE, mode1 = "all", ac message('Output ...') # Juntar os tres (left_join) - if (access == "all") { + if (indicator_access == "all") { acess <- merge(acess_cma, acess_cmp, all = TRUE, @@ -700,19 +1090,28 @@ calcular_acess_muni <- function(sigla_muni, ano, BFCA = FALSE, mode1 = "all", ac by.x = c("city", "mode", "origin", "pico"), by.y = c("city", "mode", "origin", "pico")) - } else if (access == "active") { + rm(acess_cma) + rm(acess_cmp) + rm(acess_tmi) + + } else if (indicator_access == "active") { acess <- merge(acess_cma, acess_tmi, all = TRUE, by.x = c("city", "mode", "origin", "pico"), by.y = c("city", "mode", "origin", "pico")) + rm(acess_cma) + rm(acess_tmi) + } else { acess <- acess_cmp setnames(acess, "destination", "origin") + rm(acess_cmp) + } if (BFCA) { @@ -736,8 +1135,9 @@ calcular_acess_muni <- function(sigla_muni, ano, BFCA = FALSE, mode1 = "all", ac mutate(ano = ano) # 8) Salvar output -------------------------------------- + out1 <- ifelse(mode_access == "all", "tp_active", "car") - path_out <- sprintf("../../data/acesso_oport/output_access/%s/acess_%s_%s_%s_access-%s.rds", ano, ano, sigla_muni, mode1, access) + path_out <- sprintf("../../data/acesso_oport/output_access/%s/%s/acess_%s_%s_%s_access-%s.rds", ano, out1, ano, sigla_muni, mode_access, indicator_access) write_rds(acess_sf, path_out) @@ -745,45 +1145,53 @@ calcular_acess_muni <- function(sigla_muni, ano, BFCA = FALSE, mode1 = "all", ac # gc colletc rm(ttmatrix) rm(acess_sf) + rm(acess) gc(TRUE) } -# 2. APLICAR PARA TODOS AS CIDADADES -------------------------------------------------------------- -plan(multiprocess, workers = 3) +# # 2. APLICAR PARA TODOS AS CIDADADES -------------------------------------------------------------- +# plan(multiprocess, workers = 5) # furrr::future_walk(munis_list$munis_metro[ano_metro == 2017]$abrev_muni, calcular_acess_muni, ano = 2017) # furrr::future_walk(munis_list$munis_metro[ano_metro == 2018]$abrev_muni, calcular_acess_muni, ano = 2018) -furrr::future_walk(c("for", "cur","poa","bho", - "sal","man","rec","bel", - "gua","cam","slz","sgo","mac", - "duq","cgr", 'nat'), calcular_acess_muni, ano = 2018) -calcular_acess_muni("rio", 2018) -calcular_acess_muni("spo", 2018) -calcular_acess_muni("bsb", 2018) -calcular_acess_muni("goi", 2018) - -# para carro -------------- -# big boys first -calcular_acess_muni("spo", 2018, mode1 = "car") -calcular_acess_muni("bsb_origin1", 2018, mode1 = "car", access = "active") -calcular_acess_muni("bsb_origin2", 2018, mode1 = "car", access = "active") -calcular_acess_muni("bsb_origin3", 2018, mode1 = "car", access = "active") -calcular_acess_muni("bsb_dest1", 2018, mode1 = "car", access = "passive") -calcular_acess_muni("bsb_dest2", 2018, mode1 = "car", access = "passive") -calcular_acess_muni("bsb_dest3", 2018, mode1 = "car", access = "passive") -calcular_acess_muni("rio", 2018, mode1 = "car") -calcular_acess_muni("goi", 2018, mode1 = "car") -# calcular_acess_muni("man", 2017, mode1 = "car") -# calcular_acess_muni("rec", 2017, mode1 = "car") -# calcular_acess_muni("nat", 2017, mode1 = "car") -# calcular_acess_muni("bel", 2017, mode1 = "car") +# furrr::future_walk(c("for", "cur","poa","bho", +# "sal","man","rec","bel", +# "gua","cam","slz","sgo","mac", +# "duq","cgr", 'nat'), calcular_acess_muni, ano = 2019) +# calcular_acess_muni("rio", 2019) +# calcular_acess_muni("bsb", 2019) +# calcular_acess_muni("spo", 2019) +# calcular_acess_muni("goi", 2019) +# +# +# +# +# # para carro -------------- +# # big boys first +calcular_acess_muni("man", 2019, mode_access = "car") +# calcular_acess_muni("spo", 2019, mode_access = "car") #ok +# calcular_acess_muni("man", 2019, mode_access = "car") # ok +# calcular_acess_muni("rio", 2019, mode_access = "car") #ok +calcular_acess_muni("bsb_origin1", 2019, mode_access = "car", indicator_access = "active") +calcular_acess_muni("bsb_origin2", 2019, mode_access = "car", indicator_access = "active") +calcular_acess_muni("bsb_origin3", 2019, mode_access = "car", indicator_access = "active") +# # calcular_acess_muni("bsb_dest1", 2019, mode_access = "car", indicator_access = "passive") +# # calcular_acess_muni("bsb_dest2", 2019, mode_access = "car", indicator_access = "passive") +# # calcular_acess_muni("bsb_dest3", 2019, mode_access = "car", indicator_access = "passive") +# calcular_acess_muni("goi_origin1", 2017, mode_access = "car", indicator_access = "active") +# calcular_acess_muni("goi_origin2", 2017, mode_access = "car", indicator_access = "active") +# calcular_acess_muni("goi_origin3", 2017, mode_access = "car", indicator_access = "active") +# # calcular_acess_muni("goi_dest1", 2019, mode_access = "car", indicator_access = "passive") +# # calcular_acess_muni("goi_dest2", 2019, mode_access = "car", indicator_access = "passive") +# # calcular_acess_muni("goi_dest3", 2019, mode_access = "car", indicator_access = "passive") +# # others -plan(multiprocess, workers = 1) +plan(multiprocess, workers = 14) furrr::future_walk(c("for", "cur","poa","bho", - "sal","man","rec","bel", - "gua","cam","slz","sgo","mac", - "duq","cgr", 'nat'), - calcular_acess_muni, ano = 2018, - mode1 = "car") - + "sal","rec","bel","gua", + "cam","slz","sgo", + "mac","duq",'nat'), + calcular_acess_muni, ano = 2019, + mode_access = "car") +# diff --git a/R/05.2-check_acessibilidade.R b/R/05.2-check_acessibilidade.R index d684da0..4c50220 100644 --- a/R/05.2-check_acessibilidade.R +++ b/R/05.2-check_acessibilidade.R @@ -9,21 +9,69 @@ source('./R/fun/setup.R') # oepn all access -acess <- read_rds(sprintf("../../data/acesso_oport/output_base_final/%s/dados%s_AcessOport_access_tpcar_v1.0.rds", "2017", "2017")) %>% - setDT() +acess_tpcar <- rbind( + read_rds(sprintf("../../data/acesso_oport/output_base_final/%s/dados%s_AcessOport_access_tpcar_v1.0.rds", "2017", "2017")) %>% + setDT(), + read_rds(sprintf("../../data/acesso_oport/output_base_final/%s/dados%s_AcessOport_access_tpcar_v1.0.rds", "2018", "2018")) %>% + setDT(), + # oepn all access + read_rds(sprintf("../../data/acesso_oport/output_base_final/%s/dados%s_AcessOport_access_tpcar_v1.0.rds", "2019", "2019")) %>% + setDT()) +# oepn all access +acess_active <- rbind( + # oepn all access + read_rds(sprintf("../../data/acesso_oport/output_base_final/%s/dados%s_AcessOport_access_active_v1.0.rds", "2017", "2017")) %>% + setDT(), + read_rds(sprintf("../../data/acesso_oport/output_base_final/%s/dados%s_AcessOport_access_active_v1.0.rds", "2018", "2018")) %>% + setDT(), + read_rds(sprintf("../../data/acesso_oport/output_base_final/%s/dados%s_AcessOport_access_active_v1.0.rds", "2019", "2019")) %>% + setDT()) +landuse <- rbind( + # oepn all access + read_rds(sprintf("../../data/acesso_oport/output_base_final/%s/dados%s_AcessOport_landuse_v1.0.rds", "2017", "2017")) %>% + setDT(), + read_rds(sprintf("../../data/acesso_oport/output_base_final/%s/dados%s_AcessOport_landuse_v1.0.rds", "2018", "2018")) %>% + setDT(), + read_rds(sprintf("../../data/acesso_oport/output_base_final/%s/dados%s_AcessOport_landuse_v1.0.rds", "2019", "2019")) %>% + setDT()) + + +table(acess_tpcar$sigla_muni, useNA = 'always') +table(acess_tpcar$nome_muni, useNA = 'always') +table(acess_tpcar$code_muni, useNA = 'always') + +table(acess_active$sigla_muni, useNA = 'always') +table(acess_active$nome_muni, useNA = 'always') +table(acess_active$code_muni, useNA = 'always') +table(landuse$sigla_muni, useNA = 'always') +table(landuse$nome_muni, useNA = 'always') +table(landuse$code_muni, useNA = 'always') +# valores minimos e maximos +check_extreme1 <- acess_tpcar %>% + group_by(sigla_muni, ano, modo) %>% + summarise(across(CMATT15:TMICT, min)) + # quantidade de NA -a_summary <- acess %>% - group_by(sigla_muni, modo) %>% +a_summary <- acess1 %>% + group_by(sigla_muni, ano, modo) %>% summarise(across(CMATT15:CMPP70I120, ~sum(is.na(.x)))) # quantidade de NA - TMI -a_summary_tmi <- acess %>% - group_by(sigla_muni, modo) %>% +a_summary_tmi <- acess1 %>% + group_by(sigla_muni, ano, modo) %>% summarise(across(TMIST:TMICT, ~sum(is.na(.x)))) +a_summary_tmi <- acess_tpcar %>% + filter(pico == 1) %>% + group_by(sigla_muni, ano, modo) %>% + summarise(across(TMIST:TMICT, ~sum(is.infinite(.x)))) # quantidade de hexagonos para cada cidade, modo, ano -a_summary1 <- acess %>% - count(sigla_muni, modo) +a_summary <- rbind(acess_tpcar %>% + count(sigla_muni, ano, modo), + acess_active %>% + count(sigla_muni, ano, modo) +) %>% + pivot_wider(names_from = modo, values_from = n) diff --git a/R/06.1-organizar_base_de_dados.R b/R/06.1-organizar_base_de_dados.R index 508d95f..dca0b6f 100644 --- a/R/06.1-organizar_base_de_dados.R +++ b/R/06.1-organizar_base_de_dados.R @@ -21,6 +21,7 @@ organizar_base_acess <- function(ano) { # Select the corerspondent munis_df munis_df <- munis_list$munis_df + # munis_df <- munis_df %nlike% "bsb" # 1) Carrega dados --------------------------- @@ -36,27 +37,39 @@ organizar_base_acess <- function(ano) { # dados acessibilidade --------------------------- acess_paths <- dir(sprintf("../../data/acesso_oport/output_access/%s", ano), full.names = TRUE, pattern = "acess_", recursive = TRUE) - acess_paths_ok <- acess_paths[acess_paths %nlike% "bsb_origin|bsb_dest"] - acess <- lapply(acess_paths_ok, read_rds) %>% rbindlist(fill = TRUE) + acess_paths_ok <- acess_paths[acess_paths %nlike% "bsb_origin|bsb_dest|goi_origin|goi_dest"] + acess <- lapply(acess_paths_ok, read_rds) %>% + rbindlist(fill = TRUE) - # abrir bsb carro - especial + # abrir bsb/goi carro - especial acess_paths_bsb_carro1 <- acess_paths[acess_paths %like% "bsb_origin"] acess_paths_bsb_carro2 <- acess_paths[acess_paths %like% "bsb_dest"] - acess_bsb_carro1 <- lapply(acess_paths_bsb_carro1, function(x) read_rds(x)) %>% rbindlist() %>% dplyr::select(-geometry) %>% - mutate(city = "bsb") - acess_bsb_carro2 <- lapply(acess_paths_bsb_carro2, function(x) read_rds(x)) %>% rbindlist() %>% dplyr::select(-geometry) %>% - mutate(city = "bsb") + acess_bsb_carro1 <- lapply(acess_paths_bsb_carro1, function(x) read_rds(x)) %>% rbindlist() + acess_bsb_carro2 <- lapply(acess_paths_bsb_carro2, function(x) read_rds(x)) %>% rbindlist() %>% dplyr::select(-geometry) # join them all! acess_bsb_carro <- full_join(acess_bsb_carro1, acess_bsb_carro2, by = c("origin", "city", "mode", "ano", "pico")) + + # ui <- acess_bsb_carro %>% filter(is.na(CMATT60)) + # ui %>% filter(!is.infinite(TMIST)) + # bring geom - acess_bsb_carro <- left_join(acess_bsb_carro, hex_agreg %>% dplyr::select(id_hex, geometry), by = c("origin" = "id_hex")) %>% setDT() + # acess_bsb_carro <- left_join(acess_bsb_carro, hex_agreg %>% dplyr::select(id_hex, geometry), by = c("origin" = "id_hex")) %>% setDT() # rbind both access # we have to set fill = TRUE because bsb is only for car and car doesnt have some # variables from walk and bike acess <- rbind(acess, acess_bsb_carro, fill = TRUE) + # ui <- acess %>% filter(is.na(CMATT120)) + # ui <- acess %>% filter(CMATT120 == 0) + # ui <- acess %>% filter(is.infinite(TMIST)) + # ui <- acess %>% filter(is.na(TMICT)) + # table(ui$mode) + # table(ui$city) + + # fill NA + acess[is.na(acess)] <- 0 setDT(acess) setnames(acess, 'origin', 'id_hex' ) @@ -81,6 +94,10 @@ organizar_base_acess <- function(ano) { left_join(hex_agreg_muni %>% dplyr::select(id_hex, sigla_muni, cod_muni, nome_muni), by = "id_hex") + # a acessibilidade goiania foi calculada para a toda RM em todos anos, mas so vamos usar para + # 2019 + acess_muni <- acess_muni %>% filter(!is.na(sigla_muni)) + # # join data sets # hex_dt <- left_join(setDT(hex_agreg_muni), setDT(acess_muni)[, -"geometry", with =F], # by=c("id_hex", "quintil", "decil")) @@ -92,7 +109,7 @@ organizar_base_acess <- function(ano) { hex_landuse <- hex_agreg_muni %>% - dplyr::select(id_hex, sigla_muni, nome_muni, code_muni = cod_muni, ano, + dplyr::select(id_hex, abbrev_muni = sigla_muni, name_muni = nome_muni, code_muni = cod_muni, year = ano, # Selecionar variaveis de populacao (cor) P001 = pop_total, @@ -162,25 +179,25 @@ organizar_base_acess <- function(ano) { hex_dt_tpcarro <- acess_muni %>% filter(mode %in% c("transit", "car")) %>% - dplyr::select(id_hex, sigla_muni, nome_muni, code_muni = cod_muni, ano, + dplyr::select(id_hex, abbrev_muni = sigla_muni, name_muni = nome_muni, code_muni = cod_muni, year = ano, # Selecionar variaveis de acessibilidade - modo = mode, - pico, + mode = mode, + peak = pico, ends_with(c("15", "30", "60", "90", "120")), # matches("^CMA[:upper:]{3}(15|30|45|60)"), # matches("^CMP[:upper:]{3}(15|30|45|60)"), starts_with("TMI"), geometry) %>% # renomear os modos - mutate(modo = case_when( - modo == "transit" ~ "tp", - modo == "car" ~ "carro" + mutate(mode = case_when( + mode == "transit" ~ "public_transport", + mode == "car" ~ "car" )) %>% # arredondar acessibilidade para 4 casas decimais mutate_at(vars(matches("CMA|CMP|TMI")), round, digits = 4) %>% # garantir que so tenha um unico hexagono para cada cidade, modo e pico - distinct(id_hex, sigla_muni, modo, pico, .keep_all = TRUE) %>% + distinct(id_hex, abbrev_muni, mode, peak, .keep_all = TRUE) %>% setDT() # # abrir pontos de fortalewza @@ -194,20 +211,20 @@ organizar_base_acess <- function(ano) { hex_dt_ativo <- acess_muni %>% filter(mode %in% c("bike", "walk")) %>% - dplyr::select(id_hex, sigla_muni, nome_muni, code_muni = cod_muni, ano, + dplyr::select(id_hex, abbrev_muni = sigla_muni, name_muni = nome_muni, code_muni = cod_muni, year = ano, # Selecionar variaveis de acessibilidade - modo = mode, - pico, + mode, + peak = pico, ends_with(c("15", "30", "45", "60")), starts_with("TMI"), geometry) %>% # renomear os modos - mutate(modo = case_when( - modo == "bike" ~"bicicleta", - modo == "walk" ~ "caminhada" + mutate(mode = case_when( + mode == "bike" ~"bicycle", + mode == "walk" ~ "walk" )) %>% # arredondar acessibilidade para 4 casas decimais - mutate_at(vars(matches("CMA|CMP|TMI")), round, digits = 4) %>% + mutate_at(vars(matches("CMA|CMP|TMI")), round) %>% # # filtrar cidades que so tem ativo que porventura tenham tp # mutate(ok = ifelse(sigla_muni %in% munis_list$munis_modo[ano_modo == ano & modo == "ativo"]$abrev_muni & modo == "tp", 1, 0)) %>% @@ -215,7 +232,7 @@ organizar_base_acess <- function(ano) { # select(-ok) %>% # garantir que so tenha um unico hexagono para cada cidade, modo e pico - distinct(id_hex, sigla_muni, modo, pico, .keep_all = TRUE) %>% + distinct(id_hex, abbrev_muni, mode, peak, .keep_all = TRUE) %>% setDT() # setdiff(hex_dt_ativo$id_hex, points$id_hex) @@ -237,6 +254,8 @@ organizar_base_acess <- function(ano) { # aplicar funcao # a <- lapply(munis_df$abrev_muni, purrr::possibly(join_by_muni, otherwise = "erro")) + # teste: ui <- munis_df$abrev_muni[munis_df$abrev_muni %nlike% "bsb"] + # a <- lapply(munis_df$abrev_muni[munis_df$abrev_muni %nlike% "bsb"], join_by_muni) a <- lapply(munis_df$abrev_muni, join_by_muni) b <- purrr::transpose(a)