Sentinel2tools: simple lib for downloading Sentinel-2 satellite images

    April 19, 2022

    In recent years satellite images have been used in a wide range of applications such as monitoring agriculture fields, types of crops with historical analytics, deforestation problems, field boundaries detection, etc. The availability of acquiring quality and high-resolution images periodically helps to improve the performance of models and build accurate solutions.

    Landsat and Sentinel are most commonly used for getting satellite images. They are open-sourced missions that produce multispectral images. However, Sentinel-2 mission images, provided by the European Commission and European Space Agency, carry a super-spectral imager with 13 bands covering the Visible and Near Infrared (VNIR) and ShortWave InfraRed (SWIR) wavelength region. The spatial resolution of these bands is 10-60 m and has a coverage between −56 and +84 degrees latitude with a 290 km swath width. The minimum revisits time at the equator is 10 days for Sentinel-2A.

    Sentinel-2 mission provides Level-1C (L1C, since 2015) and Level-2A (L2A, since 2017) data product types. L1C measurement is the top of the atmosphere reflectance, and L2A – is the bottom of the atmosphere reflectance. The main difference between these product types is that L2A has atmospheric correction (the portion of light reflected off the image’s atmosphere is removed). That’s why the L2A product type is preferable for analysis.

    The most common building solutions issues are downloading and preliminary processing of raw satellite images.

    Standard approaches to loading Sentinel-2 images

    One of the basic approaches to getting Sentinel-2 data is using the Sentinel hub. Their Python package supports obtaining data from Amazon Web Service. It delivers Sentinel-2 L1C imagery and asks the requester to pay for Sentinel-2 L2A images.

    Sentinel-2 open-source dataset on Google Cloud is free for loading L2A and L1C satellite products, but this requires knowledge about the dataset structure. Instead, you must select quality images (e.g., small cloud coverage areas) by analyzing metadata files, finding suitable dates, and downloading every image manually. This option is time-consuming and annoying. Also, it is hard to check that selected images cover the entire user’s geographical area of interest (AOI).

    To automate the downloading pipeline and ensure free access to the Sentinel-2 images, we would like to introduce sentinel2tools. It is an open-source project — a convenient and straightforward wrapper on the Sentinel-2 datasets on the Google Cloud platform.

    It could help developers and data scientists with a bunch of tasks. They will avoid code duplication, reduce time on manual filtering, and search for relevant satellite images for given AOI while allowing concurrent downloading conversion of L1C product type to L2A.

    How to use sentinel2tools — an easy open-source wrapper on the Sentinel-2 datasets

    Installation

    To install sentinel2tools, use the pip package as follows:

    pip install 
    git+https://github.com/QuantuMobileSoftware/sentinel2tools.git@<commit-ref>

    where <commit-ref> – latest commit reference on the master branch. Current:

    ca232cb66106db6cac729defdab91aad9aecb15b.

    Usage

    Sentinel-2 products are a compilation of fixed-sized elementary granules and a single orbit. A granule is the minimum indivisible partition (containing all possible spectral bands). For Level-1C and Level-2A, the granules, also called tiles, are 100×100 km2 ortho-images in UTM/WGS84 projection.

    • Sentinel2Overlap

    Sentinel2Overlap is a class in the sentinel2tools library that finds tiles intersecting with a given AoI in GeoJSON format. It encodes many geographic data structures. In the current implementation, the lib supports the .geojson file with polygons. geojson.io kindly helps to prepare the specified format.

    from sentinel2download.overlap import Sentinel2Overlap
    
    aoi_path = "./test_geojson/Kharkiv.geojson"
    
    overlap = Sentinel2Overlap(aoi_path)
    tiles = overlap.overlap()
    print(f"Overlapped tiles: {tiles}")

    where aoi_path – path to .geojson file with input AOI (for instance, Kharkiv region, Ukraine), see Figure 1.

    Overlapped tiles: ['36UXA', '36UXU', '36UXV', '36UYA', '36UYB', '36UYU', '36UYV', '37UCQ', '37UCR', '37UDQ', '37UDR']
    Sentinel-2-overlapped-tiles-for-given-AOI-built-by-QGIS-Desktop-tool
    Figure1. Sentinel-2 overlapped tiles for given AOI built by QGIS Desktop tool (CRS: WGS 84 / Pseudo-Mercator)

     

    • Sentinel2Downloader

    Sentinel2Downloader is the second class in the library that downloads satellite images and metadata files (see Figure 2). It takes a list of specified tiles and additional parameters for image filtering as input. The user has to create a Google API key to download satellite images. After creating the key.json file, pass it to the constructor of Sentinel2Downloader. These options could be specified in the download() method:
    — product type (‘L2A’ or ‘L1C’),
    — start date,
    — end date,
    — a path to the output directory where images will be stored,
    — number of cores (for concurrent downloading),
    — sentinel-2 bands (for instance, TCI – True Color Image),
    — constraints (for filtering the best images). Note that the NODATA_PIXEL_PERCENTAGE constraint is not applied for the ‘L1C’ product type. More available constraints are presented here.

    from sentinel2download.downloader import SentinelDownloader
    
    CONSTRAINTS = {'NODATA_PIXEL_PERCENTAGE': 15.0, 
                   'CLOUDY_PIXEL_PERCENTAGE': 10.0, }
    
    loader = Sentinel2Downloader(api_key="google_api_key.json")
    
    loaded = loader.download(product_type="L2A",
                            tiles=["36UYA", ],
                            start_date="2020-09-01",
                            end_date="2020-09-20",
                            output_dir="./sentinel2imagery",
                            bands={"TCI", "B04", },
                            cores=2,
                            constraints=CONSTRAINTS)
    Sentinel-2-L2A-downloaded-images-TCI-and-B04-spectral-bands
    Figure2. Sentinel-2 L2A downloaded images: TCI and B04 spectral bands. (CRS: WGS 84 / Pseudo-Mercator)
    • Sentinel2Converter

    As mentioned above, the atmospheric correction is applied for L2A images. However, you may need images from 2015 or quality L2A images that weren’t found for AOI. In that case, Sentinel2Converter class is used for converting L1C to L2A (see Figures 3-4) by the Sen2Cor processor.

    Sen2Cor tool is added in the sentinel2tools lib. You can install it to your system from the scripts folder.

    from sentinel2preprocessing.conversion import Sentinel2Converter
    
    converter = Sentinel2Converter()
    converted_products = converter.convert(download_dir, conversion_dir)

    where download_dir – path to the directory with loaded items, and conversion_dir – path to the directory with the stored, converted items. Note that you have to provide flag full_download=True in loader for correct conversion.download() method.

    Converting-Sentinel-2-L1C-image-to-L2A

     

    Sentinel2tools in a nutshell

    Sentinel2tools library delivers a set of instruments for downloading, filtering Sentinel-2 images, and conversion between L1C and L2A products. The project is open-sourced and allows getting images concurrently and for free. The library usage is quite simple — only several lines of code are needed to start. The library has independent modules (Seninel2Overlap, Sentinel2Downloader, Sentinel2Converter), and you can feel free to use the necessary ones. We are ready for future functionality development, improvements, and collaboration with developers — just drop us a line.

    • #Earth observing
    • #Geoanalytics
    • #Geospatial data
    • #Satellite data
    • #Sentinel-2

    Share Article

    Case studies

    CONNECT WITH OUR EXPERTS