## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", eval = FALSE)

## ----permits------------------------------------------------------------------
# library(tampaBayOpenData)
# 
# permits <- tbod_get_dataset(
#   "construction-permits",
#   where = "PROJECTSTATUS IS NOT NULL",
#   fields = c("RECORD_ID", "PROJECTSTATUS", "LASTUPDATE"),
#   limit = 100
# )
# head(permits)
# tbod_provenance(permits)[c("matched_rows", "returned_rows", "complete", "integrity")]

## ----discovered-layer---------------------------------------------------------
# found <- tbod_search_datasets("housing", source = "live", portals = "city")
# if (nrow(found)) {
#   housing <- tbod_get_dataset(found[1, ], limit = 100)
#   tbod_provenance(housing)[c("publisher", "item_id", "validation_status")]
# }

## ----direct-layer-------------------------------------------------------------
# yard_waste <- tbod_get_arcgis_layer(
#   "https://arcgis.tampagov.net/arcgis/rest/services/OpenData/SolidWaste/MapServer/4",
#   limit = 10
# )
# tbod_provenance(yard_waste)$validation_status # "not_checked"

## ----schema-check-------------------------------------------------------------
# expected <- tbod_schema("construction-permits")
# report <- tbod_check_schema("construction-permits")
# report$status
# report$issues[, c("category", "severity", "field", "expected", "actual")]

## ----external-schema----------------------------------------------------------
# # Replace this example address with a public ArcGIS layer URL.
# url <- "https://example.org/arcgis/rest/services/Example/FeatureServer/0"
# baseline <- tbod_schema(url, refresh = TRUE)
# later <- tbod_check_schema(url, expected = baseline)

## ----spatial------------------------------------------------------------------
# projects <- tbod_get_dataset(
#   "development-cases", spatial = TRUE, out_sr = 4326
# )
# boundary <- tbod_get_dataset(
#   "city-boundary", fields = "OBJECTID", spatial = TRUE, out_sr = 4326
# )
# sf::st_crs(projects)$epsg

## ----spatial-map, fig.width=7, fig.height=5.5---------------------------------
# mapped_projects <- projects[!sf::st_is_empty(projects), ]
# 
# plot(
#   sf::st_geometry(boundary),
#   col = "#EDF4F6", border = "#7898A5", lwd = 1.2,
#   axes = TRUE, graticule = TRUE, col_graticule = "#DCE7EB",
#   xlab = "Longitude", ylab = "Latitude",
#   main = "Active development cases in Tampa"
# )
# if (nrow(mapped_projects) > 0L) {
#   case_fill <- grDevices::adjustcolor("#E76F51", alpha.f = 0.7)
#   plot(sf::st_geometry(mapped_projects), add = TRUE, pch = 21,
#        bg = case_fill, col = "#9B4934", cex = 0.85)
#   graphics::legend(
#     "bottomleft", legend = "Development case location", pch = 21,
#     pt.bg = case_fill, col = "#9B4934", bg = "white",
#     box.col = "#DCE7EB", cex = 0.85
#   )
# }

## ----download-----------------------------------------------------------------
# saved <- tbod_download_dataset(
#   "construction-permits",
#   path = "~/tampaBayOpenData-downloads/permits",
#   fields = c("OBJECTID", "RECORD_ID", "LASTUPDATE"),
#   page_size = 500,
#   total_timeout = 600
# )
# saved$complete
# first_chunk <- readRDS(saved$files[[1]])
# tbod_provenance(first_chunk)

