R/mapalib.R

Defines functions get_minoritarian_ideology_map get_minoritarian_map get_majoritarian_map_data get_map get_map_points get_map_data

# mapalib.R
# (c) 2020 CincoNoveSeis Jornalismo Ltda.
#
# This file is licensed under the GNU General Public License, version 3.

#' @export
get_map_data = function(year, position, cities, epsg, aggregate_fun, ..., source_order = NULL, districts = NULL, download_fun = download_section_votes) {
  purrr::map_dfr(cities, function(city_id) {
    ibge_data = download_ibge_data(city_id)

    tracts = download_tracts(city_id, epsg) %>%
      dplyr::mutate(rn = dplyr::row_number())

    if (!is.null(districts)) {
      tracts = tracts %>%
        dplyr::filter(name_district %in% districts)
    }

    geocoded_sections = download_geocoded_sections(year, city_id) %>%
      append_chosen_latlon_to_geocoded_sections()

    votes_sum0 = download_fun(year, position, city_id)
    votes_sum = votes_sum0 %>%
      aggregate_fun(position, ...)

    tracts_with_voting_data0 = tracts %>%
      append_sections_to_tracts(dplyr::filter(geocoded_sections, ano == year), epsg) %>%
      dplyr::semi_join(votes_sum, by = c('zona' = 'NUM_ZONA', 'secao' = 'NUM_SECAO')) %>%
      dplyr::semi_join(ibge_data, by = c('code_tract' = 'Cod_setor')) %>%
      #dplyr::distinct(code_tract, rn) %>%
      dplyr::select(code_tract, rn, geom)

    tracts_with_voting_data = tracts_with_voting_data0 %>%
      dplyr::distinct() %>%
      dplyr::mutate(main = rn)

    mw = mapwalk(tracts_with_voting_data, tracts, tracts_with_voting_data$main, epsg) %>%
      append_sections_to_tracts(dplyr::filter(geocoded_sections, ano == year), epsg) %>%
      dplyr::left_join(votes_sum, by = c('zona' = 'NUM_ZONA', 'secao' = 'NUM_SECAO')) %>%
      dplyr::inner_join(ibge_data, by = c('code_tract' = 'Cod_setor'))

    #mw %>%
    #  dplyr::distinct(main, zona, secao) %>%
    #  write.csv('__latest_main_zona_secao_corr.csv', row.names = F)

    mw %>%
      dplyr::group_by(main) %>%
      dplyr::summarize(dplyr::across(dplyr::starts_with('cand'), mean, na.rm = T),
                       dplyr::across(dplyr::starts_with('abs_votes'), sum, na.rm = T),
                       renda = mean(basico_V009, na.rm = T))
  })
}

#' @export
get_map_points = function(year, position, cities, epsg, aggregate_fun, ..., source_order = NULL, districts = NULL) {
  purrr::map_dfr(cities, function(city_id) {
    tracts = download_tracts(city_id, epsg) %>%
      dplyr::mutate(rn = dplyr::row_number())

    if (!is.null(districts)) {
      tracts = tracts %>%
        dplyr::filter(name_district %in% districts)
    }

    geocoded_sections = download_geocoded_sections(year, city_id) %>%
      append_chosen_latlon_to_geocoded_sections()

    votes_sum = download_section_votes(year, position, city_id) %>%
      aggregate_fun(position, ...)

    geocoded_sections %>%
      dplyr::filter(ano == year) %>%
      get_section_points(tracts, epsg) %>%
      dplyr::inner_join(votes_sum, c('zona' = 'NUM_ZONA', 'secao' = 'NUM_SECAO'))
  })
}

get_map = function(...) {
  get_map_data(..., gen_map = T)
}

get_majoritarian_map_data = function(year, cities, epsg, position, turno, party_number) {
  get_map_data(year, cities, epsg, aggregate_majoritarian, position, turno, party_number)
}

get_minoritarian_map = function(year, cities, position, candidate_or_party_number, source_order) {
  NULL
}

get_minoritarian_ideology_map = function(year, cities, positions = c(6, 7), source_order) {
  NULL
}
pindograma/mapalib documentation built on Oct. 12, 2024, 4:35 p.m.