File size: 2,417 Bytes
1fc9fc6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import os
import gradio as gr
import numpy as np
from io import BytesIO
from PIL import Image
from dotenv import load_dotenv

from src.utils.stac_client import download_sentinel_image

# Load environment variables
load_dotenv()
USERNAME = os.environ.get("DESTINE_USERNAME")
PASSWORD = os.environ.get("DESTINE_PASSWORD")

def fetch_sentinel_image(date_from, date_to):
    """Fetch a Sentinel image based on criteria."""
    # Validate date format    
    # Download the image
    content, metadata = download_sentinel_image(
        username=USERNAME,
        password=PASSWORD,
        start_date=date_from,
        end_date= date_to
    )
    
    # Handle error case
    if isinstance(content, str):
        return None, content
    
    # Convert to PIL Image
    try:
        img = Image.open(BytesIO(content))
        
        # Create metadata string
        metadata_str = "\n".join([
            f"**Date:** {metadata.get('datetime', 'Unknown')}",
            # f"**Cloud Cover:** {metadata.get('cloud_cover', 'Unknown')}%",
            f"**Filename:** {metadata.get('filename', 'Unknown')}"
        ])
        
        return img, metadata_str
    except Exception as e:
        return None, f"Error processing image: {str(e)}"

# Create Gradio interface
with gr.Blocks(title="Sentinel Image Viewer") as demo:
    gr.Markdown("# Sentinel-2 Image Viewer")
    gr.Markdown("Browse and view Sentinel-2 satellite imagery")
    
    with gr.Row():
        with gr.Column(scale=1):
            date_from = gr.Textbox(label="Date From (YYYY-MM-DD)", value="2024-12-15")
            date_to = gr.Textbox(label="Date To (YYYY-MM-DD)", value="2024-12-30")
            fetch_btn = gr.Button("Fetch Image")
        
        with gr.Column(scale=2):
            image_output = gr.Image(type="pil", label="Sentinel-2 Image")
            metadata_output = gr.Markdown(label="Image Metadata")
    
    fetch_btn.click(
        fn=fetch_sentinel_image, 
        inputs=[date_from, date_to], 
        outputs=[image_output, metadata_output]
    )

    gr.Markdown("## About")
    gr.Markdown("""
    This application allows you to browse and view Sentinel-2 satellite imagery using the DESTINE API.
    
    - **TCI Images**: The images shown are true color (RGB) composites at 60m resolution.
    - **Date Range**: Specify the date range to search for images.
    """)

if __name__ == "__main__":
    demo.launch(share=True)