cboettig commited on
Commit
fd48dc7
·
1 Parent(s): 9d335d0

:butterfly:

Browse files
Files changed (3) hide show
  1. app.R +10 -8
  2. inat-ranges.R +20 -9
  3. test.R +11 -3
app.R CHANGED
@@ -49,12 +49,15 @@ server <- function(input, output, session) {
49
  if(file.exists(cache)) {
50
  meta <- jsonlite::read_json(cache)
51
  } else {
52
- meta <- list(center = c(-110, 37), zoom=4)
 
 
 
53
  }
54
 
55
  m <- maplibre(center = meta$center, zoom = meta$zoom) |>
56
  add_draw_control()
57
- richness_map(m)
58
 
59
  })
60
 
@@ -70,19 +73,18 @@ server <- function(input, output, session) {
70
  aoi <- spData::us_states
71
  }
72
 
 
73
  if (input$filter) {
74
- inat <- taxa |>
75
- filter(.data[[input$rank]] == input$taxon) |>
76
- select(taxon_id) |>
77
- inner_join(inat, by = "taxon_id")
78
  }
79
 
80
  message("Computing richness...")
81
- clock <- richness(inat, aoi)
82
 
83
  center <- st_coordinates(st_centroid(st_as_sfc(st_bbox(aoi))))
84
  zoom <- input$map_zoom
85
- jsonlite::write_json(list(center = c(center), zoom = zoom), cache)
86
 
87
  session$reload()
88
 
 
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
 
 
73
  aoi <- spData::us_states
74
  }
75
 
76
+ rank <- taxon <- NULL
77
  if (input$filter) {
78
+ rank <- input$rank
79
+ taxon <- input$taxon
 
 
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
 
inat-ranges.R CHANGED
@@ -4,7 +4,22 @@ library(mapgl)
4
 
5
  # Also requires get_h3_aoi() from utils.R
6
 
7
- richness <- function(inat, aoi) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
  h3_aoi <- get_h3_aoi(aoi, precision = 4) |> select(h3id)
10
 
@@ -16,17 +31,17 @@ richness <- function(inat, aoi) {
16
  group_by(h3id) |>
17
  summarise(n = n()) |>
18
  mutate(height = n / max(n)) |>
19
- duckdbfs::to_h3j("s3://public-data/inat-tmp-ranges.h3j")
20
  # write_dataset("s3://public-data/inat-tmp-ranges.parquet")
21
  })
22
 
23
- return(clock)
 
24
  }
25
 
26
  richness_map <- function(
27
  m = maplibre(),
28
- url = "https://minio.carlboettiger.info/public-data/inat-tmp-ranges.h3j",
29
- aoi = NULL
30
  ) {
31
 
32
  m <- m |>
@@ -49,10 +64,6 @@ richness_map <- function(
49
  ),
50
  fill_extrusion_opacity = 0.7
51
  )
52
- if(!is.null(aoi)) {
53
- # center <- st_coordinates(st_centroid(st_as_sfc(st_bbox(aoi))))
54
- # m <- m |> set_view(center = center, zoom = 3)
55
- }
56
 
57
  return(m)
58
  }
 
4
 
5
  # Also requires get_h3_aoi() from utils.R
6
 
7
+ richness <- function(inat, aoi, rank = NULL, taxon = NULL) {
8
+
9
+ taxa <- open_dataset("https://minio.carlboettiger.info/public-inat/taxonomy/taxa.csv", format = "csv", recursive = FALSE)
10
+
11
+ hash <- digest::digest(list(aoi, rank, taxon))
12
+ s3 <- paste0("s3://public-data/cache/inat/", hash, ".h3j")
13
+
14
+ # check if hash exists
15
+
16
+ # filter
17
+ if (!is.null(rank) && !is.null(taxon)) {
18
+ inat <- taxa |>
19
+ filter(.data[[rank]] == taxon) |>
20
+ select(taxon_id) |>
21
+ inner_join(inat, by = "taxon_id")
22
+ }
23
 
24
  h3_aoi <- get_h3_aoi(aoi, precision = 4) |> select(h3id)
25
 
 
31
  group_by(h3id) |>
32
  summarise(n = n()) |>
33
  mutate(height = n / max(n)) |>
34
+ duckdbfs::to_h3j(s3)
35
  # write_dataset("s3://public-data/inat-tmp-ranges.parquet")
36
  })
37
 
38
+ url <- gsub("s3://", "https://minio.carlboettiger.info/", s3)
39
+ return(url)
40
  }
41
 
42
  richness_map <- function(
43
  m = maplibre(),
44
+ url = "https://minio.carlboettiger.info/public-data/inat-tmp-ranges.h3j"
 
45
  ) {
46
 
47
  m <- m |>
 
64
  ),
65
  fill_extrusion_opacity = 0.7
66
  )
 
 
 
 
67
 
68
  return(m)
69
  }
test.R CHANGED
@@ -15,12 +15,20 @@ duckdbfs::duckdb_secrets()
15
  inat <- open_dataset("s3://public-inat/hex")
16
 
17
  aoi <- spData::us_states
18
-
19
- taxa <- open_dataset("https://minio.carlboettiger.info/public-inat/taxonomy/taxa.csv", format = "csv", recursive = FALSE)
20
 
21
  # publish richness at the aoi (bbox or poly)
22
  richness(inat, aoi)
23
- richness_map()
 
 
 
 
 
 
 
 
 
24
 
25
 
26
 
 
15
  inat <- open_dataset("s3://public-inat/hex")
16
 
17
  aoi <- spData::us_states
18
+ aoi <- spData::world
 
19
 
20
  # publish richness at the aoi (bbox or poly)
21
  richness(inat, aoi)
22
+ m <- richness_map()
23
+
24
+ library(htmlwidgets)
25
+ htmlwidgets::saveWidget(m, "total-richness.html")
26
+
27
+
28
+ # publish richness at the aoi (bbox or poly)
29
+ richness(inat, aoi, rank = "class", taxon = "Aves")
30
+ m <- richness_map()
31
+ htmlwidgets::saveWidget(m, "aves-richness.html")
32
 
33
 
34