Example #1
0
def download_from_aws(scene_id: str, destination: str):
    """Download the Sentinel Scene from AWS.

    It uses the library `sentinelhub-py <https://sentinelhub-py.readthedocs.io>`_ to download
    the Sentinel-2 SAFE folder. Once downloaded, it compressed into a `zip`.

    Notes:
        Make sure to set both `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` in environment variable.

        This method does not raise Exception.

    Args:
        scene_id - Sentinel-2 Product Id (We call as scene_id)
        destination - Path to store data. We recommend to use python `tempfile.TemporaryDirectory` and then move.

    Returns:
        Path to the downloaded file when success or None when an error occurred.
    """
    try:
        config = SHConfig()
        config.aws_access_key_id = Config.AWS_ACCESS_KEY_ID
        config.aws_secret_access_key = Config.AWS_SECRET_ACCESS_KEY

        logging.info(f'Downloading {scene_id} From AWS...')

        request = AwsProductRequest(product_id=scene_id,
                                    data_folder=destination,
                                    safe_format=True,
                                    config=config)
        _ = request.get_data(save_data=True)

        file_name = '{}.SAFE'.format(scene_id)

        logging.info(f'Compressing {scene_id}.SAFE...')

        with working_directory(destination):
            shutil.make_archive(base_dir=file_name,
                                format='zip',
                                base_name=scene_id)

        return Path(destination) / file_name

    except BaseException as e:
        logging.error(f'Error downloading from AWS. {scene_id} - {str(e)}')
        return None
Example #2
0
from sentinelhub import AwsTileRequest
import sentinelhub
sentinelhub.data
bands = ['B8A', 'B10']
metafiles = ['tileInfo', 'preview', 'qi/MSK_CLOUDS_B00']
data_folder = './AwsData'

request = AwsTileRequest(tile=tile_name,
                         time=time,
                         aws_index=aws_index,
                         metafiles=metafiles,
                         data_folder=data_folder,
                         data_source=DataSource.SENTINEL2_L1C)

request.save_data()  # This is where the download is triggered
data_list = request.get_data(
)  # This will not redownload anything because data is already stored on disk

b8a, b10, tile_info, preview, cloud_mask = data_list

from sentinelhub import AwsTile

tile_id = x[0]['properties']['id']
tile_name, time, aws_index = AwsTile.tile_id_to_tile(tile_id)
tile_name, time, aws_index

import rasterio, numpy, skimage
temp_dir = '/home/shilo/Downloads'
with rasterio.open(temp_dir + '/B02.jp2') as fop:
    b2 = fop.read()
with rasterio.open(temp_dir + '/B03.jp2') as fop:
    b3 = fop.read()