-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfabdem.py
More file actions
153 lines (134 loc) · 4.63 KB
/
Copy pathfabdem.py
File metadata and controls
153 lines (134 loc) · 4.63 KB
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
# ---
# title: Download FABDEM for US and Canada
# author: Brendan Casey
# created: 2026-07-10
# inputs:
# - FABDEM ImageCollection
# (projects/sat-io/open-datasets/FABDEM)
# - FAO GAUL country boundaries (FAO/GAUL/2015/level0)
# outputs:
# - DEM GeoTIFF for US and Canada (exported to Google
# Drive)
# notes:
# This script downloads the FABDEM digital elevation
# model for the United States and Canada. It mosaics the
# collection, clips to the US + Canada boundaries, and
# exports a GeoTIFF to Google Drive. Clipping directly to
# the feature collection avoids geometry edge limits. Map
# visualization layers (hillshade, ocean mask, elevation
# palette) from the original GEE JavaScript are dropped.
#
# Setup (once):
# pip install earthengine-api
# earthengine authenticate
# Then set EE_PROJECT in _gee_config.py to your
# registered Earth Engine cloud project and run the
# script.
# ---
import os
import sys
import ee
# Make utils importable regardless of the working
# directory VS Code runs the script from
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from _gee_config import DRIVE_FOLDER
from utils.compute_report import ComputeReport
from utils.gee_utils import export_image_to_drive, initialize_ee
# 1. Setup ----
# 1.1 User parameters ----
EXPORT_SCALE = 30 # meters
EXPORT_CRS = "EPSG:4326"
PRINT_STATS = False # min/max check (slow for large AOIs)
USE_TEST_AOI = True # True: small test AOI; False: US+Canada
COMPUTE_REPORT = True # write EECU usage report (txt);
# blocks until the export task finishes
# 1.2 Initialize Earth Engine ----
# Project ID is read from _gee_config.py
initialize_ee()
# 1.3 Set up compute usage report ----
# Profiles EECU usage per section and per export task.
# Best used with USE_TEST_AOI = True to find choke
# points cheaply before a full US + Canada run.
report = ComputeReport(
"fabdem",
enabled=COMPUTE_REPORT,
)
# 2. Define study area ----
# This section defines the export geometry. It uses a
# small test polygon when USE_TEST_AOI is True; otherwise
# it filters the FAO GAUL countries for the US and Canada.
# Clipping directly to the feature collection avoids
# geometry edge limits.
if USE_TEST_AOI:
# Small aoi for testing purposes
aoi = ee.Geometry.Polygon([
[-113.5, 55.5], # Top-left corner
[-113.5, 55.0], # Bottom-left corner
[-112.8, 55.0], # Bottom-right corner
[-112.8, 55.5], # Top-right corner
])
else:
# US and Canada country features
aoi = ee.FeatureCollection("FAO/GAUL/2015/level0").filter(
ee.Filter.Or(
ee.Filter.eq("ADM0_NAME", "Canada"),
ee.Filter.eq(
"ADM0_NAME", "United States of America"
),
)
)
# The export region and stats reducer both need an
# ee.Geometry; derive one from the AOI (which may be a
# FeatureCollection when USE_TEST_AOI is False).
if USE_TEST_AOI:
aoi_geom = aoi
else:
aoi_geom = aoi.geometry()
# 3. Create and clip elevation mosaic ----
# This section mosaics the FABDEM collection and clips it
# to the study area. It produces a clipped elevation image.
fabdem = ee.ImageCollection(
"projects/sat-io/open-datasets/FABDEM"
)
elev = fabdem.mosaic().setDefaultProjection(
"EPSG:3402", None, 30
)
elev_clipped = elev.clip(aoi)
# 3.1 Check min and max values (optional) ----
# Also runs when COMPUTE_REPORT is on: Earth Engine is
# lazy, so the profiler needs an evaluated computation
# (getInfo) to measure per-algorithm EECU usage.
if PRINT_STATS or COMPUTE_REPORT:
with report.section("Elevation min/max (reduceRegion)"):
stats = elev_clipped.reduceRegion(
reducer=ee.Reducer.minMax(),
geometry=aoi_geom,
scale=EXPORT_SCALE,
maxPixels=1e13,
bestEffort=True,
).getInfo()
print("Elevation min and max values:", stats)
# 4. Export data ----
# This section exports the clipped elevation image to
# Google Drive as a GeoTIFF. Set wait=True to block until
# the task finishes; otherwise monitor progress at
# https://code.earthengine.google.com/tasks
task = export_image_to_drive(
image=elev_clipped,
description="FABDEM_US_Canada",
region=aoi_geom,
folder=DRIVE_FOLDER,
file_name_prefix="fabdem_us_canada",
scale=EXPORT_SCALE,
crs=EXPORT_CRS,
max_pixels=1e13,
wait=False,
)
# 5. Compute usage report ----
# This section waits for the export to finish, records
# its total EECU-seconds, and writes the txt report to
# gee_compute_reports/. Note: a full US + Canada export
# can take hours; for a quick profile use the test AOI.
report.log_task(task)
report.write()
# End of script ----