File size: 1,491 Bytes
c399c9b
5a7aca2
c399c9b
5a7aca2
 
c399c9b
5a7aca2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

library(dplyr)
library(duckdbfs)
library(sf)
duckdbfs::load_h3()

as_dataset.sf <- function(sf, ...) {
  # cludgy way to get polygon into duckdb as spatial data
  tmp <- tempfile(fileext = ".fgb")
  sf |> sf::st_transform(4326) |> sf::write_sf(tmp, append = FALSE)
  aoi <- duckdbfs::open_dataset(tmp, ...)

  aoi
}

get_h3_aoi <- function(aoi, zoom = 0L, precision = 6L) {

  zoom <- as.integer(zoom)

  # consider auto-retry at higher precision if subset is empty.
  precision <- as.integer(precision)

  res <- paste0("h", precision)
  # multipolygon dump may not be needed for draw tools.
  h3_aoi <- aoi |>
    mutate(poly = array_extract(unnest(st_dump(geom)),"geom"),
          h3id = h3_polygon_wkt_to_cells(poly,{precision}),
          h3id = unnest(h3id)
          ) |>
    mutate(h0 = h3_h3_to_string( h3_cell_to_parent(h3id, {zoom})),
           h3id = h3_h3_to_string (h3id) ) |>
    mutate(h0 = toupper(h0), h3id = toupper(h3id)) |>
    select(h0, h3id) |>
    as_view("h3_aoi")
}

hex_res <- function(x) {
    x |>
    utils::head(1) |>
    dplyr::mutate(res = h3_get_resolution(h3id)) |>
    dplyr::pull(res)
}

hex_join <- function(x,y) {
  res_x <- hex_res(x)
  res_y <- hex_res(y)

  if (res_x > res_y) {
    y <- y |> 
      dplyr::mutate(h3id = unnest(h3_cell_to_children(h3id, {res_x})),
                    h3id = toupper(h3id))
  }
    if (res_x < res_y) {
    y <- y |> 
      dplyr::mutate(h3id = h3_cell_to_parent(h3id, {res_x}))
  }

  dplyr::inner_join(x, y)
}