def DownloadFromDrive(file_name, formato="tif", path=os.getcwd(), force=False): """ Downloads an image from google drive and deletes it at the end :param file_name: file to download :param formato: formato de el asset a descargar :param path: path to download the image :param force: if we want to force the donwload (overwrites if exist) :return: the full path to the donwloaded image """ image_name_full_original = os.path.join(path, addFormat(file_name, formato)) if os.path.isfile(image_name_full_original) and not force: return image_name_full_original # Download file from drive drive = AuthDrive() file_list = drive.ListFile({ 'q': "title contains '{}' and trashed=false".format(file_name) }).GetList() #expr = re.compile("%s-(\d+)-(\d+)\.%s" % (file_name, formato)) expr = re.compile("(%s)(-\d+-\d+)?\.%s" % (file_name, formato)) f_downs = [] for file_down in file_list: title = file_down.attr["metadata"]["title"] fmo = expr.fullmatch(title) if fmo is not None: image_name_full = os.path.join(path, addFormat(title, formato)) logger.info("Downloading image %s from drive" % title) file_down.GetContentFile(image_name_full) f_downs.append(image_name_full) # Delete image from drive filele = drive.CreateFile() filele.auth.service.files().delete( fileId=file_down['id']).execute() if len(f_downs) == 1: return f_downs[0] if len(f_downs) == 4: import tifffile f_downs = sorted(f_downs) up = np.concatenate([tifffile.imread(f) for f in f_downs[:2]], axis=1) down = np.concatenate([tifffile.imread(f) for f in f_downs[2:]], axis=1) tifffile.imsave(image_name_full_original, np.concatenate((up, down), axis=0)) for fd in f_downs: os.remove(fd) return image_name_full_original raise IOError("files {} dont know how to concat them".format(f_downs))
def MaybeDownloadWithTask(image, image_name, path=os.getcwd(), force=False): """ Download the image to google drive and it download it afterwards to path Note: image is downloaded as a geotif file. :param image: server image object to download :type image: ee.Image :param image_name: image file name if exists and not force it doesnt download anything :param path: path to download the image :param force: if we want to force the donwload (overwrites if exist) :return: the full path to the downloaded image """ image_name_full = os.path.join(path, addFormat(image_name, "tif")) if os.path.isfile(image_name_full) and not force: return image_name_full task = ee.batch.Export.image.toDrive(image, description=image_name, folder="ee_ipl_uv_downloads") task.start() return WaitAndDownload(task, image_name, "tif", path, True)
def eeFeatureCollectionToPandas(feature_col, properties=None, with_task=False, filename=None, mounted_drive=False): """ Converts ee.FeatureCollection server obj to pandas.DataFrame local obj. :param feature_col: feature collection to export :type feature_col: ee.FeatureCollection :param properties: (optional) list of columns to export :param with_task: (default false). If download is done throw ee.batch.Export.table :param filename: (optional) if None csv downloaded will be removed. :param mounted_drive: if drive is mounted we don't need to use pyDrive If present nothing will be downloaded :return: pandas.DataFrame object """ # Features is a list of dict with the output remove_file = not mounted_drive filetype = "csv" if filename is None: filename = file_utils.createTempFile(params={"format": filetype}, prefix="pandas_ftcol") else: filename_full = file_utils.addFormat(filename, filetype) if os.path.isfile(filename_full): return _readCSV(filename_full) remove_file = False prefix = file_utils.removeFormat(os.path.basename(filename), "csv") if with_task: from ee_ipl_uv.download import WaitAndDownload, WaitTask if properties is not None: feature_col = feature_col.select(properties) tarea = ee.batch.Export.table.toDrive(feature_col, prefix, folder="ee_ipl_uv_downloads", fileFormat=filetype) tarea.start() if mounted_drive: WaitTask(tarea) filename = os.path.join("/content/drive/My Drive/ee_ipl_uv_downloads/", prefix+".csv") if not os.path.exists(filename): logger.info("File %s not ready in drive. Waiting 30 seconds" % filename) time.sleep(30) assert os.path.exists(filename), "%s does not exists in the drive" % filename else: filename = WaitAndDownload(tarea, prefix, formato="csv", force=True) else: if properties is None: url = feature_col.getDownloadURL(filetype=filetype) else: properties_list = properties.getInfo() url = feature_col.getDownloadURL(filetype=filetype, selectors=properties_list) logger.debug("Downloading data from: " + url) r_link = requests.get(url, stream=True) if r_link.status_code == 200: with open(filename, 'wb') as f: r_link.raw.decode_content = True shutil.copyfileobj(r_link.raw, f) logger.debug("File downloaded, reading csv: " +filename) datos = _readCSV(filename) if remove_file: os.remove(filename) return datos