From d91dd1cdadbacb0c37501e4340c46bef168d1d00 Mon Sep 17 00:00:00 2001 From: Justin Terry Date: Thu, 13 Jun 2024 15:26:27 -0700 Subject: [PATCH] Create folder for asset in GEE, and overwrite old assets --- batch/python/export_to_gee.py | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/batch/python/export_to_gee.py b/batch/python/export_to_gee.py index 0b7e775c4..f2e016014 100644 --- a/batch/python/export_to_gee.py +++ b/batch/python/export_to_gee.py @@ -39,8 +39,26 @@ def upload_cog_to_gcs(dataset, implementation): def create_cog_backed_asset(dataset, implementation, gcs_path, service_account): credentials = ee.ServiceAccountCredentials(service_account, GCS_CREDENTIALS_FILE) - session = AuthorizedSession(credentials.with_quota_project(EE_PROJECT)) + ee.Initialize(credentials) + + # delete any existing asset with the same dataset/implementatio + try: + ee.data.deleteAsset(f"projects/{EE_PROJECT}/assets/{dataset}/{implementation}") + except ee.EEException: + # asset doesn't exist + pass + + # create dataset folder if it doesn't exist + try: + ee.data.createAsset( + {"type": "Folder"}, f"projects/{EE_PROJECT}/assets/{dataset}" + ) + except ee.EEException: + # folder already exists + pass + # link GCS COG to the GEE asset + session = AuthorizedSession(credentials.with_quota_project(EE_PROJECT)) request = {"type": "IMAGE", "gcs_location": {"uris": [gcs_path]}} asset_id = f"{dataset}/{implementation}" @@ -62,7 +80,7 @@ def export_to_gee( ): service_account = set_google_application_credentials() gcs_path = upload_cog_to_gcs(dataset, implementation) - create_cog_backed_asset(gcs_path, dataset, implementation, service_account) + create_cog_backed_asset(dataset, implementation, gcs_path, service_account) if __name__ == "__main__":