我是google earth engine的新手,正在尝试了解如何使用Google Earth Engine python api。我可以创建一个图像集合,但显然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' object is not iterable
那么,如果集合是不可迭代的,我如何访问各个图像?
一旦我有了一个图像,我应该能够使用通常的
path = col[i].getDownloadUrl({
'scale': 30,
'crs': 'EPSG:4326',
'region': region
})
发布于 2017-10-27 02:05:33
在这方面使用ee.batch.Export
是个好主意。此外,避免混用客户端和服务器功能(reference)也是一种很好的做法。因此,可以使用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()
请注意,以这种方式将集合转换为列表对于大型集合(reference)也是危险的。但是,如果您确实需要下载,这可能是最具伸缩性的方法。
发布于 2018-11-10 04:48:58
以下是我的解决方案:
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()
你可以在这里找到一个可重现的例子:https://colab.research.google.com/drive/1Nv8-l20l82nIQ946WR1iOkr-4b_QhISu
发布于 2017-10-26 11:23:15
您可以将ee.ImageCollection.iterate()
与获取图像并将其添加到列表的函数一起使用。
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,但它可能会帮助您找到解决方案。
https://stackoverflow.com/questions/46943061
复制相似问题