|
| 1 | +# should this also support richness or single species? |
| 2 | +inat_rangemap <- function( |
| 3 | + poly, |
| 4 | + zoom, |
| 5 | + id_column = "id", |
| 6 | + taxa_selections = list(), |
| 7 | + server = Sys.getenv("AWS_S3_ENDPOINT", "minio.carlboettiger.info"), |
| 8 | + bucket = "public-data/cache/gbif-app" |
| 9 | +) { |
| 10 | + poly_hexed <- get_h3_aoi(poly, precision = zoom, keep_cols = id_column) |
| 11 | +} |
| 12 | + |
| 13 | + |
| 14 | +get_inat_hexes <- function( |
| 15 | + poly, |
| 16 | + zoom = 4L, |
| 17 | + id_column = "id", |
| 18 | + taxa_selections = list(), |
| 19 | + server = Sys.getenv("AWS_S3_ENDPOINT", "minio.carlboettiger.info"), |
| 20 | + bucket = "public-data/cache/gbif-app" |
| 21 | +) { |
| 22 | + duckdbfs::load_h3() |
| 23 | + |
| 24 | + # get_h3_aoi is self-caching, shared across metrics |
| 25 | + poly_hexed_url <- get_h3_aoi(poly, precision = zoom, keep_cols = id_column) |
| 26 | + poly_hexed <- duckdbfs::open_dataset(poly_hexed_url, recursive = FALSE) |
| 27 | + |
| 28 | + inat <- |
| 29 | + open_dataset("s3://public-inat/hex") |> |
| 30 | + filter_gbif_taxa(taxa_selections) |
| 31 | + |
| 32 | + # handle alternate resolutions on the fly? Or precompute these? |
| 33 | + if (zoom < 4) { |
| 34 | + inat <- inat |> mutate(h3id = unnest(h3_cell_to_children(h4, zoom))) |
| 35 | + } else if (zoom > 4) { |
| 36 | + inat <- inat |> |
| 37 | + mutate(h3id = h3_cell_to_parent(h4, zoom)) |> |
| 38 | + select(-h4) |> |
| 39 | + distinct() # is it worthwhile dropping duplicate rows? |
| 40 | + } else { |
| 41 | + inat <- inat |> rename(h3id = h4) |
| 42 | + } |
| 43 | + |
| 44 | + inat <- inat |> |
| 45 | + dplyr::inner_join(poly_hexed) |> |
| 46 | + dplyr::count(h3id) |> |
| 47 | + dplyr::mutate(logn = log(n), value = logn / max(logn)) |> |
| 48 | + dplyr::mutate( |
| 49 | + geom = ST_GeomFromText( |
| 50 | + h3_cell_to_boundary_wkt(h3id) |
| 51 | + ) |
| 52 | + ) |
| 53 | + |
| 54 | + ## this part should be separate? Or be included in cache logic. |
| 55 | + label <- "inat" |
| 56 | + hash <- digest::digest(list(inat, zoom, id_column, label)) |
| 57 | + s3 <- glue::glue("s3://{bucket}/{label}/{hash}.geojson") |
| 58 | + duckdbfs::to_geojson(inat, s3, as_http = TRUE) |
| 59 | +} |
| 60 | + |
| 61 | +get_inat_zonal <- function( |
| 62 | + poly, |
| 63 | + zoom = 4L, |
| 64 | + id_column = "id", |
| 65 | + taxa_selections = list(), |
| 66 | + server = Sys.getenv("AWS_S3_ENDPOINT", "minio.carlboettiger.info"), |
| 67 | + bucket = "public-data/cache/gbif-app" |
| 68 | +) { |
| 69 | + duckdbfs::load_h3() |
| 70 | + |
| 71 | + # get_h3_aoi is self-caching, shared across metrics |
| 72 | + poly_hexed_url <- get_h3_aoi( |
| 73 | + poly, |
| 74 | + precision = zoom, |
| 75 | + keep_cols = id_column, |
| 76 | + h3_column = "h3id" |
| 77 | + ) |
| 78 | + poly_hexed <- |
| 79 | + duckdbfs::open_dataset(poly_hexed_url, recursive = FALSE) |> |
| 80 | + dplyr::mutate(h3id = tolower(h3id)) |
| 81 | + |
| 82 | + inat <- |
| 83 | + open_dataset("s3://public-inat/hex") |> |
| 84 | + filter_inat_taxa(taxa_selections) |
| 85 | + |
| 86 | + print("POSITION 1") |
| 87 | + zoom <- as.integer(zoom) |
| 88 | + # handle alternate resolutions on the fly? Or precompute these? |
| 89 | + if (zoom > 4) { |
| 90 | + inat <- inat |> mutate(h3id = unnest(h3_cell_to_children(h4, zoom))) |
| 91 | + } else if (zoom < 4) { |
| 92 | + inat <- inat |> |
| 93 | + mutate(h3id = h3_cell_to_parent(h4, zoom)) |> |
| 94 | + select(-h4) |> |
| 95 | + distinct() # is it worth dropping duplicates? |
| 96 | + } else { |
| 97 | + inat <- inat |> rename(h3id = h4) |
| 98 | + } |
| 99 | + |
| 100 | + print("POSITION 2") |
| 101 | + |
| 102 | + print(inat) |
| 103 | + print(poly_hexed) |
| 104 | + |
| 105 | + inat <- inat |> |
| 106 | + dplyr::inner_join(poly_hexed) |> |
| 107 | + dplyr::count(.data[[id_column]]) |> |
| 108 | + dplyr::mutate(logn = log(n), value = logn / max(logn)) |
| 109 | + |
| 110 | + # join back to poly with geoms |
| 111 | + poly <- poly |> |
| 112 | + dplyr::select(dplyr::all_of(id_column), geometry) |> |
| 113 | + dplyr::inner_join(inat, by = id_column) |> |
| 114 | + rename(geom = "geometry") |
| 115 | + |
| 116 | + ## this part should be separate? Or be included in cache logic. |
| 117 | + label <- "inat" |
| 118 | + hash <- digest::digest(list(poly, zoom, id_column, label)) |
| 119 | + s3 <- glue::glue("s3://{bucket}/{label}/{hash}.geojson") |
| 120 | + duckdbfs::to_geojson(poly, s3, as_http = TRUE) |
| 121 | +} |
| 122 | + |
| 123 | +# FIXME do the filter! |
| 124 | +filter_inat_taxa <- function(df, taxa_list) { |
| 125 | + df |
| 126 | +} |
0 commit comments