How to iterate over and download each image in an image collection from the Google Earth Engine python api

前端 未结 3 619
别跟我提以往
别跟我提以往 2021-01-06 23:08

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 getdow

相关标签:
3条回答
  • 2021-01-06 23:40

    Here is my solution:

    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()
    

    A reproducible example can you found it here: https://colab.research.google.com/drive/1Nv8-l20l82nIQ946WR1iOkr-4b_QhISu

    0 讨论(0)
  • 2021-01-06 23:51

    You could possibly use ee.ImageCollection.iterate() with a function that gets the image and adds it to a list.

    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))
    

    Unfortunately I am not able to test this code as I do not have access to the API, but it might help you arrive at a solution.

    0 讨论(0)
  • 2021-01-07 00:01

    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.

    0 讨论(0)
提交回复
热议问题