File size: 4,210 Bytes
2c1140f
cc1034c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2c1140f
639cb71
3cf94ac
639cb71
3cf94ac
639cb71
 
3551e8c
3cf94ac
 
 
 
 
 
 
 
 
 
 
72ec702
3cf94ac
 
3551e8c
 
2c1140f
fd48dc7
 
 
 
 
72ec702
 
fd48dc7
3cf94ac
2c1140f
72ec702
 
2c1140f
3cf94ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
639cb71
3cf94ac
639cb71
3551e8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174

source("utils.R")
source("inat-ranges.R")

url <- "https://s3-west.nrp-nautilus.io/public-data/cache/inat/f0108ef86feababffeab3d2be6f09373.h3j"
#url <-  "https://minio.carlboettiger.info/public-data/cache/inat/4fd3323845ee860b39609783566b2212.h3j"

url <- "https://minio.carlboettiger.info/public-data/cache/inat/f0108ef86feababffeab3d2be6f09373.h3j"

#x <- jsonlite::read_json(url)

 m = maplibre(center = c(-110,37), zoom=3) |>
    add_draw_control() |>
    add_h3j_source("h3j_source",
                  url = url
    ) |>
    add_fill_extrusion_layer(
      id = "h3j_layer",
      source = "h3j_source",
      tooltip = "n",
      fill_extrusion_color = viridis_pal("height"),
      fill_extrusion_height = list(
        "interpolate",
        list("linear"),
        list("zoom"),
        0,
        0, 1,
        list("*", 100000, list("get", "height"))
      ),
      fill_extrusion_opacity = 0.7
    )


htmlwidgets::saveWidget(m, "test2.html")


## Illustrate/test core app functionality without shiny
library(dplyr)
library(duckdbfs)
library(ggplot2)
library(mapgl)
library(glue)
library(jsonlite)
load_h3()
load_spatial()

source("utils.R")
source("inat-ranges.R")


duckdbfs::duckdb_secrets()
inat <- open_dataset("s3://public-inat/hex")

aoi <- spData::us_states
aoi <- spData::world

# publish richness at the aoi (bbox or poly)
meta <- richness(inat, aoi)
m <- richness_map(meta)
m
library(htmlwidgets)
htmlwidgets::saveWidget(m, "total-richness.html")


# publish richness at the aoi (bbox or poly)
meta <- richness(inat, aoi, rank = "class", taxon = "Aves")
m <- richness_map(meta)
htmlwidgets::saveWidget(m, "aves-richness.html")

# publish richness at the aoi (bbox or poly)
meta <- richness(inat, aoi, rank = "class", taxon = "Mammalia")
m <- richness_map(meta)
htmlwidgets::saveWidget(m, "mammals-richness.html")


## UGH can't deal with antimeridian
# dropme <- antimeridian_hexes(3)
# dropme <- antimeridian_hexes(4) |> rename(h4 = h3id)
# inat |> anti_join(dropme) |> write_dataset("s3://public-inat/ranges.parquet")
# inat <- open_dataset("s3://public-inat/ranges.parquet", recursive = FALSE)

# mutate(h3 = h3_cell_to_parent(h4, 3L))



m <- maplibre(center = c(-110.5, 34.8), zoom = 4) |> add_draw_control() 
richness_map(m, "https://minio.carlboettiger.info/public-data/inat-tmp-ranges.h3j")











library(htmlwidgets)
htmlwidgets::saveWidget(m, "example.html")












amphib = open_dataset("s3://public-inat/polygon/Amphibia.parquet", recursive = FALSE)

gdf <- amphib |> 
filter(name == "Ambystoma californiense") |>
 to_sf(crs=4326)

maplibre(center = c(-122.5, 37.8), zoom = 4) |>
      add_source(id = "gdf", gdf) |>
      add_layer("gdf-layer",
                type = "fill",
                source = "gdf",
                paint = list(
                  "fill-color" =  "darkgreen",
                  "fill-opacity" = .9
                )
        )
      




# Access SVI
#svi = open_dataset("https://minio.carlboettiger.info/public-social-vulnerability/2022/SVI2022_US_tract.parquet")
#tracts = open_dataset("https://minio.carlboettiger.info/public-social-vulnerability/2022-tracts-h3-z5.parquet") # Access CalEnviroScreen
# ces = open_dataset("https://minio.carlboettiger.info/public-calenviroscreen/ces_2021.parquet", format="parquet")

# Filter GBIF to our area-of-interest (h-index) and species of interest

ca <- tracts |>
  filter(STATE == "California") |>
  mutate(h4 = h3_cell_to_parent(h5, 4L)) |>
  mutate(h4 = tolower(as.character(h4)))

out <- ca |>
 inner_join(inat, by = "h4") |>
 count(STATE, COUNTY, FIPS, h5)


#  mutate(height = n / max(n)) |>

url = "https://minio.carlboettiger.info/public-data/cache/inat/cec4b3087f0b6c41ecc384da2521f97c.h3j"
 maplibre() |>
    add_draw_control() |>
    add_h3j_source("h3j_source",
                  url = url
    ) |>
    add_fill_extrusion_layer(
      id = "h3j_layer",
      source = "h3j_source",
      tooltip = "n",
      fill_extrusion_color = viridis_pal("height"),
      fill_extrusion_height = list(
        "interpolate",
        list("linear"),
        list("zoom"),
        0,
        0, 1,
        list("*", 100000, list("get", "height"))
      ),
      fill_extrusion_opacity = 0.7
    )