0

I'm exporting a map to my drive using the GEE python API, I'm looking for a way to retreive all the created files automatically to a local folder.

my current code create a image on my drive :

def download_to_disk(country_code, year):
    """download the tile to the GEE disk"""
    file_name = 'alerts_' + country_code + '_' + year
    task_config = {
        'image':alerts,
        'description':file_name,
        'scale': 30,
        'region':country,
        'maxPixels': 1e10
    }
    
    task = ee.batch.Export.image.toDrive(**task_config)
    task.start()

Is there a way to retrieve the files created by this very task automatically using only the Python API ?

2
  • where does the variable service come from? I think your question is related to GDrive Python API and not GEE Jun 28, 2020 at 14:25
  • It was just a bad copy-paste from the documentation. I realized my question wasn't super clear so I edited it. I pretty sure it's basic usage I just didn't find the appropriate documentation or example. Jun 29, 2020 at 4:53

1 Answer 1

0

So after crawling a bit on the internet I found a workaround :

It's impossible to retreive a file from Gdrive with a task name or a filename are they are all identify with their unique ID.

Solution is to create a function that crawls into your files in your drive and select thoses corresponding to your task. in my case it look like :

import ee

class gdrive(object):

    def __init__(self):
        
        self.initialize = ee.Initialize()
        self.credentials = ee.Credentials()
        self.service = discovery.build(serviceName='drive', version='v3', cache_discovery=False, credentials=self.credentials)

    def get_files(self, file_name):
        """ look for the file_name patern in my Gdrive files and retreive a list of Ids"""
        
        items = self.get_items()
        files = []
        for item in items:
            if (file_name in item['name']):
                files.append({'id':item['id'], 'name': item['name']})
                
        return files

then in the same gdrive object I created a function that retreive all my selected files from my drive to my local path.


from googleapiclient.http import MediaIoBaseDownload

class gdrive(object):
    # [...] som code

    def download_files(self, files, local_path):
        service = self.service
        
        for fId in files:
            request = service.files().get_media(fileId=fId['id'])
            fh = io.BytesIO()
            downloader = MediaIoBaseDownload(fh, request)
            done = False
            while done is False:
                status, done = downloader.next_chunk()
                #print('Download %d%%.' % int(status.progress() * 100))
            
            fo = open(local_path+fId['name'], 'wb')
            fo.write(fh.getvalue())
            fo.close()

hope that it will help someone

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.