Skip to content

Commit

Permalink
exporting to GCS as large mosaics
Browse files Browse the repository at this point in the history
  • Loading branch information
bbest committed Sep 21, 2023
1 parent 36fd072 commit 44582ee
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 16 deletions.
93 changes: 90 additions & 3 deletions export_spp_gee2gcs.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ if (!"librarian" %in% installed.packages())

# devtools::install_bitbucket("bklamer/rcrypt") # aquamapsdata dependency
librarian::shelf(
raquamaps/aquamapsdata, dplyr, ggplot2, glue, here, jsonlite,
raquamaps/aquamapsdata, dplyr, ggplot2, fs, glue, here, jsonlite,
leaflet, leaflet.extras2, listviewer,
purrr, raster, rgee, reticulate, scales, sf,
#shiny,
stringr, tibble, tidyr)
stringr, terra, tibble, tidyr)
select = dplyr::select
source(here("sp-map/functions.R"))

Expand Down Expand Up @@ -57,5 +57,92 @@ source(here("sp-map/functions.R"))
am_db <- default_db("sqlite") # ~/.config/aquamaps/am.db

# list of species
am_spp <- get_am_spp()
am_spp <- get_am_spp() |>
arrange(SpeciesID)

# sample for export
spp_ids <- am_spp$SpeciesID[1:10]

# source(here("sp-map/functions.R"))
lst_spp_info <- lapply(spp_ids, get_sp_info)

lst_spp_coarse <- map(lst_spp_info, calc_im_sp_coarse)

im_spp <- ee$ImageCollection(lst_spp_coarse)$toBands()$rename(spp_ids)
# im_spp$bandNames()$getInfo()

prj_im <- im_spp$projection()
# crs_im <- im_spp$projection()$crs()$getInfo() # "EPSG:4326"
# transform_im <- im_spp$projection()$transform() # "PARAM_MT[\"Affine\", \n PARAMETER[\"num_row\", 3], \n PARAMETER[\"num_col\", 3]]"
ply_globe <- ee$Geometry$BBox(-180,-89.9999,180,89.9999)$transform("EPSG:4326", 0.001)

ic_gebco <- ee$ImageCollection("projects/sat-io/open-datasets/gebco/gebco_sub-ice-topo")
im_am <- ee$Image("projects/eq-am-fine/assets/sdmpredictors/am-hcaf_v1-simple")

prj_fine <- ic_gebco$first()$projection()$getInfo()
prj_coarse <- im_am$projection()$getInfo()

# export as GeoTIFF (not cloud-optimized)
task_im_spp <- ee_image_to_gcs(
image = im_spp,
description = "im-spp10-coarse-not-cog-v03_to_gcs",
bucket = "sdm-env_gebco-global",
fileNamePrefix = "im-spp10-coarse_v03",
crs = prj_coarse$crs(),
scale = prj_coarse$nominalScale()$getInfo(), # v3: with scale
# crsTransform = cat(prj_coarse$transform()$getInfo()),
# https://github.com/r-spatial/rgee/blob/219d84917300ca378f395d75a4372fbbeefba73b/R/ee_download.R#L335C4-L336
# @param crsTransform A comma-separated string of 6 numbers describing
# the affine transform of the coordinate reference system of the exported
# crsTransform = "3,3,0.5,-180.0,-0.5,90.0",
# list(PARAM_MT = list(
# Affine = list(
# PARAMETER = list(
# num_row = 3)))),
# PARAMETER["num_col", 3],
# PARAMETER["elt_0_0", 0.5],
# PARAMETER["elt_0_2", -180.0],
# PARAMETER["elt_1_1", -0.5],
# PARAMETER["elt_1_2", 90.0]]
# crsTransform = prj_im$transform(), # v01: commented out
maxPixels = 1e13,
region = ply_globe,
skipEmptyTiles = TRUE,
fileFormat = 'GeoTIFF') # 25 min for single on EE
task_im_spp$start()
ee_monitoring(task_im_spp)
# ID: TLMWVPWDQAS5N22KXDSSWX7I
# Phase: Completed
# Runtime: 7s (started 2023-09-21 11:16:21 -0700)
# Attempted 1 time
# Batch compute usage: 4.4424 EECU-seconds


lst_spp_info <- lapply(spp_ids, get_sp_info)

lst_spp_f <- map(lst_spp_info, calc_im_sp_fine)

im_spp_f <- ee$ImageCollection(lst_spp_f)$toBands()$rename(spp_ids)
# im_spp$bandNames()$getInfo()

task_im_spp <- ee_image_to_gcs(
image = im_spp_f,
description = "im-spp10-fine-not-cog-v08_to_gcs", # without region
bucket = "sdm-env_gebco-global",
fileNamePrefix = "im-spp10-fine-not-cog_v08",
# crs = prj_im$crs(),
# crsTransform = prj_fine$transform()$getInfo(), # v01: commented out
# "PARAM_MT[\"Affine\", \n PARAMETER[\"num_row\", 3], \n PARAMETER[\"num_col\", 3]]"
crs = prj_fine$crs,
crsTransform = prj_fine$transform,
# scale = prj_coarse$nominalScale()$getInfo(), # v3: with scale
# scale = 111319.5/240,
maxPixels = 1e13,
region = ply_globe,
# skipEmptyTiles = TRUE,
fileFormat = 'GeoTIFF') # 25 min for single on EE
task_im_spp$start()
ee_monitoring(task_im_spp)
# im-spp10-fine-not-cog-v03_to_gcs
# ERROR in Earth Engine servers: Unable to transform edge (360.000000, 76.000000 to 359.999802, 76.000000) from EPSG:4326 PLANAR [0.5, 0.0, 0.0, 0.0, -0.5, 0.0] to EPSG:4326.

42 changes: 29 additions & 13 deletions sp-map/functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,7 @@ get_am_spp <- function(){
am_spp <- tbl(am_db, "speciesoccursum_r") |>
select(
SpeciesID, SpecCode, Genus, Species) |>

distinct() |>
distinct() |>
collect() |>
mutate(
sp_sci = glue("{Genus} {Species}")) |>
Expand All @@ -232,16 +231,33 @@ get_am_spp <- function(){
# am_spp$Order |> unique() |> length() # 298
}

get_sp_info <- function(sp_code){
get_sp_info <- function(sp_key){
# sp_key matches hspen_r.Speccode if numeric, otherwise
# hspen_r.SpeciesID

# TODO:
# - look at speciesoccursum_r.StockDefs multiple stock definitions / lifestage per sp?
# tbl(am_db, "speciesoccursum_r")

if (is.numeric(sp_key)){
d_hspen <- tbl(am_db, "hspen_r") |>
filter(Speccode == !!sp_key) |>
collect() |>
# TODO: filter by sp_id vs sp_code
# eg sp_code == 46802:
# "sp_scientific": ["Ammodytoides gilli", "Isopora palifera"]
# "sp_id" : ["Fis-138579" , "W-Scl-730686"]
slice(1)
} else {
d_hspen <- tbl(am_db, "hspen_r") |>
filter(SpeciesID == !!sp_key) |>
collect() |>
slice(1)
}

d_hspen <- tbl(am_db, "hspen_r") |>
filter(Speccode == !!sp_code) |>
collect() |>
# TODO: filter by sp_id vs sp_code
# eg sp_code == 46802:
# "sp_scientific": ["Ammodytoides gilli", "Isopora palifera"]
# "sp_id" : ["Fis-138579" , "W-Scl-730686"]
slice(1)
# browser()

message(glue("sp_key: {sp_key}"))

vars_yes <- d_hspen |>
select(ends_with("YN")) |>
Expand Down Expand Up @@ -280,8 +296,8 @@ get_sp_info <- function(sp_code){

sp_info <- list()

d_sp <- am_spp |>
filter(SpecCode == sp_code)
d_sp <- tbl(am_db, "speciesoccursum_r") |>
filter(SpeciesID == d_hspen$SpeciesID)
sp_info["sp_scientific"] <- list(d_sp$sp_sci)
sp_info["sp_code"] <- list(d_sp$SpecCode)
sp_info["sp_id"] <- list(d_sp$SpeciesID)
Expand Down

0 comments on commit 44582ee

Please sign in to comment.