Skip to content

Commit

Permalink
hexes
Browse files Browse the repository at this point in the history
  • Loading branch information
cboettig committed Dec 5, 2023
1 parent d1ef998 commit 65182a7
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 22 deletions.
4 changes: 2 additions & 2 deletions _freeze/contents/ace/execute-results/html.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"hash": "997de147c6ee92af63b756f4c50126d6",
"hash": "7eabe4980b9671ea9ed0849d2d8c9f65",
"result": {
"markdown": "---\ntitle: ACE Data\n---\n\n\n\nCalifornia Department of Fish and Wildlife (CDFW) provides a series of data products to identify \"areas of conservation emphasis\" (ACE), which are currently being used as part of [California's 30x30 conservation initiative](https://www.californianature.ca.gov/pages/30x30). At a recent event at the California Academy, one of the CDFW scientists mentioned it would be nice to see how the Biodiversity Intactness Index (BII) values compare to the CDFW prioritization layers. The BII (sometimes called the local or LBII) is one of a handful of indicators of global biodiversity change highlighted by [GEO BON](https://geobon.org/ebvs/indicators/). Various attempts to estimate this indicator have been made over the past decades -- the most recent and most high-resolution one currently available (and updated annually) is the Impact Observatory product based on the approach of Newbold et al and the PREDICTS database, which is [available in cloud-optimized format](https://planetarycomputer.microsoft.com/dataset/io-biodiversity) from the Planetary Computer STAC catalog. \n\nAside from drowning in acronyms, this task provides a great chance to apply the same tools seen in our [intro](intro.html) at a larger spatial scale. We will average the rasterized 100m projections from the BII over each of the 63,890 polygon tiles used in the CDFW data. Even though we are now dealing with a raster layer that involves a very derived product (BII) from a different provider (Planetary Computer), and a much larger set of polygons from a different source and in different scale and projection, the process is almost identical to the intro example. \n\n\n\n\n::: {.cell}\n\n```{.r .cell-code}\nlibrary(stars)\nlibrary(rstac)\nlibrary(gdalcubes)\nlibrary(tmap)\nlibrary(dplyr)\n\ngdalcubes::gdalcubes_options(parallel=24*2)\n```\n:::\n\n\nCDFW uses a hex-based tiling scheme for their data products. \nNote that these pre-date the modern [H3 hex tiling system](https://h3geo.org/), so do not provide the magic of [hierarchical (zoom level) tiles](https://h3geo.org/docs/highlights/indexing).\nCDFW makes the [ACE GIS Data](https://wildlife.ca.gov/Data/Analysis/Ace) freely available, but in one large zip archive. \nThe ACE data includes may different layers all using the same hex-based tiles. \nThe layer we pull here draws from their summary rankings on Terrestrial Irreplacability of species biodiversity.\n\nHere I'll use a public mirror on a Berkeley-based server for a more cloud-native access pattern so we don't have to download the whole thing. We also index this in a simple [stac catalog](https://radiantearth.github.io/stac-browser/#/external/raw.githubusercontent.com/schmidtDSE/biodiversity-catalog/main/stac/v1/collections/cdfw_ace/summary.json?.asset=asset-ds2715)\n\n\n\n::: {.cell}\n\n```{.r .cell-code}\nurl <- \"/vsicurl/https://minio.carlboettiger.info/public-biodiversity/ACE_summary/ds2715.gdb\"\n```\n:::\n\n::: {.cell}\n\n```{.r .cell-code}\nace <- st_read(url) \n```\n:::\n\n::: {.cell}\n\n```{.r .cell-code}\ntm_shape(ace) + tm_polygons(fill = \"RwiRankEco\", col=NA)\n```\n\n::: {.cell-output-display}\n![](ace_files/figure-html/unnamed-chunk-5-1.png){width=672}\n:::\n:::\n\n\nFind Biodiversity Intactness Index COG tiles from Planetary Computer using STAC search:\n\n\n::: {.cell}\n\n```{.r .cell-code}\nbox <- st_bbox(st_transform(ace, 4326))\nitems <-\n stac(\"https://planetarycomputer.microsoft.com/api/stac/v1\") |>\n stac_search(collections = \"io-biodiversity\",\n bbox = c(box),\n limit = 1000) |>\n post_request() |>\n items_sign(sign_fn = sign_planetary_computer())\n```\n:::\n\n\nCreated a desired cube view \n\n\n::: {.cell}\n\n```{.r .cell-code}\ncube <- gdalcubes::stac_image_collection(items$features, asset_names = \"data\")\nbox <- st_bbox(ace)\nv <- cube_view(srs = \"EPSG:3310\",\n extent = list(t0 = \"2020-07-01\", t1 = \"2020-07-31\",\n left = box[1], right = box[3],\n top = box[4], bottom = box[2]),\n dx = 100, dy=100, dt = \"P1M\")\n\nQ <- raster_cube(cube,v)\n```\n:::\n\n\nQuick plot of the data at requested cube resolution. \n\n\n::: {.cell}\n\n```{.r .cell-code}\nQ |> plot(col=viridisLite::viridis(30), nbreaks=31)\n```\n\n::: {.cell-output-display}\n![](ace_files/figure-html/unnamed-chunk-8-1.png){width=672}\n:::\n:::\n\n\nExtract mean value of the BII for each hex.\n\n\n::: {.cell}\n\n```{.r .cell-code}\nbii <- Q |> gdalcubes::extract_geom(ace, FUN=mean)\n```\n:::\n\n\n\n## Plots\n\nView the average BII index values by ACE hexagon:\n\n\n::: {.cell}\n\n```{.r .cell-code}\ntmap_mode(\"plot\")\n\n# color palette\nviridis <- tm_scale_continuous(values = viridisLite::viridis(30))\n\nbii <- ace |>\n tibble::rowid_to_column(\"FID\") |> \n left_join(bii)\n\ntm_shape(bii) + \n tm_polygons(fill = \"data\", col=NA, fill.scale = viridis)\n```\n\n::: {.cell-output-display}\n![](ace_files/figure-html/unnamed-chunk-10-1.png){width=672}\n:::\n:::\n\n\nLet's zoom in manually:\n\n\n::: {.cell}\n\n```{.r .cell-code}\ntmap_mode(\"plot\")\nsf = st_bbox(c(xmin=-123, ymin=38.5, xmax=-122, ymax=37.5), crs=4326)\ntm_shape(bii, bbox = sf) + \n tm_polygons(fill = \"data\", col=NA,\n fill.scale = viridis)\n```\n\n::: {.cell-output-display}\n![](ace_files/figure-html/unnamed-chunk-11-1.png){width=672}\n:::\n:::\n\n\nNow that we have the data in convenient and familiar data structures, it is easy to analyze.\nOn average, a hexagon's irreplacability rank shows little correlation with BII:\n\n\n::: {.cell}\n\n```{.r .cell-code}\nlibrary(tidyverse)\nbii |> as_tibble() |> select(-Shape) |>\n group_by(RwiRankEco) |> \n summarise(BII = mean(data, na.rm=TRUE),\n sd = sd(data, na.rm=TRUE)) |>\n ggplot(aes(RwiRankEco, BII)) + \n geom_col(aes(fill=RwiRankEco)) + \n geom_linerange(aes(ymin = BII-2*sd, ymax = BII+2*sd), col = \"grey50\")\n```\n\n::: {.cell-output-display}\n![](ace_files/figure-html/unnamed-chunk-12-1.png){width=672}\n:::\n:::\n\n\n\n## Leaflet map\n\nWe can also render an interactive leaflet plot where we can zoom in and out and toggle basemaps:\n\n\n\n```{.r .cell-code}\ntmap_mode(\"view\")\n\nmap <- \n tm_shape(bii) +\n tm_polygons(fill = \"data\", col=NA, fill.scale = viridis) +\n tm_shape(ace) +\n tm_polygons(fill = \"RwiRankEco\", col=NA)\ntmap::tmap_save(map, \"bii_hexes.html\")\n```\n\n\n\n[interactive map](bii_hexes.html)\n\n",
"markdown": "---\ntitle: ACE Data\n---\n\n\n\nCalifornia Department of Fish and Wildlife (CDFW) provides a series of data products to identify \"areas of conservation emphasis\" (ACE), which are currently being used as part of [California's 30x30 conservation initiative](https://www.californianature.ca.gov/pages/30x30). At a recent event at the California Academy, one of the CDFW scientists mentioned it would be nice to see how the Biodiversity Intactness Index (BII) values compare to the CDFW prioritization layers. The BII (sometimes called the local or LBII) is one of a handful of indicators of global biodiversity change highlighted by [GEO BON](https://geobon.org/ebvs/indicators/). Various attempts to estimate this indicator have been made over the past decades -- the most recent and most high-resolution one currently available (and updated annually) is the Impact Observatory product based on the approach of Newbold et al and the PREDICTS database, which is [available in cloud-optimized format](https://planetarycomputer.microsoft.com/dataset/io-biodiversity) from the Planetary Computer STAC catalog. \n\nAside from drowning in acronyms, this task provides a great chance to apply the same tools seen in our [intro](intro.html) at a larger spatial scale. We will average the rasterized 100m projections from the BII over each of the 63,890 polygon tiles used in the CDFW data. Even though we are now dealing with a raster layer that involves a very derived product (BII) from a different provider (Planetary Computer), and a much larger set of polygons from a different source and in different scale and projection, the process is almost identical to the intro example. \n\n\n\n\n::: {.cell}\n\n```{.r .cell-code}\nlibrary(stars)\nlibrary(rstac)\nlibrary(gdalcubes)\nlibrary(tmap)\nlibrary(dplyr)\n\ngdalcubes::gdalcubes_options(parallel=24*2)\n```\n:::\n\n\nCDFW uses a hex-based tiling scheme for their data products. \nNote that these pre-date the modern [H3 hex tiling system](https://h3geo.org/), so do not provide the magic of [hierarchical (zoom level) tiles](https://h3geo.org/docs/highlights/indexing).\nCDFW makes the [ACE GIS Data](https://wildlife.ca.gov/Data/Analysis/Ace) freely available, but in one large zip archive. \nThe ACE data includes may different layers all using the same hex-based tiles. \nThe layer we pull here draws from their summary rankings on Terrestrial Irreplacability of species biodiversity.\n\nHere I'll use a public mirror on a Berkeley-based server for a more cloud-native access pattern so we don't have to download the whole thing. We also index this in a simple [stac catalog](https://radiantearth.github.io/stac-browser/#/external/raw.githubusercontent.com/schmidtDSE/biodiversity-catalog/main/stac/v1/collections/cdfw_ace/summary.json?.asset=asset-ds2715)\n\n\n\n::: {.cell}\n\n```{.r .cell-code}\nurl <- \"/vsicurl/https://minio.carlboettiger.info/public-biodiversity/ACE_summary/ds2715.gdb\"\n```\n:::\n\n::: {.cell}\n\n```{.r .cell-code}\nace <- st_read(url) \n```\n:::\n\n::: {.cell}\n\n```{.r .cell-code}\ntm_shape(ace) + tm_polygons(fill = \"RwiRankEco\", col=NA)\n```\n\n::: {.cell-output-display}\n![](ace_files/figure-html/unnamed-chunk-5-1.png){width=672}\n:::\n:::\n\n\nFind Biodiversity Intactness Index COG tiles from Planetary Computer using STAC search:\n\n\n::: {.cell}\n\n```{.r .cell-code}\nbox <- st_bbox(st_transform(ace, 4326))\nitems <-\n stac(\"https://planetarycomputer.microsoft.com/api/stac/v1\") |>\n stac_search(collections = \"io-biodiversity\",\n bbox = c(box),\n limit = 1000) |>\n post_request() |>\n items_sign(sign_fn = sign_planetary_computer())\n```\n:::\n\n\nCreated a desired cube view \n\n\n::: {.cell}\n\n```{.r .cell-code}\ncube <- gdalcubes::stac_image_collection(items$features, asset_names = \"data\")\nbox <- st_bbox(ace)\nv <- cube_view(srs = \"EPSG:3310\",\n extent = list(t0 = \"2020-07-01\", t1 = \"2020-07-31\",\n left = box[1], right = box[3],\n top = box[4], bottom = box[2]),\n dx = 100, dy=100, dt = \"P1M\")\n\nQ <- raster_cube(cube,v)\n```\n:::\n\n\nQuick plot of the data at requested cube resolution. \n\n\n::: {.cell}\n\n```{.r .cell-code}\nQ |> plot(col=viridisLite::viridis(30), nbreaks=31)\n```\n\n::: {.cell-output-display}\n![](ace_files/figure-html/unnamed-chunk-8-1.png){width=672}\n:::\n:::\n\n\nExtract mean value of the BII for each hex.\n\n\n::: {.cell}\n\n```{.r .cell-code}\nbii <- Q |> gdalcubes::extract_geom(ace, FUN=mean)\n```\n:::\n\n\n\n## Plots\n\nView the average BII index values by ACE hexagon:\n\n\n::: {.cell}\n\n```{.r .cell-code}\ntmap_mode(\"plot\")\n\n# color palette\nviridis <- tm_scale_continuous(values = viridisLite::viridis(30))\n\nbii <- ace |>\n tibble::rowid_to_column(\"FID\") |> \n left_join(bii)\n\ntm_shape(bii) + \n tm_polygons(fill = \"data\", col=NA, fill.scale = viridis)\n```\n\n::: {.cell-output-display}\n![](ace_files/figure-html/unnamed-chunk-10-1.png){width=672}\n:::\n:::\n\n\nLet's zoom in manually:\n\n\n::: {.cell}\n\n```{.r .cell-code}\ntmap_mode(\"plot\")\nsf = st_bbox(c(xmin=-123, ymin=38.5, xmax=-122, ymax=37.5), crs=4326)\ntm_shape(bii, bbox = sf) + \n tm_polygons(fill = \"data\", col=NA,\n fill.scale = viridis)\n```\n\n::: {.cell-output-display}\n![](ace_files/figure-html/unnamed-chunk-11-1.png){width=672}\n:::\n:::\n\n\nNow that we have the data in convenient and familiar data structures, it is easy to analyze.\nOn average, a hexagon's irreplacability rank shows little correlation with BII:\n\n\n::: {.cell}\n\n```{.r .cell-code}\nlibrary(tidyverse)\nbii |> as_tibble() |> select(-Shape) |>\n group_by(RwiRankEco) |> \n summarise(BII = mean(data, na.rm=TRUE),\n sd = sd(data, na.rm=TRUE)) |>\n ggplot(aes(RwiRankEco, BII)) + \n geom_col(aes(fill=RwiRankEco)) + \n geom_linerange(aes(ymin = BII-2*sd, ymax = BII+2*sd), col = \"grey50\")\n```\n\n::: {.cell-output-display}\n![](ace_files/figure-html/unnamed-chunk-12-1.png){width=672}\n:::\n:::\n\n\n\n## Leaflet map\n\nWe can also render an interactive leaflet plot where we can zoom in and out and toggle basemaps:\n\n\n\n```{.r .cell-code}\ntmap_mode(\"view\")\nbii <- Q |> st_as_stars()\nmap <- \n tm_shape(bii) +\n tm_raster(col.scale = viridis) +\n tm_shape(ace) +\n tm_polygons(fill = \"RwiRankEco\", col=NA)\n\ntmap_save(map, \"bii_hexes.html\")\n```\n\n\n\n[interactive map](bii_hexes.html)\n\n",
"supporting": [
"ace_files"
],
Expand Down
Binary file modified _freeze/contents/ace/figure-html/unnamed-chunk-8-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 0 additions & 14 deletions _freeze/gdalcubes-stac-cog/execute-results/html.json

This file was deleted.

7 changes: 4 additions & 3 deletions contents/ace.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,14 @@ We can also render an interactive leaflet plot where we can zoom in and out and

```{r results = "asis"}
tmap_mode("view")
bii <- Q |> st_as_stars()
map <-
tm_shape(bii) +
tm_polygons(fill = "data", col=NA, fill.scale = viridis) +
tm_raster(col.scale = viridis) +
tm_shape(ace) +
tm_polygons(fill = "RwiRankEco", col=NA)
tmap::tmap_save(map, "bii_hexes.html")
tmap_save(map, "bii_hexes.html")
```


Expand Down
7 changes: 4 additions & 3 deletions contents/bii_hexes.html

Large diffs are not rendered by default.

0 comments on commit 65182a7

Please sign in to comment.