## ----include=FALSE------------------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", fig.width = 7, fig.height = 5)
helper <- if (file.exists("vignettes/real-example-helpers.R")) {
  "vignettes/real-example-helpers.R"
} else {
  "real-example-helpers.R"
}
source(helper)
real_mode <- bt_real_examples_enabled()

## ----setup, include=FALSE, eval=bt_real_examples_enabled()--------------------
# real <- bt_real_example_setup()
# real_aoi <- real$aoi
# available_resolution <- sort(unique(as.data.frame(real$tiles)$resolution_m))
# exact_available <- max(available_resolution)

## ----policies, eval=bt_real_examples_enabled()--------------------------------
# policies <- list(
#   native = list(resolution = "native", coverage = "warn"),
#   finest = list(resolution = "finest", coverage = "ignore"),
#   `finest + coverage fill` = list(resolution = "finest", coverage = "fill"),
#   coarsest = list(resolution = "coarsest", coverage = "ignore"),
#   `exact available native resolution` = list(resolution = exact_available, coverage = "ignore"),
#   `nearest 6 m` = list(resolution = bluertopo_resolution("nearest", value = 6), coverage = "ignore")
# )
# 
# policy_tiles <- lapply(policies, function(policy) {
#   bluertopo_tiles(
#     real_aoi,
#     resolution = policy$resolution,
#     coverage = policy$coverage,
#     quiet = TRUE
#   )
# })
# 
# policy_summary <- do.call(rbind, lapply(names(policy_tiles), function(name) {
#   tiles <- policy_tiles[[name]]
#   df <- as.data.frame(tiles)
#   coverage <- attr(tiles, "coverage")
#   data.frame(
#     policy = name,
#     selected_tile_count = nrow(df),
#     selected_resolutions = paste(sort(unique(df$resolution_m)), collapse = ", "),
#     selected_coverage_fraction = coverage$selected_coverage_fraction,
#     selected_aoi_fraction = coverage$selected_aoi_fraction,
#     target_met = coverage$target_met,
#     stringsAsFactors = FALSE
#   )
# }))

## ----policy-table, echo=FALSE, eval=bt_real_examples_enabled()----------------
# bt_display_table(policy_summary)

## ----policy-maps, echo=FALSE, eval=bt_real_examples_enabled(), fig.cap="BlueTopo tile coverage selected under different native source-resolution policies.", fig.alt="Six maps showing BlueTopo tile selections for native, finest, coverage fill, coarsest, exact, and nearest policies."----
# old_par <- par(mfrow = c(2, 3), mar = c(2, 2, 3, 1))
# for (name in names(policy_tiles)) {
#   bt_plot_tiles(
#     policy_tiles[[name]],
#     real_aoi,
#     main = name,
#     place_label = real$place,
#     label_resolutions = TRUE
#   )
# }
# par(old_par)

## ----policy-coverage-bars, echo=FALSE, eval=bt_real_examples_enabled(), fig.cap="Coverage fractions selected under different native source-resolution policies.", fig.alt="Bar plot comparing selected coverage fractions by native source-resolution policy."----
# barplot(
#   stats::setNames(policy_summary$selected_coverage_fraction, policy_summary$policy),
#   ylim = c(0, 1),
#   las = 2,
#   col = "#005f73",
#   ylab = "selected coverage fraction",
#   main = "Coverage by resolution policy"
# )
# abline(h = 1, col = "#d00000", lwd = 2, lty = 2)

## ----policy-bathy-map, echo=FALSE, eval=bt_real_examples_enabled(), fig.cap="BlueTopo bathymetry for New York Harbor, displayed with hillshade, contours, and the example-area boundary.", fig.alt="BlueTopo bathymetry for New York Harbor with hillshade, contours, and the example-area boundary."----
# policy_elevation <- bluertopo(
#   real_aoi,
#   layers = "elevation",
#   resolution = "native",
#   coverage = "fill",
#   details = TRUE,
#   progress = FALSE,
#   quiet = TRUE
# )
# 
# bt_plot_bathy_map(policy_elevation$data, real_aoi, main = "New York Harbor native policy context")

