Skip to content

Instantly share code, notes, and snippets.

@jdbcode
Created October 11, 2024 22:10
Show Gist options
  • Save jdbcode/29b22546c97b7d395bceaab2180b0921 to your computer and use it in GitHub Desktop.
Save jdbcode/29b22546c97b7d395bceaab2180b0921 to your computer and use it in GitHub Desktop.
ee_export_assets_to_gcs.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyMmmnx/Z/FAaB+ohovdAWd7",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/jdbcode/29b22546c97b7d395bceaab2180b0921/ee_export_assets_to_gcs.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"id": "CI99N3bWbG7E"
},
"outputs": [],
"source": [
"import ee\n",
"\n",
"ee.Authenticate()\n",
"ee.Initialize(project='your-project-id')"
]
},
{
"cell_type": "code",
"source": [
"assets_path = 'projects/your-project-id/assets/'\n",
"bucket_name = 'your-bucket-name'"
],
"metadata": {
"id": "x8ZHGTq_cf-S"
},
"execution_count": 16,
"outputs": []
},
{
"cell_type": "code",
"source": [
"assets = ee.data.listAssets({'parent': assets_path})\n",
"\n",
"assets = assets['assets']\n",
"\n",
"tables = []\n",
"images = []\n",
"for asset in assets:\n",
" if asset['type'] == 'TABLE':\n",
" tables.append(asset)\n",
" elif asset['type'] == 'IMAGE':\n",
" images.append(asset)\n",
"\n",
"print('tables', tables)\n",
"print('images', images)"
],
"metadata": {
"id": "o2sc_5qTc83D"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"for table in tables:\n",
" task = ee.batch.Export.table.toCloudStorage(\n",
" collection=ee.FeatureCollection(table['id']),\n",
" description=table['id'].split('/')[-1], # Use asset name as description\n",
" bucket=bucket_name,\n",
" fileNamePrefix=table['id'].split('/')[-1], # Use asset name as prefix\n",
" fileFormat='SHP',\n",
" )\n",
"\n",
" task.start()\n",
" print(f\"Exporting {table['id']} to gs://{bucket_name}/{table['id'].split('/')[-1]}.shp\")\n"
],
"metadata": {
"id": "H1nLYLxWmlOL"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"for image in images:\n",
" # Get projection information\n",
" projection = ee.Image(image['id']).projection()\n",
" crs = projection.crs()\n",
" crsTransform = projection.getInfo()['transform'] # Get transform from getInfo()\n",
"\n",
" task = ee.batch.Export.image.toCloudStorage(\n",
" image=ee.Image(image['id']),\n",
" description=image['id'].split('/')[-1],\n",
" bucket=bucket_name,\n",
" fileNamePrefix=image['id'].split('/')[-1],\n",
" region=ee.Image(image['id']).geometry(),\n",
" crs=crs,\n",
" crsTransform=crsTransform,\n",
" maxPixels=1e13,\n",
" fileFormat='GeoTIFF',\n",
" )\n",
"\n",
" task.start()\n",
" print(f\"Exporting {image['id']} to gs://{bucket_name}/{image['id'].split('/')[-1]}.tif\")"
],
"metadata": {
"id": "yg38DgrUmn9b"
},
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment