如何遍历并从Google Earth Engine python API下载图像集中的每个图像 [英] How to iterate over and download each image in an image collection from the Google Earth Engine python api

查看:705
本文介绍了如何遍历并从Google Earth Engine python API下载图像集中的每个图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Google Earth Engine的新手,并试图了解如何使用Google Earth Engine python API.我可以创建一个图像集合,但是显然getdownloadurl()方法仅对单个图像起作用.因此,我试图了解如何迭代并下载集合中的所有图像.

I am new to google earth engine and was trying to understand how to use the Google Earth Engine python api. I can create an image collection, but apparently the getdownloadurl() method operates only on individual images. So I am trying to understand how to iterate over and download all of the images in the collection.

这是我的基本代码.我为正在做的其他一些工作详细介绍了它.

Here is my basic code. I broke it out in great detail for some other work I am doing.

import ee
ee.Initialize()
col = ee.ImageCollection('LANDSAT/LC08/C01/T1')
col.filterDate('1/1/2015', '4/30/2015')
pt = ee.Geometry.Point([-2.40986111110000012, 26.76033333330000019])
buff = pt.buffer(300)
region = ee.Feature.bounds(buff)
col.filterBounds(region)

因此,我提取了Landsat集合,并按日期和缓冲区几何形状进行了过滤.因此,我应该在集合中(所有乐队)拥有类似7-8张图像的图像.

So I pulled the Landsat collection, filtered by date and a buffer geometry. So I should have something like 7-8 images in the collection (with all bands).

但是,我似乎无法遍历整个集合.

However, I could not seem to get iteration to work over the collection.

例如:

for i in col:
    print(i)

错误指示TypeError: 'ImageCollection' object is not iterable

因此,如果集合不可迭代,我如何访问单个图像?

So if the collection is not iterable, how can I access the individual images?

一旦我有了图像,我就应该能够使用通常的

Once I have an image, I should be able to use the usual

path = col[i].getDownloadUrl({
    'scale': 30,
    'crs': 'EPSG:4326',
    'region': region
})

推荐答案

使用参考).因此,可以使用for循环,因为Export是客户端函数.这是一个入门的简单示例:

It's a good idea to use ee.batch.Export for this. Also, it's good practice to avoid mixing client and server functions (reference). For that reason, a for-loop can be used, since Export is a client function. Here's a simple example to get you started:

import ee
ee.Initialize()

rectangle = ee.Geometry.Rectangle([-1, -1, 1, 1])
sillyCollection = ee.ImageCollection([ee.Image(1), ee.Image(2), ee.Image(3)])

# This is OK for small collections
collectionList = sillyCollection.toList(sillyCollection.size())
collectionSize = collectionList.size().getInfo()
for i in xrange(collectionSize):
    ee.batch.Export.image.toDrive(
        image = ee.Image(collectionList.get(i)).clip(rectangle), 
        fileNamePrefix = 'foo' + str(i + 1), 
        dimensions = '128x128').start()

请注意,以这种方式将集合转换为列表对于大型集合也很危险(参考).但是,如果您确实需要下载,这可能是最具扩展性的方法.

Note that converting a collection to a list in this manner is also dangerous for large collections (reference). However, this is probably the most scalable method if you really need to download.

这篇关于如何遍历并从Google Earth Engine python API下载图像集中的每个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆