Skip to content

Instantly share code, notes, and snippets.

@cboettig
Last active May 12, 2025 19:37
Show Gist options
  • Save cboettig/25b961067d082c1150ca882151bc0432 to your computer and use it in GitHub Desktop.
Save cboettig/25b961067d082c1150ca882151bc0432 to your computer and use it in GitHub Desktop.
Convert rasters to approximate corresponding h3geo.org hex index
unlink("mobi.xyz")
remotes::install_github("cboettig/duckdbfs")
library(duckdbfs)
library(dplyr)
library(sf)
tracts_z8 = open_dataset("https://minio.carlboettiger.info/public-social-vulnerability/2022-tracts-h3-z8.parquet", recursive = FALSE) |>
mutate(h8 = tolower(h8))
# ~ 30 sec. We could speed up by downscaling with the warper at the same time
sf::gdal_utils("warp",
"/vsicurl/https://minio.carlboettiger.info/public-mobi/mobi-species-richness.tif",
"mobi.xyz",
options = c("-t_srs", "EPSG:4326", "-of", "XYZ"))
# ~ fast
combined <- open_dataset("mobi.xyz",
recursive = FALSE,
format = "csv",
parser_options =
c(delim = "' '",
columns = "{'X': 'FLOAT', 'Y': 'FLOAT', 'Z': 'INTEGER'}")
) |>
mutate(h8 = h3_latlng_to_cell_string(Y, X, 8L)) |>
inner_join(tracts_z8)
# Average the species richness to the county level
combined |> group_by(COUNTY) |> summarize(mean_richness = mean(Z))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment