sentinelhub Python API

repository·master·Indexed 21 days ago

https://github.com/sentinel-hub/sentinelhub-py

The official Python interface for Sentinel Hub services, providing access to satellite data collections including Sentinel, Landsat, and MODIS. It includes support for Process, Catalog, Batch Processing, BYOC, and Statistical APIs, as well as OGC services (WMS, WCS, WFS) and Geopedia. The library features geospatial utilities for large area splitting and data collection, and serves as a core dependency for the eo-learn package.

Tokens
29.7K
Snippets
99
Records
117
Agent score
75%

What's inside sentinelhub-py

  1. Overview of sentinelhub functionalities

    master

    The sentinelhub package provides an interface to Sentinel Hub services and geospatial utilities:

    Sentinel Hub Services

    • Process API: For requesting processed imagery.
    • Catalog API: For searching data collections.
    • Batch Processing API: For large-scale asynchronous processing.
    • BYOC API: Bring Your Own Cloud.
    • Statistical API: For extracting statistics from imagery.
    • OGC services: Support for WMS, WCS, and WFS.
    • Authentication & Rate-limiting: Built-in handling for service access.

    Geospatial Utilities

    • Interface for geospatial objects and transformations.
    • Large area splitting.
    • Data collection objects.
    • IO tools.

    Geopedia

    • Support for Geopedia WMS and REST API.
  2. Understand configuration precedence

    master

    When multiple configuration sources are present, sentinelhub-py follows this precedence order (from highest to lowest):

    1. Explicit parameters: Values passed directly to SHConfig(sh_client_id="...") or function calls.
    2. Environment variables: Values set via SH_CLIENT_ID, SH_CLIENT_SECRET, or SH_PROFILE.
    3. Configuration file: Values defined in config.toml.
    4. Defaults: The hardcoded default values in the library.
  3. Use a configuration file (config.toml)

    master

    To avoid hardcoding credentials, you can use a config.toml file. When a new SHConfig object is created, it automatically updates its default values with the contents of this file.

    File Location

    • Linux/macOS: ~/.config/sentinelhub/config.toml
    • Windows: C:/Users/<USERNAME>/.config/sentinelhub/config.toml
    • Use SHConfig.get_config_location() to find the exact path on your system.

    Profiles

    The configuration file supports multiple profiles. You can load a specific profile by passing its name to the constructor: SHConfig("myprofile"). If no profile is specified, the default profile (defined by sentinelhub.config.DEFAULT_PROFILE, currently "default-profile") is used.

    TOML Structure

    Sections are defined by the profile name in square brackets:

    [default-profile]
    instance_id = "my-instance-id"
    max_download_attempts = 3
    
    [custom-profile]
    instance_id = "something-else"
  4. Install sentinelhub via pip

    master

    You can install the core sentinelhub package using pip. If you need to interact with Amazon Web Services, install the package with the [AWS] extension tag to include the necessary extra dependencies.

    # Install core package
    pip install sentinelhub
    
    # Install with AWS support
    pip install sentinelhub[AWS]
  5. Track logs by thread name

    master

    Because downloading is multi-threaded using the threading library, you can distinguish which log belongs to which thread by adding %(threadName)s to your logging format string. The default format is %(levelname)s:%(name)s:%(message)s.

    import logging
    
    # The default format is '%(levelname)s:%(name)s:%(message)s'
    
    logging.basicConfig(
        level=logging.DEBUG,
        format='%(levelname)s:%(name)s:%(threadName)s:%(message)s',
    )
  6. Debug HTTP requests and urllib3 logs

    master

    Since sentinelhub uses the requests package for HTTP communication, you can obtain detailed information about HTTP requests by enabling low-level urllib3 logs and setting the HTTPConnection.debuglevel.

    import logging
    from http.client import HTTPConnection
    
    HTTPConnection.debuglevel = 1
    
    logging.basicConfig(level=logging.DEBUG)
    requests_log = logging.getLogger("requests.packages.urllib3")
    requests_log.setLevel(logging.DEBUG)
    requests_log.propagate = True
  7. Configure for Copernicus Data Space Ecosystem (CDSE)

    master

    To use the Copernicus Data Space Ecosystem, you must register an OAuth client via the Sentinel Hub Services Dashboard and configure SHConfig with the following specific parameters:

    from sentinelhub import SHConfig
    
    config = SHConfig()
    config.sh_client_id = 'oauth-client-id'
    config.sh_client_secret = 'oauth-client-secret'
    config.sh_base_url = 'https://sh.dataspace.copernicus.eu'
    config.sh_token_url = 'https://identity.dataspace.copernicus.eu/auth/realms/CDSE/protocol/openid-connect/token'