inst/doc/traversal-and-distance.R

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

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

## -----------------------------------------------------------------------------
origin <- a5_lonlat_to_cell(35, 55, resolution = 14)

disk <- a5_grid_disk(origin, k = 20, vertex = FALSE)
disk_vertex <- a5_grid_disk(origin, k = 20, vertex = TRUE)
cap_compact <- a5_spherical_cap(origin, radius = 5000)
cap <- a5_uncompact(cap_compact, resolution = 14)

## ----heatmap, fig.width = 10, fig.height = 10---------------------------------
dcap <- a5_cell_distance(origin, cap, units = "km")
dcap_compact <- a5_cell_distance(origin, cap_compact, units = "km")
ddisk <- a5_cell_distance(origin, disk, units = "km")
ddisk_vertex <- a5_cell_distance(origin, disk_vertex, units = "km")

pal <- hcl.colors(256, "Inferno")

oldpar <- par(mfrow = c(2, 2), mar = c(2, 2, 2, 1))
for (info in list(
  list(s = disk, d = ddisk, lab = "Grid disk (edges)"),
  list(s = disk_vertex, d = ddisk_vertex, lab = "Grid disk (vertices)"),
  list(s = cap_compact, d = dcap_compact, lab = "Spherical cap (compact)"),
  list(s = cap, d = dcap, lab = "Spherical cap")
)) {
  brk <- seq(0, max(as.numeric(info$d)), length.out = 257)
  cols <- pal[findInterval(as.numeric(info$d), brk, all.inside = TRUE)]

  plot(a5_cell_to_boundary(info$s), col = cols, border = NA, asp = 1,
       main = info$lab)
  plot(a5_cell_to_boundary(origin), border = "#ffffffff", add = TRUE)
}

## ----include = FALSE----------------------------------------------------------
par(oldpar)

## ----methods, fig.width = 10, fig.height = 10---------------------------------
wide_origin <- a5_lonlat_to_cell(35, 55, resolution = 8)
wide_cap <- a5_uncompact(
  a5_spherical_cap(wide_origin, radius = 500000),
  resolution = 8
)

hav <- a5_cell_distance(wide_origin, wide_cap, units = "m", method = "haversine")
geo <- a5_cell_distance(wide_origin, wide_cap, units = "m", method = "geodesic")

diff_m <- as.numeric(geo - hav)
brk <- seq(min(diff_m), max(diff_m), length.out = 257)
cols <- pal[findInterval(diff_m, brk, all.inside = TRUE)]

oldpar <- par(no.readonly = TRUE)
layout(matrix(c(1, 2), nrow = 1), widths = c(4, 1))
par(mar = c(2, 2, 2, 1))
plot(a5_cell_to_boundary(wide_cap), col = cols, border = NA, asp = 1,
     main = "Geodesic \u2212 Haversine (m)")

par(mar = c(9, 0, 9, 9))
image(1, seq(min(diff_m), max(diff_m), length.out = 256),
      t(seq_along(pal)), col = pal, axes = FALSE, xlab = "", ylab = "")
axis(4, las = 1)

## ----include = FALSE----------------------------------------------------------
par(oldpar)

Try the a5R package in your browser

Any scripts or data that you put into this service are public.

a5R documentation built on March 26, 2026, 5:10 p.m.