## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

## ----setup--------------------------------------------------------------------
library(a5R)

## -----------------------------------------------------------------------------
cell <- a5_lonlat_to_cell(-3.19, 55.95, resolution = 10)
cell

## -----------------------------------------------------------------------------
a5_cell_to_lonlat(cell)

## ----boundary-plot, fig.width = 5, fig.height = 4-----------------------------
boundary <- a5_cell_to_boundary(cell)
boundary

plot(boundary, col = "#206ead20", border = "#206ead", asp = 1)

## -----------------------------------------------------------------------------
parent <- a5_cell_to_parent(cell)
parent

children <- a5_cell_to_children(cell)
children

## ----hierarchy-plot, fig.width = 5, fig.height = 4----------------------------
supercell <- a5_cell_to_supercell(cell)
subcells <- a5_uncompact(a5_cell_to_subcells(cell))

plot(a5_cell_to_boundary(c(supercell, cell, subcells)), border = NA,
     asp = 1 / cos(56 * pi / 180))
plot(a5_cell_to_boundary(subcells), col = "#ad6e2020", border = "#ad6e20", add = TRUE)
plot(a5_cell_to_boundary(cell), col = "#206ead40", border = "#206ead", lwd = 2, add = TRUE)
plot(a5_cell_to_boundary(supercell), border = "#333333", lwd = 2, add = TRUE)
plot(a5_cell_to_lonlat(cell), pch = 19, add = TRUE)

## -----------------------------------------------------------------------------
a5_cell_area(0:5)
a5_cell_edge_length_avg(0:5, units = "km")

## -----------------------------------------------------------------------------
children
covering <- a5_compact(children)
covering

# round-trips back to the original cells
a5_uncompact(covering)

## ----traversal, fig.width = 7, fig.height = 4---------------------------------
disk <- a5_grid_disk(cell, k = 10)
cap <- a5_spherical_cap(cell, radius = 50, units = "km")
c(disk, cap)

plot(a5_cell_to_boundary(a5_uncompact(cap)), col = "#6ead2020", border = "#6ead20",
     asp = 1 / cos(56 * pi / 180))
plot(a5_cell_to_boundary(a5_uncompact(disk)), col = "#206ead20", border = "#206ead",
     add = TRUE)

## ----grid-plot, fig.width = 7, fig.height = 4---------------------------------
covering <- a5_polygon_to_cells(wk::rct(-3.3, 55.9, -3.1, 56.0), resolution = 12)
covering

cells <- a5_uncompact(covering)
plot(a5_cell_to_boundary(cells), col = "#206ead20", border = "#206ead",
     asp = 1 / cos(56 * pi / 180))

## ----containment-plot, fig.width = 7, fig.height = 4--------------------------
overlapping <- a5_polygon_to_cells(wk::rct(-3.3, 55.9, -3.1, 56.0),
                                   resolution = 12, containment = "overlapping")
overlapping

plot(a5_cell_to_boundary(a5_uncompact(overlapping)),
     col = "#ad6e2020", border = "#ad6e20", asp = 1 / cos(56 * pi / 180))
plot(a5_cell_to_boundary(cells),
     col = "#206ead40", border = "#206ead", add = TRUE)
plot(wk::rct(-3.3, 55.9, -3.1, 56.0), border = "#333333", lwd = 2, add = TRUE)

## ----eval = requireNamespace("sf", quietly = TRUE), fig.width = 7, fig.height = 3.5----
library(sf)
demo(nc, ask = FALSE, echo = FALSE)
nc$cells <- a5_polygon_to_cells(nc, resolution = 9)
nc[1:3, c("NAME", "cells")]

plot(a5_cell_to_boundary(a5_uncompact(nc$cells)),
     col = "#03030320", border = "#6d20adff", asp = 1)

## ----linestring-a5R, fig.width = 7, fig.height = 3----------------------------
a5R_strokes <- wk::wkt(
  "MULTILINESTRING (
    (1.8 52.5, 1.2 52.8, 0.6 52.5, 0.4 52, 0.6 51.5, 1.2 51.2, 1.8 51.5, 2 52, 1.8 52.5, 2 51.2),
    (5 53, 3.5 53, 3.5 52.2, 4 52.2, 4.7 52, 5 51.6, 4.7 51.2, 4 51, 3.5 51.1),
    (6.5 51, 6.5 53, 7.5 53, 8 52.8, 8.2 52.5, 8 52.2, 7.5 52, 6.5 52, 8.2 51)
  )"
)

cells <- a5_line_string_to_cells(a5R_strokes, resolution = 9)
length(cells)

plot(a5_cell_to_boundary(cells),
     col = "#206ead80", border = "#37af6d", asp = 1 / cos(52 * pi / 180))

