gee批量上传tif
时间: 2025-01-02 11:27:04 浏览: 3
### 使用Google Earth Engine (GEE) 进行批量上传GeoTIFF文件
为了实现批量上传GeoTIFF文件至Google Earth Engine (GEE),通常的做法不是直接通过GEE平台完成,而是借助Python API或者其他编程接口间接处理。具体来说,可以先将GeoTIFF文件上传到Google Cloud Storage (GCS),再利用`ee.data.startIngestion()`函数配合资产管理和元数据设置,在GEE中创建对应的影像集合。
下面是一个简单的例子展示如何使用Python脚本批量上传多个GeoTIFF文件:
#### 准备工作
确保已经安装并配置好`earthengine-api`库,并且设置了有效的云项目[^4]。
```bash
pip install earthengine-api google-cloud-storage
```
#### 编写Python脚本来执行批量上传任务
```python
import ee
from google.cloud import storage
def upload_geotiff_to_gcs(bucket_name, source_file_names, destination_blob_names):
"""Upload multiple local GeoTIFF files to a specified GCS bucket."""
client = storage.Client()
bucket = client.bucket(bucket_name)
for src, dest in zip(source_file_names, destination_blob_names):
blob = bucket.blob(dest)
blob.upload_from_filename(src)
print(f"File {src} uploaded to {dest}.")
def create_asset(asset_id, gcs_url, properties=None):
"""Create or update an asset from the given GCS URL with optional metadata"""
task_config = {
'id': asset_id,
'properties': properties if properties else {},
'sources': [{
'uris': [gcs_url],
}]
}
task = ee.data.newTaskId()[0]
ee.data.startIngestion(task, task_config)
print(f'Started ingestion of "{asset_id}" from {gcs_url}')
if __name__ == "__main__":
# Initialize EE and set project ID.
ee.Initialize()
BUCKET_NAME = "your-gcs-bucket-name"
ASSET_PARENT_FOLDER = "projects/your-project-id/assets/path/to/folder"
geo_tiffs_local_paths = ["path/to/local/file1.tif", "path/to/local/file2.tif"]
gcs_destinations = ["file1.tif", "file2.tif"]
# Step 1: Upload all GeoTIFFs to GCS first.
upload_geotiff_to_gcs(BUCKET_NAME, geo_tiffs_local_paths, gcs_destinations)
base_gcs_uri = f'gs://{BUCKET_NAME}/'
for i, file_name in enumerate(gcs_destinations):
full_gcs_path = base_gcs_uri + file_name
new_asset_id = f"{ASSET_PARENT_FOLDER}/{i}"
props = {"description": "Batch Uploaded Image"}
# Step 2: Create assets within GEE based on each GCS path.
create_asset(new_asset_id, full_gcs_path, props)
```
此代码片段展示了怎样把本地存储的一系列GeoTIFF文件上载到指定的GCS桶内,随后调用`create_asset`方法基于这些位于云端的对象构建新的GEE图像资源。注意这里的路径替换为实际使用的值。
阅读全文