如何迭代并从Google Earth引擎python api下载图像集合中的每个图像
我是google earth引擎的新手,我试图了解如何使用google earth引擎python api。我可以创建一个图像集合,但显然如何迭代并从Google Earth引擎python api下载图像集合中的每个图像,python,python-3.x,google-earth-engine,landsat,Python,Python 3.x,Google Earth Engine,Landsat,我是google earth引擎的新手,我试图了解如何使用google earth引擎python api。我可以创建一个图像集合,但显然getdownloadurl()方法只对单个图像进行操作。因此,我试图了解如何迭代并下载集合中的所有图像 这是我的基本代码。我把它讲得非常详细,是为了我正在做的其他工作 import ee ee.Initialize() col = ee.ImageCollection('LANDSAT/LC08/C01/T1') col.filterDate('1/1/20
getdownloadurl()
方法只对单个图像进行操作。因此,我试图了解如何迭代并下载集合中的所有图像
这是我的基本代码。我把它讲得非常详细,是为了我正在做的其他工作
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)
因此,我提取了陆地卫星收集,按日期和缓冲区几何进行过滤。因此,我应该有一些像7-8图像的集合(与所有乐队)
然而,我似乎无法让迭代在集合上工作
例如:
for i in col:
print(i)
该错误表示TypeError:“ImageCollection”对象不可编辑
因此,如果集合不可编辑,我如何访问单个图像
一旦我有了一个图像,我应该能够使用通常的
path = col[i].getDownloadUrl({
'scale': 30,
'crs': 'EPSG:4326',
'region': region
})
您可以使用函数获取图像并将其添加到列表中
import ee
def accumluate_images(image, images):
images.append(image)
return images
for img in col.iterate(accumulate_images, []):
url = img.getDownloadURL(dict(scale=30, crs='EPSG:4326', region=region))
不幸的是,我无法测试这段代码,因为我没有访问API的权限,但它可能会帮助您找到解决方案。使用它是一个好主意。此外,避免混合使用客户机和服务器函数()也是一种很好的做法。因此,可以使用For循环,因为Export
是一个客户端函数。下面是一个简单的示例,让您开始学习:
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()
请注意,以这种方式将集合转换为列表对于大型集合()也是危险的。但是,如果您确实需要下载,这可能是最具可扩展性的方法。以下是我的解决方案:
import ee
ee.Initialize()
pt = ee.Geometry.Point([-2.40986111110000012, 26.76033333330000019])
region = pt.buffer(10)
col = ee.ImageCollection('LANDSAT/LC08/C01/T1')\
.filterDate('2015-01-01','2015-04-30')\
.filterBounds(region)
bands = ['B4','B5'] #Change it!
def accumulate(image,img):
name_image = image.get('system:index')
image = image.select([0],[name_image])
cumm = ee.Image(img).addBands(image)
return cumm
for band in bands:
col_band = col.map(lambda img: img.select(band)\
.set('system:time_start', img.get('system:time_start'))\
.set('system:index', img.get('system:index')))
# ImageCollection to List
col_list = col_band.toList(col_band.size())
# Define the initial value for iterate.
base = ee.Image(col_list.get(0))
base_name = base.get('system:index')
base = base.select([0], [base_name])
# Eliminate the image 'base'.
new_col = ee.ImageCollection(col_list.splice(0,1))
img_cummulative = ee.Image(new_col.iterate(accumulate,base))
task = ee.batch.Export.image.toDrive(
image = img_cummulative.clip(region),
folder = 'landsat',
fileNamePrefix = band,
scale = 30).start()
print('Export Image '+ band+ ' was submitted, please wait ...')
img_cummulative.bandNames().getInfo()
你可以在这里找到一个可复制的例子:是的,我看到一个关于
迭代
方法的提到。我要试一试。非常感谢你的建议。你应该避免使用这种方法,原因有二。第一,避免混合使用客户端(getDownloadURL()
)和服务器(iterate()
)函数()是一种很好的做法。第二,getDownloadURL()
是脆弱的,有时会导致下载损坏。@NicholasClinton:谢谢。根据您的参考,getDownloadURL()
是一个服务器函数,因为它是ee.Image()
对象上的一个方法。您是指传递给iterate()
的Python列表和对list.append()
的调用吗?@NicholasClinton:关于第二点,API及其功能是实验性的,可能会发生变化。我们是否可以假设该方法的脆弱性可能就是由于这个原因呢?getDownloadURL()
flakines的最佳解释是。注意,getDownloadURL()
while'iterate()`Good call。是的,我来看看这个方法。我以前没见过它。