(image, polygon, bandsId, filepath)
Downloads a .TIF image from the ee server. The image is downloaded as a
    zip file then moved to the working directory, unzipped and stacked into a
    single .TIF file.

    Two different codes based on which version of the earth-engine-api is being
    used.

    KV WRL 2018

    Arguments:
    -----------
    image: ee.Image
        Image object to be downloaded
    polygon: list
        polygon containing the lon/lat coordinates to be extracted
        longitudes in the first column and latitudes in the second column
    bandsId: list of dict
        list of bands to be downloaded
    filepath: location where the temporary file should be saved

    Returns:
    -----------
    Downloads an image in a file named data.tif
Downloads a .TIF image from the ee server. The image is downloaded as a
    zip file then moved to the working directory, unzipped and stacked into a
    single .TIF file.
Downloads a . TIF image from the ee server . The image is downloaded as a zip file then moved to the working directory unzipped and stacked into a single . TIF file .
def download_tif(image, polygon, bandsId, filepath):
    """
    Downloads a .TIF image from the ee server. The image is downloaded as a
    zip file then moved to the working directory, unzipped and stacked into a
    single .TIF file.

    Two different codes based on which version of the earth-engine-api is being
    used.

    KV WRL 2018

    Arguments:
    -----------
    image: ee.Image
        Image object to be downloaded
    polygon: list
        polygon containing the lon/lat coordinates to be extracted
        longitudes in the first column and latitudes in the second column
    bandsId: list of dict
        list of bands to be downloaded
    filepath: location where the temporary file should be saved

    Returns:
    -----------
    Downloads an image in a file named data.tif

    """

    # for the old version of ee only
    if int(ee.__version__[-3:]) <= 201:
        url = ee.data.makeDownloadUrl(ee.data.getDownloadId({
            'image': image.serialize(),
            'region': polygon,
            'bands': bandsId,
            'filePerBand': 'false',
            'name': 'data',
            }))
        local_zip, headers = urlretrieve(url)
        with zipfile.ZipFile(local_zip) as local_zipfile:
            return local_zipfile.extract('data.tif', filepath)
    # for the newer versions of ee
    else:
        # crop image on the server and create url to download
        url = ee.data.makeDownloadUrl(ee.data.getDownloadId({
            'image': image,
            'region': polygon,
            'bands': bandsId,
            'filePerBand': 'false',
            'name': 'data',
            }))
        # download zipfile with the cropped bands
        local_zip, headers = urlretrieve(url)
        # move zipfile from temp folder to data folder
        dest_file = os.path.join(filepath, 'imagezip')
        shutil.move(local_zip,dest_file)
        # unzip file
        with zipfile.ZipFile(dest_file) as local_zipfile:
            for fn in local_zipfile.namelist():
                local_zipfile.extract(fn, filepath)
            # filepath + filename to single bands
            fn_tifs = [os.path.join(filepath,_) for _ in local_zipfile.namelist()]
        # stack bands into single .tif
        outds = gdal.BuildVRT(os.path.join(filepath,'stacked.vrt'), fn_tifs, separate=True)
        outds = gdal.Translate(os.path.join(filepath,'data.tif'), outds)
        # delete single-band files
        for fn in fn_tifs: os.remove(fn)
        # delete .vrt file
        os.remove(os.path.join(filepath,'stacked.vrt'))
        # delete zipfile
        os.remove(dest_file)
        # delete data.tif.aux (not sure why this is created)
        if os.path.exists(os.path.join(filepath,'data.tif.aux')):
            os.remove(os.path.join(filepath,'data.tif.aux'))
        # return filepath to stacked file called data.tif
        return os.path.join(filepath,'data.tif')
def download_tif ( image , polygon , bandsId , filepath ) : # for the old version of ee only if int ( ee . __version__ [ - 3 : ] ) <= 201 : url = ee . data . makeDownloadUrl ( ee . data . getDownloadId ( { 'image' : image . serialize ( ) , 'region' : polygon , 'bands' : bandsId , 'filePerBand' : 'false' , 'name' : 'data' , } ) ) local_zip , headers = urlretrieve ( url ) with zipfile . ZipFile ( local_zip ) as local_zipfile : return local_zipfile . extract ( 'data.tif' , filepath ) # for the newer versions of ee else : # crop image on the server and create url to download url = ee . data . makeDownloadUrl ( ee . data . getDownloadId ( { 'image' : image , 'region' : polygon , 'bands' : bandsId , 'filePerBand' : 'false' , 'name' : 'data' , } ) ) # download zipfile with the cropped bands local_zip , headers = urlretrieve ( url ) # move zipfile from temp folder to data folder dest_file = os . path . join ( filepath , 'imagezip' ) shutil . move ( local_zip , dest_file ) # unzip file with zipfile . ZipFile ( dest_file ) as local_zipfile : for fn in local_zipfile . namelist ( ) : local_zipfile . extract ( fn , filepath ) # filepath + filename to single bands fn_tifs = [ os . path . join ( filepath , _ ) for _ in local_zipfile . namelist ( ) ] # stack bands into single .tif outds = gdal . BuildVRT ( os . path . join ( filepath , 'stacked.vrt' ) , fn_tifs , separate = True ) outds = gdal . Translate ( os . path . join ( filepath , 'data.tif' ) , outds ) # delete single-band files for fn in fn_tifs : os . remove ( fn ) # delete .vrt file os . remove ( os . path . join ( filepath , 'stacked.vrt' ) ) # delete zipfile os . remove ( dest_file ) # delete data.tif.aux (not sure why this is created) if os . path . exists ( os . path . join ( filepath , 'data.tif.aux' ) ) : os . remove ( os . path . join ( filepath , 'data.tif.aux' ) ) # return filepath to stacked file called data.tif return os . path . join ( filepath , 'data.tif' )