-
Notifications
You must be signed in to change notification settings - Fork 0
/
export_spp_gee2gcs.R
178 lines (152 loc) · 7.65 KB
/
export_spp_gee2gcs.R
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
175
176
source(here::here("sp-map/global.R"))
librarian::shelf(
fs, terra)
## On MacBook initial ----
# python: /opt/homebrew/bin/python3
# libpython: /opt/homebrew/opt/[email protected]/Frameworks/Python.framework/Versions/3.10/lib/python3.10/config-3.10-darwin/libpython3.10.dylib
# pythonhome: /opt/homebrew/Cellar/[email protected]/3.10.9/Frameworks/Python.framework/Versions/3.10:/opt/homebrew/Cellar/[email protected]/3.10.9/Frameworks/Python.framework/Versions/3.10
# version: 3.10.9 (main, Dec 15 2022, 17:11:09) [Clang 14.0.0 (clang-1400.0.29.202)]
# numpy: /opt/homebrew/lib/python3.10/site-packages/numpy
# numpy_version: 1.24.1
# ee: /opt/homebrew/lib/python3.10/site-packages/ee
#
## On MacBook install in Terminal
# brew install pyenv
# pyenv install 3.8.6
#
# ee_install(
# py_env = "rgee",
# earthengine_version = ee_version(),
# python_version = "3.8.6",
# confirm = interactive())
# rgee::ee_check()
# ◉ Python version
# ✔ [Ok] /opt/venv/rgee/bin/python v3.8
# ◉ Python packages:
# ✔ [Ok] numpy
# ✔ [Ok] earthengine-api
# aquamaps database ----
am_db <- default_db("sqlite") # ~/.config/aquamaps/am.db
# list of species
am_spp <- get_am_spp() |>
arrange(SpeciesID)
# sample for export
spp_ids <- am_spp$SpeciesID[1:10]
# sp_key = spp_ids[1]
# source(here("sp-map/functions.R"))
lst_spp_info <- lapply(spp_ids, get_sp_info)
# listviewer::jsonedit(lst_spp_info[1:3])
# lst_spp_coarse <- map(lst_spp_info, calc_im_sp_coarse)
lst_spp <- map(lst_spp_info, calc_im_sp_fine) # Possible error: "reached elapsed time limit"
# im_spp <- ee$ImageCollection(lst_spp_coarse)$toBands()$rename(spp_ids)
im_spp <- ee$ImageCollection(lst_spp)$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)
# ERROR in Earth Engine servers: Unable to transform edge (5440.000977, 21408.000000 to 5440.001099, 21408.000000) from EPSG:4326 PLANAR [0.004166666666666667, 0.0, 0.0, 0.0, -0.004166666666666667, 0.0] to EPSG:4326.
ply_globe <- ee$Geometry$BBox(-180,-89.99,180,89.99)$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_10spp_v4-fine-not-cog_to_gcs",
bucket = "sdm-env_gebco-global",
fileNamePrefix = "im_10spp_v4-fine",
# crs = prj_coarse$crs(),
# scale = prj_coarse$nominalScale()$getInfo(), # v3: with scale
crs = prj_fine$crs()$getInfo(),
scale = prj_fine$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)
# State: FAILED
# ERROR in Earth Engine servers: Unable to transform edge (5440.000977, 21408.000000 to 5440.001099, 21408.000000) from EPSG:4326 PLANAR [0.004166666666666667, 0.0, 0.0, 0.0, -0.004166666666666667, 0.0] to EPSG:4326.
# 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.
# test gcs tifs locally -----
# gsutil -m cp \
# "gs://sdm-env_gebco-global/im-spp10-fine-not-cog_v07_2023_09_21_20_52_260000020736-0000062208.tif" \
# "gs://sdm-env_gebco-global/im-spp10-fine-not-cog_v07_2023_09_21_20_52_260000000000-0000020736.tif" \
# "gs://sdm-env_gebco-global/im-spp10-fine-not-cog_v07_2023_09_21_20_52_260000020736-0000020736.tif" \
# "gs://sdm-env_gebco-global/im-spp10-fine-not-cog_v07_2023_09_21_20_52_260000020736-0000041472.tif" \
# "gs://sdm-env_gebco-global/im-spp10-fine-not-cog_v07_2023_09_21_20_52_260000020736-0000000000.tif" \
# "gs://sdm-env_gebco-global/im-spp10-fine-not-cog_v07_2023_09_21_20_52_260000000000-0000041472.tif" \
# "gs://sdm-env_gebco-global/im-spp10-fine-not-cog_v07_2023_09_21_20_52_260000000000-0000062208.tif" \
# "gs://sdm-env_gebco-global/im-spp10-fine-not-cog_v07_2023_09_21_20_52_260000000000-0000000000.tif" \
# "gs://sdm-env_gebco-global/im-spp10-fine-not-cog_v07_2023_09_21_20_52_260000041472-0000041472.tif" \
# "gs://sdm-env_gebco-global/im-spp10-fine-not-cog_v07_2023_09_21_20_52_260000020736-0000082944.tif" \
# "gs://sdm-env_gebco-global/im-spp10-fine-not-cog_v07_2023_09_21_20_52_260000000000-0000082944.tif" \
# "gs://sdm-env_gebco-global/im-spp10-fine-not-cog_v07_2023_09_21_20_52_260000041472-0000000000.tif" \
# "gs://sdm-env_gebco-global/im-spp10-fine-not-cog_v07_2023_09_21_20_52_260000041472-0000020736.tif" \
# "gs://sdm-env_gebco-global/im-spp10-fine-not-cog_v07_2023_09_21_20_52_260000041472-0000062208.tif" \
# "gs://sdm-env_gebco-global/im-spp10-fine-not-cog_v07_2023_09_21_20_52_260000041472-0000082944.tif" \
# .
librarian::shelf(
# gdalcubes,
leaflet, mapview, stars, terra)
dir_tif <- "/Users/bbest/Desktop/am-fine_im-dl"
tifs <- list.files(dir_tif, pattern=".tif$", full.names = TRUE)
m <- st_mosaic(tifs, options = c("-vrtnodata", "0"))
m
s <- read_stars(m)
# proxy = T,
# RasterIO = list(
# # nXSize = 240, nYSize = 240,
# bands = c(1)))
d <- s[,,,2] |> # select bands # 1: NZ funk
st_downsample(200)
d_t <- rast(d) |>
terra::trim()
plot(d_t)
plet(d_t, tile=providers$CartoDB.DarkMatter)
mapView(stars::st_as_stars(d_t))