cboettig commited on
Commit
3551e8c
·
1 Parent(s): bffaf86
Files changed (3) hide show
  1. app.R +9 -11
  2. inat-ranges.R +17 -11
  3. test.R +30 -6
app.R CHANGED
@@ -48,16 +48,17 @@ server <- function(input, output, session) {
48
  # Hacky -- we sidecar the metadata here
49
  if(file.exists(cache)) {
50
  meta <- jsonlite::read_json(cache)
 
51
  } else {
52
- meta <- list(center = c(-110, 37),
 
53
  zoom = 4,
54
  url = paste0("https://minio.carlboettiger.info/",
55
  "public-data/inat-tmp-ranges.h3j"))
56
  }
57
-
58
- m <- maplibre(center = meta$center, zoom = meta$zoom) |>
59
- add_draw_control()
60
- richness_map(m, url = meta$url)
61
 
62
  })
63
 
@@ -80,12 +81,9 @@ server <- function(input, output, session) {
80
  }
81
 
82
  message("Computing richness...")
83
- url <- richness(inat, aoi, rank, taxon)
84
-
85
- center <- st_coordinates(st_centroid(st_as_sfc(st_bbox(aoi))))
86
- zoom <- input$map_zoom
87
- jsonlite::write_json(list(center = c(center), zoom = zoom, url = url), cache)
88
-
89
  session$reload()
90
 
91
  # UGH not sure why this fails, we manually reload instead!
 
48
  # Hacky -- we sidecar the metadata here
49
  if(file.exists(cache)) {
50
  meta <- jsonlite::read_json(cache)
51
+ print(meta$url)
52
  } else {
53
+ meta <- list(X = -110,
54
+ Y = 37,
55
  zoom = 4,
56
  url = paste0("https://minio.carlboettiger.info/",
57
  "public-data/inat-tmp-ranges.h3j"))
58
  }
59
+
60
+ m <- richness_map(meta)
61
+ m
 
62
 
63
  })
64
 
 
81
  }
82
 
83
  message("Computing richness...")
84
+ meta <- richness(inat, aoi, rank, taxon, zoom = input$map_zoom)
85
+ jsonlite::write_json(meta, cache, auto_unbox = TRUE)
86
+ message(paste("rendering", meta$url))
 
 
 
87
  session$reload()
88
 
89
  # UGH not sure why this fails, we manually reload instead!
inat-ranges.R CHANGED
@@ -4,7 +4,7 @@ library(mapgl)
4
 
5
  # Also requires get_h3_aoi() from utils.R
6
 
7
- richness <- function(inat, aoi, rank = NULL, taxon = NULL) {
8
 
9
  hash <- digest::digest(list(aoi, rank, taxon))
10
  s3 <- paste0("s3://public-data/cache/inat/", hash, ".h3j")
@@ -36,18 +36,25 @@ richness <- function(inat, aoi, rank = NULL, taxon = NULL) {
36
  # write_dataset("s3://public-data/inat-tmp-ranges.parquet")
37
  })
38
 
 
 
39
  url <- gsub("s3://", "https://minio.carlboettiger.info/", s3)
40
- return(url)
 
 
 
 
 
 
41
  }
42
 
43
- richness_map <- function(
44
- m = maplibre(),
45
- url = "https://minio.carlboettiger.info/public-data/inat-tmp-ranges.h3j"
46
- ) {
47
-
48
- m <- m |>
49
  add_h3j_source("h3j_source",
50
- url = url
51
  ) |>
52
  add_fill_extrusion_layer(
53
  id = "h3j_layer",
@@ -59,8 +66,7 @@ richness_map <- function(
59
  list("linear"),
60
  list("zoom"),
61
  0,
62
- 0,
63
- 1,
64
  list("*", 100000, list("get", "height"))
65
  ),
66
  fill_extrusion_opacity = 0.7
 
4
 
5
  # Also requires get_h3_aoi() from utils.R
6
 
7
+ richness <- function(inat, aoi, rank = NULL, taxon = NULL, zoom = 3) {
8
 
9
  hash <- digest::digest(list(aoi, rank, taxon))
10
  s3 <- paste0("s3://public-data/cache/inat/", hash, ".h3j")
 
36
  # write_dataset("s3://public-data/inat-tmp-ranges.parquet")
37
  })
38
 
39
+
40
+ center <- c(st_coordinates(st_centroid(st_as_sfc(st_bbox(aoi)))))
41
  url <- gsub("s3://", "https://minio.carlboettiger.info/", s3)
42
+
43
+ meta <- list(X = center[1],
44
+ Y = center[2],
45
+ zoom = zoom,
46
+ url = url,
47
+ time = clock[[2]])
48
+ return(meta)
49
  }
50
 
51
+ richness_map <- function(meta) {
52
+
53
+ m <-
54
+ maplibre(center = c(meta$X, meta$Y), zoom = meta$zoom) |>
55
+ add_draw_control() |>
 
56
  add_h3j_source("h3j_source",
57
+ url = meta$url
58
  ) |>
59
  add_fill_extrusion_layer(
60
  id = "h3j_layer",
 
66
  list("linear"),
67
  list("zoom"),
68
  0,
69
+ 0, 1,
 
70
  list("*", 100000, list("get", "height"))
71
  ),
72
  fill_extrusion_opacity = 0.7
test.R CHANGED
@@ -6,6 +6,7 @@ library(duckdbfs)
6
  library(ggplot2)
7
  library(mapgl)
8
  library(glue)
 
9
  load_h3()
10
  load_spatial()
11
 
@@ -17,12 +18,14 @@ duckdbfs::duckdb_secrets()
17
  inat <- open_dataset("s3://public-inat/hex")
18
 
19
  aoi <- spData::us_states
20
- aoi <- spData::world
21
 
22
  # publish richness at the aoi (bbox or poly)
23
- url <- richness(inat, aoi)
24
- m <- maplibre() |> add_draw_control()
25
- m <- richness_map(url = url)
 
 
26
  m
27
  library(htmlwidgets)
28
  htmlwidgets::saveWidget(m, "total-richness.html")
@@ -34,8 +37,8 @@ m <- richness_map(url = url)
34
  htmlwidgets::saveWidget(m, "aves-richness.html")
35
 
36
  # publish richness at the aoi (bbox or poly)
37
- richness(inat, aoi, rank = "class", taxon = "Mammalia")
38
- m <- richness_map()
39
  htmlwidgets::saveWidget(m, "mammals-richness.html")
40
 
41
 
@@ -116,3 +119,24 @@ out <- ca |>
116
 
117
  # mutate(height = n / max(n)) |>
118
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  library(ggplot2)
7
  library(mapgl)
8
  library(glue)
9
+ library(jsonlite)
10
  load_h3()
11
  load_spatial()
12
 
 
18
  inat <- open_dataset("s3://public-inat/hex")
19
 
20
  aoi <- spData::us_states
21
+ #aoi <- spData::world
22
 
23
  # publish richness at the aoi (bbox or poly)
24
+ meta <- richness(inat, aoi)
25
+
26
+ toJSON(meta, auto_unbox = TRUE) |> fromJSON(meta)
27
+
28
+ m <- richness_map(meta)
29
  m
30
  library(htmlwidgets)
31
  htmlwidgets::saveWidget(m, "total-richness.html")
 
37
  htmlwidgets::saveWidget(m, "aves-richness.html")
38
 
39
  # publish richness at the aoi (bbox or poly)
40
+ url <- richness(inat, aoi, rank = "class", taxon = "Mammalia")
41
+ m <- richness_map(url = url)
42
  htmlwidgets::saveWidget(m, "mammals-richness.html")
43
 
44
 
 
119
 
120
  # mutate(height = n / max(n)) |>
121
 
122
+ url = "https://minio.carlboettiger.info/public-data/cache/inat/cec4b3087f0b6c41ecc384da2521f97c.h3j"
123
+ maplibre() |>
124
+ add_draw_control() |>
125
+ add_h3j_source("h3j_source",
126
+ url = url
127
+ ) |>
128
+ add_fill_extrusion_layer(
129
+ id = "h3j_layer",
130
+ source = "h3j_source",
131
+ tooltip = "n",
132
+ fill_extrusion_color = viridis_pal("height"),
133
+ fill_extrusion_height = list(
134
+ "interpolate",
135
+ list("linear"),
136
+ list("zoom"),
137
+ 0,
138
+ 0, 1,
139
+ list("*", 100000, list("get", "height"))
140
+ ),
141
+ fill_extrusion_opacity = 0.7
142
+ )