File size: 1,149 Bytes
80e070e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Authenticate and initialize the Earth Engine connection.

Usage
-----
1. Put your project ID in config.py (GEE_PROJECT).
2. Run once to authenticate (opens a browser / pastes a token):
       python auth_setup.py
3. Thereafter, every script just calls `init_ee()` from this module.
"""

import ee

from config import GEE_PROJECT


def init_ee(project: str | None = None):
    """Initialize EE, authenticating the first time if needed."""
    project = project or GEE_PROJECT
    if project == "REPLACE_WITH_YOUR_PROJECT_ID":
        raise SystemExit(
            "Set GEE_PROJECT in config.py to your Earth Engine Cloud project ID "
            "before running."
        )
    try:
        ee.Initialize(project=project)
    except Exception:
        # Not authenticated yet -> trigger the interactive auth flow.
        ee.Authenticate()
        ee.Initialize(project=project)
    return project


if __name__ == "__main__":
    proj = init_ee()
    # Smoke test: round-trip a trivial computation through the server.
    info = ee.Number(1).add(1).getInfo()
    print(f"Earth Engine initialized on project '{proj}'. Sanity check 1+1 = {info}")