Skip to content

Remove zone stats code #533

@Robinlovelace

Description

@Robinlovelace

There is a lot of clunky code involved here for not a lot of benefit. Will be good to remove it.

Details
  tar_target(commute_stats_baseline, {
    stats = sf::st_drop_geometry(od_commute_subset)
    stats = aadt_adjust(stats, purpose = "commute", aadt_parameters = aadt_parameters)
    stats_from = dplyr::group_by(stats, geo_code1) |>
      dplyr::summarise(
        all = sum(all, na.rm = TRUE),
        bicycle = sum(bicycle, na.rm = TRUE),
        car = sum(car, na.rm = TRUE),
        foot = sum(foot, na.rm = TRUE),
        public_transport = sum(public_transport, na.rm = TRUE),
        taxi = sum(taxi, na.rm = TRUE)
      )
    stats_to = dplyr::group_by(stats, geo_code2) |>
      dplyr::summarise(
        all = sum(all, na.rm = TRUE),
        bicycle = sum(bicycle, na.rm = TRUE),
        car = sum(car, na.rm = TRUE),
        foot = sum(foot, na.rm = TRUE),
        public_transport = sum(public_transport, na.rm = TRUE),
        taxi = sum(taxi, na.rm = TRUE)
      )

    names(stats_from)[1] = "DataZone"
    names(stats_to)[1] = "DataZone"

    names(stats_from)[2:ncol(stats_from)] = paste0("comm_orig_", names(stats_from)[2:ncol(stats_from)])
    names(stats_to)[2:ncol(stats_to)] = paste0("comm_dest_", names(stats_to)[2:ncol(stats_to)])
    stats = dplyr::full_join(stats_from, stats_to, by = "DataZone")
    stats
  }),
  tar_target(commute_stats_fastest, {
    make_commute_stats(uptake_commute_fastest, "fastest")
  }),
  tar_target(commute_stats_quietest, {
    make_commute_stats(uptake_commute_quietest, "quietest")
  }),
  tar_target(commute_stats_ebike, {
    make_commute_stats(uptake_commute_ebike, "ebike")
  }),
  tar_target(commute_stats_balanced, {
    make_commute_stats(uptake_commute_balanced, "balanced")
  }),


  # School Zone stats ---------------------------------------------------------

  tar_target(school_stats_baseline, {
    stats = sf::st_drop_geometry(od_school)
    stats = aadt_adjust(stats, purpose = "school", aadt_parameters = aadt_parameters)
    stats = dplyr::group_by(stats, SeedCode, schooltype) |>
      dplyr::summarise(
        all = sum(all, na.rm = TRUE),
        bicycle = sum(bicycle, na.rm = TRUE),
        car = sum(car, na.rm = TRUE),
        foot = sum(foot, na.rm = TRUE),
        public_transport = sum(public_transport, na.rm = TRUE),
        other = sum(other, na.rm = TRUE)
      )

    stats$schooltype = paste0("schl_", stats$schooltype, "_dest")


    stats = tidyr::pivot_wider(stats,
      id_cols = c("SeedCode"),
      names_from = c("schooltype"),
      names_glue = "{schooltype}_{.value}",
      values_from = names(stats)[3:ncol(stats)]
    )
    stats
  }),
  tar_target(school_stats_from_baseline, {
    stats = sf::st_drop_geometry(od_school)
    stats = aadt_adjust(stats, purpose = "school", aadt_parameters = aadt_parameters)
    stats = dplyr::group_by(stats, DataZone, schooltype) |>
      dplyr::summarise(
        all = sum(all, na.rm = TRUE),
        bicycle = sum(bicycle, na.rm = TRUE),
        car = sum(car, na.rm = TRUE),
        foot = sum(foot, na.rm = TRUE),
        public_transport = sum(public_transport, na.rm = TRUE),
        other = sum(other, na.rm = TRUE)
      )

    stats$schooltype = paste0("schl_", stats$schooltype, "_orig")

    stats = tidyr::pivot_wider(stats,
      id_cols = c("DataZone"),
      names_from = c("schooltype"),
      names_glue = "{schooltype}_{.value}",
      values_from = names(stats)[3:ncol(stats)]
    )
    stats
  }),
  tar_target(school_stats_fastest, {
    make_school_stats(uptake_school_fastest, "fastest")
  }),
  tar_target(school_stats_quietest, {
    make_school_stats(uptake_school_quietest, "quietest")
  }),
  tar_target(school_stats_ebike, {
    make_school_stats(uptake_school_ebike, "ebike")
  }),
  tar_target(school_stats_balanced, {
    make_school_stats(uptake_school_balanced, "balanced")
  }),
  tar_target(school_stats_from_fastest, {
    make_school_stats_from(uptake_school_fastest, "fastest")
  }),
  tar_target(school_stats_from_quietest, {
    make_school_stats_from(uptake_school_quietest, "quietest")
  }),
  tar_target(school_stats_from_ebike, {
    make_school_stats_from(uptake_school_ebike, "ebike")
  }),
  tar_target(school_stats_from_balanced, {
    make_school_stats_from(uptake_school_balanced, "balanced")
  }),

  # Combine stats  ---------------------------------------------------------

  tar_target(school_stats, {
    # Ebike routes for ebike scenario
    ebike = school_stats_ebike
    fastest = school_stats_fastest
    ebike = ebike[, !grepl("go_dutch", names(ebike))]
    # Can't use quietness/hilliness for ebike
    ebike = ebike[, !grepl("(quietness|hilliness)", names(ebike))]
    fastest = fastest[, !grepl("ebike", names(fastest))]
    names(ebike) = gsub("_ebike$", "_fastest", names(ebike))

    stats = dplyr::left_join(school_stats_baseline, fastest, by = "SeedCode")
    stats = dplyr::left_join(stats, ebike, by = "SeedCode")
    stats = dplyr::left_join(stats, school_stats_quietest, by = "SeedCode")
    stats
  }),
  tar_target(school_stats_from, {
    # Ebike routes for ebike scenario
    ebike = school_stats_from_ebike
    fastest = school_stats_from_fastest
    ebike = ebike[, !grepl("go_dutch", names(ebike))]
    # Can't use quietness/hilliness for ebike
    ebike = ebike[, !grepl("(quietness|hilliness)", names(ebike))]
    fastest = fastest[, !grepl("ebike", names(fastest))]
    names(ebike) = gsub("_ebike$", "_fastest", names(ebike))

    stats = dplyr::left_join(school_stats_from_baseline, fastest, by = "DataZone")
    stats = dplyr::left_join(stats, ebike, by = "DataZone")
    stats = dplyr::left_join(stats, school_stats_from_quietest, by = "DataZone")
    stats
  }),
  tar_target(commute_stats, {
    # Ebike routes for ebike scenario
    ebike = commute_stats_ebike
    fastest = commute_stats_fastest
    ebike = ebike[, !grepl("go_dutch", names(ebike))]
    # Can't use quietness/hilliness for ebike
    ebike = ebike[, !grepl("(quietness|hilliness)", names(ebike))]
    fastest = fastest[, !grepl("ebike", names(fastest))]
    names(ebike) = gsub("_ebike$", "_fastest", names(ebike))

    stats = dplyr::left_join(commute_stats_baseline, fastest, by = "DataZone")
    stats = dplyr::left_join(stats, ebike, by = "DataZone")
    stats = dplyr::left_join(stats, commute_stats_quietest, by = "DataZone")
    stats
  }),
  tar_target(zones_stats, {
    stats = dplyr::full_join(commute_stats, school_stats_from, by = "DataZone")
    stats = dplyr::full_join(stats, utility_stats, by = "DataZone")
    stats
  }),

  # Now covered in build.R:
  # tar_target(zones_stats_json, {
  #   export_zone_json(zones_stats, "DataZone", path = region_folder)
  # }),

  # tar_target(school_stats_json, {
  #   export_zone_json(school_stats, "SeedCode", path = region_folder)
  # }),

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions