<jupyter_start><jupyter_text>Downloading data from Sentinel-hub<jupyter_code>import numpy as np
import pandas as pd

import matplotlib.pyplot as plt

import pprint
import math

from sentinelhub import AwsTile
from sentinelhub import AwsTileRequest

from sentinelhub import WebFeatureService, BBox, CRS, DataCollection, SHConfig

from shapely.geometry import shape, Polygon, MultiPolygon, MultiLineString<jupyter_output><empty_output><jupyter_text>Configuring Credentials<jupyter_code>config = SHConfig()

# Set your own ids and secrets
# https://sentinelhub-py.readthedocs.io/en/latest/configure.html#


config.instance_id = ""
config.sh_client_id = ""
config.sh_client_secret = ""
config.aws_access_key_id = ""
config.aws_secret_access_key = ""
config.save()

if config.instance_id == '':
    print("Warning! To use WFS functionality, please configure the `instance_id`.")
sokoto_bbox = [(4.364, 12.319), (6.125, 13.798)]

search_bbox = BBox(bbox=sokoto_bbox, crs=CRS.WGS84)
search_time_interval = (
    '2021-06-14',
    '2021-06-14'
)

wfs_iterator = WebFeatureService(
    search_bbox,
    search_time_interval,
    data_collection=DataCollection.SENTINEL2_L2A,
    maxcc=0.05,
    config=config
)

tiles_details = list(wfs_iterator)

print('Total number of tiles:', len(tiles_details))
print("\nFirst result:")
tiles_details[0]
def show_polygon(polygon):
    plt.plot(*polygon.exterior.xy)
    plt.grid()
    
def plot_tiles(tiles, bbox):
    cols = 5
    rows = math.ceil(len(tiles)/cols)

    fig, axs = plt.subplots(nrows=rows, ncols=cols, figsize=(15, 15), sharex=True, sharey=True)

    for i, (ax, tile) in enumerate(zip(axs.flat, tiles)):
        coords = tile["geometry"]["coordinates"][0][0]
        polygon = Polygon(coords)
        
        ax.plot(*bbox.geometry.exterior.xy)
        ax.plot(*polygon.exterior.xy)
        ax.set(aspect='equal')
        ax.set_title(f'Tile {i}')
        ax.grid()

    fig.tight_layout()
    fig.show()
show_polygon(search_bbox.geometry)
    
for data in tiles_details:
    coords = data["geometry"]["coordinates"][0][0]
    show_polygon(Polygon(coords))
plot_tiles(tiles_details, search_bbox)
bands = ['R10m/TCI'] # True Color Index
metafiles = ['tileInfo']
data_folder = './data/sentinel2'
selected_tiles_number = [0, 1, 2, 3, 4, 5, 6, 7, 8]
selected_tiles = []
tiles_id = wfs_iterator.get_tiles()

for tile_number in selected_tiles_number:
    selected_tiles.append(tiles_id[tile_number])
    
print(f'# selected tiles: {len(selected_tiles)}')
for tile in selected_tiles:
    print(tile)
    tile_name, time, aws_index = tile

    request = AwsTileRequest(
        tile=tile_name,
        time=time,
        aws_index=aws_index,
        bands=bands,
        metafiles=metafiles,
        data_folder=data_folder,
        data_collection=DataCollection.SENTINEL2_L2A
    )

    request.save_data()  # This is where the download is triggered<jupyter_output>('31PFP', '2021-6-14', 0)
{'fraction_non_alphanumeric': 0.12278308321964529, 'fraction_numerical': 0.02557980900409277, 'mean_word_length': 4.246869409660107, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '308446', 'license': 'MIT', 'n_tokens_mistral': 1122, 'n_tokens_neox': 1066, 'n_words': 203, 'path': 'jupyter-notebooks/1 - sentinel2 download.ipynb', 'repo_name': 'matt989253/heig-TerraGazer', 'size': '2932'}