Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,457 +1,461 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
import
|
| 5 |
-
import
|
| 6 |
-
|
| 7 |
-
from
|
| 8 |
-
import
|
| 9 |
-
|
| 10 |
-
from
|
| 11 |
-
from src.
|
| 12 |
-
from src.utils.
|
| 13 |
-
from src.utils.
|
| 14 |
-
#
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
#
|
| 19 |
-
#
|
| 20 |
-
import
|
| 21 |
-
from
|
| 22 |
-
import
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
folium
|
| 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 |
-
# Count
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
return False #
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
#
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
#
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
#
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
s3 = None
|
| 126 |
-
s3_client = None
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
print(f"Image {item.id}
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
- **
|
| 259 |
-
- **
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
error_message
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
#
|
| 449 |
-
# (
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
print("
|
| 456 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 457 |
demo.launch(share=True) # Or exit(1) if clients are essential
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import time
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import numpy as np
|
| 5 |
+
import io
|
| 6 |
+
import random
|
| 7 |
+
from PIL import Image
|
| 8 |
+
from dotenv import load_dotenv
|
| 9 |
+
import pystac_client
|
| 10 |
+
from datetime import datetime
|
| 11 |
+
from src.auth.auth import S3Connector
|
| 12 |
+
from src.utils.utils import extract_s3_path_from_url
|
| 13 |
+
from src.utils.stac_client import get_product_content
|
| 14 |
+
# from src.utils.geo_guesser import get_random_land_point
|
| 15 |
+
from src.utils.geo_guesser import get_random_land_point_rejection
|
| 16 |
+
from folium.plugins import HeatMap
|
| 17 |
+
|
| 18 |
+
# Assuming these utility modules are in the specified paths relative to the script
|
| 19 |
+
# If not, adjust the import paths accordingly.
|
| 20 |
+
# import geopandas as gpd # Not used in the provided snippet, can be removed if not needed elsewhere
|
| 21 |
+
# from shapely.geometry import Point # Not used, can be removed
|
| 22 |
+
# import geodatasets # Not used, can be removed
|
| 23 |
+
|
| 24 |
+
import folium
|
| 25 |
+
from gradio_folium import Folium
|
| 26 |
+
import tempfile
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
# Function to create a Folium map for a location
|
| 30 |
+
def create_location_map(latitude, longitude):
|
| 31 |
+
try:
|
| 32 |
+
# Create a folium map centered at the point
|
| 33 |
+
m = folium.Map(location=[latitude, longitude], width='100%', height='100%', zoom_start=7, tiles="Cartodb dark_matter")
|
| 34 |
+
|
| 35 |
+
# Add a marker for the point
|
| 36 |
+
folium.Marker(
|
| 37 |
+
location=[latitude, longitude],
|
| 38 |
+
popup=f"Image Location<br>Lon: {longitude:.2f}<br>Lat: {latitude:.2f}",
|
| 39 |
+
icon=folium.Icon(color='red', icon='camera')
|
| 40 |
+
).add_to(m)
|
| 41 |
+
|
| 42 |
+
return m
|
| 43 |
+
except Exception as e:
|
| 44 |
+
print(f"Error creating map: {e}")
|
| 45 |
+
return folium.Map(location=[0, 0], zoom_start=2)
|
| 46 |
+
|
| 47 |
+
# --- START MODIFICATION: Add Image Quality Check Function ---
|
| 48 |
+
def check_image_quality(img: Image.Image, max_black_pct=15.0, max_white_pct=50.0):
|
| 49 |
+
"""
|
| 50 |
+
Checks if a PIL Image has excessive black or white pixels.
|
| 51 |
+
|
| 52 |
+
Args:
|
| 53 |
+
img: The PIL Image object.
|
| 54 |
+
max_black_pct: Maximum allowed percentage of pure black pixels.
|
| 55 |
+
max_white_pct: Maximum allowed percentage of pure white pixels.
|
| 56 |
+
|
| 57 |
+
Returns:
|
| 58 |
+
True if the image quality is problematic (too much black/white), False otherwise.
|
| 59 |
+
"""
|
| 60 |
+
try:
|
| 61 |
+
img_array = np.array(img)
|
| 62 |
+
# Ensure it's an RGB image for consistent checks
|
| 63 |
+
if img_array.ndim != 3 or img_array.shape[2] != 3:
|
| 64 |
+
print("Warning: Image is not in standard RGB format. Skipping quality check.")
|
| 65 |
+
return False # Assume okay if not standard RGB
|
| 66 |
+
|
| 67 |
+
total_pixels = img_array.shape[0] * img_array.shape[1]
|
| 68 |
+
if total_pixels == 0:
|
| 69 |
+
print("Warning: Image has zero pixels.")
|
| 70 |
+
return True # Problematic if no pixels
|
| 71 |
+
|
| 72 |
+
# Count black pixels (0, 0, 0)
|
| 73 |
+
black_pixels = np.sum(np.all(img_array == [0, 0, 0], axis=2))
|
| 74 |
+
black_pct = (black_pixels / total_pixels) * 100
|
| 75 |
+
|
| 76 |
+
# Count white pixels (255, 255, 255)
|
| 77 |
+
white_pixels = np.sum(np.all(img_array == [255, 255, 255], axis=2))
|
| 78 |
+
white_pct = (white_pixels / total_pixels) * 100
|
| 79 |
+
|
| 80 |
+
print(f"Image Quality Check - Black: {black_pct:.2f}%, White: {white_pct:.2f}%")
|
| 81 |
+
|
| 82 |
+
if black_pct > max_black_pct:
|
| 83 |
+
print(f"Image rejected: Exceeds black pixel threshold ({black_pct:.2f}% > {max_black_pct}%)")
|
| 84 |
+
return True # Problematic
|
| 85 |
+
if white_pct > max_white_pct:
|
| 86 |
+
print(f"Image rejected: Exceeds white pixel threshold ({white_pct:.2f}% > {max_white_pct}%)")
|
| 87 |
+
return True # Problematic
|
| 88 |
+
|
| 89 |
+
return False # Image quality is acceptable
|
| 90 |
+
except Exception as e:
|
| 91 |
+
print(f"Error during image quality check: {e}")
|
| 92 |
+
# Decide how to handle check errors, e.g., assume okay or problematic
|
| 93 |
+
return False # Let's be lenient and assume okay if check fails
|
| 94 |
+
# --- END MODIFICATION ---
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
# Load environment variables
|
| 98 |
+
load_dotenv()
|
| 99 |
+
|
| 100 |
+
# Get credentials from environment variables
|
| 101 |
+
ACCESS_KEY_ID = os.environ.get("ACCESS_KEY_ID")
|
| 102 |
+
SECRET_ACCESS_KEY = os.environ.get("SECRET_ACCESS_KEY")
|
| 103 |
+
ENDPOINT_URL = 'https://eodata.dataspace.copernicus.eu'
|
| 104 |
+
ENDPOINT_STAC = "https://stac.dataspace.copernicus.eu/v1/"
|
| 105 |
+
BUCKET_NAME = "eodata"
|
| 106 |
+
|
| 107 |
+
# Initialize the connector
|
| 108 |
+
# Ensure S3Connector is correctly defined in src.auth.auth
|
| 109 |
+
try:
|
| 110 |
+
connector = S3Connector(
|
| 111 |
+
endpoint_url=ENDPOINT_URL,
|
| 112 |
+
access_key_id=ACCESS_KEY_ID,
|
| 113 |
+
secret_access_key=SECRET_ACCESS_KEY,
|
| 114 |
+
region_name='default' # Adjust if a specific region is needed
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
# Connect to S3
|
| 118 |
+
s3 = connector.get_s3_resource()
|
| 119 |
+
s3_client = connector.get_s3_client()
|
| 120 |
+
# buckets = connector.list_buckets() # Optional: Listing buckets might require different permissions
|
| 121 |
+
# print("Available buckets:", buckets) # Comment out if not needed or causes issues
|
| 122 |
+
except ImportError:
|
| 123 |
+
print("Error: S3Connector class not found. Ensure src/auth/auth.py exists and is correct.")
|
| 124 |
+
# Provide dummy clients if needed for Gradio interface to load without full functionality
|
| 125 |
+
s3 = None
|
| 126 |
+
s3_client = None
|
| 127 |
+
except Exception as e:
|
| 128 |
+
print(f"Error initializing S3 Connector: {e}")
|
| 129 |
+
s3 = None
|
| 130 |
+
s3_client = None
|
| 131 |
+
|
| 132 |
+
# Initialize STAC Client
|
| 133 |
+
try:
|
| 134 |
+
catalog = pystac_client.Client.open(ENDPOINT_STAC)
|
| 135 |
+
except Exception as e:
|
| 136 |
+
print(f"Error initializing STAC Client: {e}")
|
| 137 |
+
catalog = None
|
| 138 |
+
|
| 139 |
+
# --- START MODIFICATION: Update fetch_sentinel_image ---
|
| 140 |
+
def fetch_sentinel_image(longitude, latitude, date_from, date_to, cloud_cover):
|
| 141 |
+
"""Fetch a Sentinel image based on criteria, retrying if quality is poor."""
|
| 142 |
+
if not catalog or not s3_client:
|
| 143 |
+
error_message = "STAC Catalog or S3 Client not initialized. Check credentials and endpoints."
|
| 144 |
+
print(error_message)
|
| 145 |
+
default_map = folium.Map(location=[0, 0], zoom_start=2)
|
| 146 |
+
return None, error_message, default_map
|
| 147 |
+
|
| 148 |
+
try:
|
| 149 |
+
# Use the coordinates from inputs
|
| 150 |
+
LON, LAT = float(longitude), float(latitude)
|
| 151 |
+
|
| 152 |
+
# Use the date range from inputs
|
| 153 |
+
date_range = f"{date_from}/{date_to}"
|
| 154 |
+
|
| 155 |
+
cloud_query = f"eo:cloud_cover<{cloud_cover}"
|
| 156 |
+
|
| 157 |
+
# Search for items
|
| 158 |
+
search = catalog.search(
|
| 159 |
+
collections=['sentinel-2-l2a'],
|
| 160 |
+
intersects=dict(type="Point", coordinates=[LON, LAT]),
|
| 161 |
+
datetime=date_range,
|
| 162 |
+
query=[cloud_query],
|
| 163 |
+
max_items=20 # Fetch a few items to have alternatives for quality check
|
| 164 |
+
)
|
| 165 |
+
# It's often better to get items as a list directly if possible
|
| 166 |
+
# Depending on pystac_client version, .items() or .item_collection() might be preferred
|
| 167 |
+
# Using item_collection and converting to list for broader compatibility
|
| 168 |
+
items_collection = search.item_collection()
|
| 169 |
+
items_list = list(items_collection)
|
| 170 |
+
|
| 171 |
+
|
| 172 |
+
if len(items_list) == 0:
|
| 173 |
+
# Return a default map with no data
|
| 174 |
+
default_map = create_location_map(LAT, LON) # Use helper function
|
| 175 |
+
folium.Marker(
|
| 176 |
+
location=[LAT, LON],
|
| 177 |
+
popup=f"No images found at this location\nLon: {LON:.2f}, Lat: {LAT:.2f}\nwithin {date_from} to {date_to}\nand cloud cover < {cloud_cover}%",
|
| 178 |
+
icon=folium.Icon(color='gray', icon='question-sign')
|
| 179 |
+
).add_to(default_map)
|
| 180 |
+
|
| 181 |
+
return None, f"No images found for the specified criteria at coordinates ({LON}, {LAT}) with cloud cover < {cloud_cover}%.", default_map
|
| 182 |
+
|
| 183 |
+
# Shuffle the list to try different items if multiple calls are made
|
| 184 |
+
random.shuffle(items_list)
|
| 185 |
+
|
| 186 |
+
MAX_QUALITY_ATTEMPTS = 20 # Max images to check for quality from the found list
|
| 187 |
+
selected_item = None
|
| 188 |
+
img = None
|
| 189 |
+
product_url = None
|
| 190 |
+
metadata = "Failed to retrieve a suitable quality image." # Default failure msg
|
| 191 |
+
|
| 192 |
+
for attempt, item in enumerate(items_list):
|
| 193 |
+
if attempt >= MAX_QUALITY_ATTEMPTS:
|
| 194 |
+
print(f"Checked {MAX_QUALITY_ATTEMPTS} images, none passed quality criteria.")
|
| 195 |
+
metadata = f"Found {len(items_list)} images, but the first {MAX_QUALITY_ATTEMPTS} checked failed quality check (Black > 15% or White > 50%)."
|
| 196 |
+
break # Stop trying after max attempts
|
| 197 |
+
|
| 198 |
+
print(f"Attempt {attempt + 1}/{min(MAX_QUALITY_ATTEMPTS, len(items_list))}: Trying item {item.id}")
|
| 199 |
+
|
| 200 |
+
try:
|
| 201 |
+
# Ensure 'TCI_60m' asset exists
|
| 202 |
+
if 'TCI_60m' not in item.assets:
|
| 203 |
+
print(f"Item {item.id} does not have a 'TCI_60m' asset. Skipping.")
|
| 204 |
+
continue
|
| 205 |
+
|
| 206 |
+
# Get the TCI_60m asset from the randomly selected item
|
| 207 |
+
current_product_url = extract_s3_path_from_url(item.assets['TCI_60m'].href)
|
| 208 |
+
# Ensure get_product_content is correctly defined in src.utils.stac_client
|
| 209 |
+
product_content = get_product_content(s3_client=s3_client, bucket_name=BUCKET_NAME,
|
| 210 |
+
object_url=current_product_url)
|
| 211 |
+
print(f"Selected product URL: {current_product_url}")
|
| 212 |
+
|
| 213 |
+
# Convert to PIL Image
|
| 214 |
+
current_img = Image.open(io.BytesIO(product_content))
|
| 215 |
+
|
| 216 |
+
# Perform image quality check
|
| 217 |
+
if not check_image_quality(current_img, max_black_pct=15.0, max_white_pct=50.0):
|
| 218 |
+
# Quality is good, select this image
|
| 219 |
+
selected_item = item
|
| 220 |
+
img = current_img
|
| 221 |
+
product_url = current_product_url
|
| 222 |
+
print(f"Image {item.id} passed quality check.")
|
| 223 |
+
break # Found a good image, exit the loop
|
| 224 |
+
else:
|
| 225 |
+
# Quality is bad, close image and loop continues
|
| 226 |
+
print(f"Image {item.id} failed quality check. Trying next.")
|
| 227 |
+
current_img.close() # Close the problematic image
|
| 228 |
+
|
| 229 |
+
except (FileNotFoundError, KeyError) as asset_err: # Handle S3 errors or missing keys
|
| 230 |
+
print(f"Error accessing asset for item {item.id}: {asset_err}. Skipping.")
|
| 231 |
+
continue # Try the next item
|
| 232 |
+
except Exception as proc_err:
|
| 233 |
+
print(f"Error processing item {item.id}: {proc_err}. Skipping.")
|
| 234 |
+
# Close image if it was opened before error
|
| 235 |
+
if 'current_img' in locals() and hasattr(current_img, 'close'):
|
| 236 |
+
current_img.close()
|
| 237 |
+
continue # Try the next item
|
| 238 |
+
|
| 239 |
+
# After the loop, check if a good image was found
|
| 240 |
+
if selected_item and img:
|
| 241 |
+
# Format datetime for readability
|
| 242 |
+
datetime_str = selected_item.properties.get('datetime', 'N/A')
|
| 243 |
+
try:
|
| 244 |
+
# Handle potential timezone 'Z'
|
| 245 |
+
if isinstance(datetime_str, str) and datetime_str.endswith('Z'):
|
| 246 |
+
datetime_str = datetime_str[:-1] + '+00:00'
|
| 247 |
+
dt = datetime.fromisoformat(datetime_str)
|
| 248 |
+
formatted_date = dt.strftime('%Y-%m-%d %H:%M:%S UTC')
|
| 249 |
+
except ValueError:
|
| 250 |
+
formatted_date = datetime_str # Keep original if parsing fails
|
| 251 |
+
except Exception as date_e:
|
| 252 |
+
print(f"Date formatting error: {date_e}")
|
| 253 |
+
formatted_date = datetime_str # Fallback
|
| 254 |
+
|
| 255 |
+
# Extract metadata for display
|
| 256 |
+
metadata = f"""
|
| 257 |
+
## Product Information
|
| 258 |
+
- **Location**: {LAT:.4f}°N, {LON:.4f}°E
|
| 259 |
+
- **Date**: {formatted_date}
|
| 260 |
+
- **Cloud Cover**: {selected_item.properties.get('eo:cloud_cover', 'N/A')}%
|
| 261 |
+
- **Cloud Cover Threshold**: < {cloud_cover}%
|
| 262 |
+
- **Satellite**: {selected_item.properties.get('platform', 'N/A')}
|
| 263 |
+
- **Product ID**: {selected_item.id}
|
| 264 |
+
"""
|
| 265 |
+
|
| 266 |
+
# Create a location map
|
| 267 |
+
location_map = create_location_map(LAT, LON)
|
| 268 |
+
|
| 269 |
+
return img, metadata, location_map
|
| 270 |
+
else:
|
| 271 |
+
# If loop finished without finding a good image
|
| 272 |
+
default_map = create_location_map(LAT, LON) # Show location map
|
| 273 |
+
folium.Marker(
|
| 274 |
+
location=[LAT, LON],
|
| 275 |
+
popup=f"Found {len(items_list)} images, but none passed quality check (checked up to {MAX_QUALITY_ATTEMPTS}).\nLon: {LON:.2f}, Lat: {LAT:.2f}",
|
| 276 |
+
icon=folium.Icon(color='orange', icon='exclamation-sign')
|
| 277 |
+
).add_to(default_map)
|
| 278 |
+
# Use the metadata message set earlier if loop failed
|
| 279 |
+
return None, metadata, default_map
|
| 280 |
+
|
| 281 |
+
|
| 282 |
+
except ValueError as ve:
|
| 283 |
+
error_message = f"Invalid input: {str(ve)}. Please ensure longitude and latitude are valid numbers."
|
| 284 |
+
print(error_message)
|
| 285 |
+
default_map = folium.Map(location=[0, 0], zoom_start=2)
|
| 286 |
+
return None, error_message, default_map
|
| 287 |
+
except pystac_client.exceptions.APIError as api_err:
|
| 288 |
+
error_message = f"STAC API Error: {api_err}. Check STAC endpoint and query parameters."
|
| 289 |
+
print(error_message)
|
| 290 |
+
default_map = folium.Map(location=[0,0], zoom_start=2)
|
| 291 |
+
return None, error_message, default_map
|
| 292 |
+
except Exception as e:
|
| 293 |
+
error_message = f"An unexpected error occurred: {str(e)}"
|
| 294 |
+
import traceback
|
| 295 |
+
print(error_message)
|
| 296 |
+
traceback.print_exc() # Print full traceback for debugging
|
| 297 |
+
default_map = folium.Map(location=[0, 0], zoom_start=2)
|
| 298 |
+
return None, error_message, default_map
|
| 299 |
+
# --- END MODIFICATION ---
|
| 300 |
+
|
| 301 |
+
|
| 302 |
+
# Function to handle random location and auto-fetch
|
| 303 |
+
def random_location_and_fetch(date_from, date_to, cloud_cover):
|
| 304 |
+
"""Get a random land location and fetch an image from there."""
|
| 305 |
+
# Ensure get_random_land_point is correctly defined in src.utils.geo_guesser
|
| 306 |
+
try:
|
| 307 |
+
lon, lat = get_random_land_point_rejection()
|
| 308 |
+
except ImportError:
|
| 309 |
+
print("Error: get_random_land_point_rejection function not found. Ensure src/utils/geo_guesser.py exists.")
|
| 310 |
+
return 0, 0, None, "Error: Cannot generate random point.", folium.Map(location=[0,0], zoom_start=2)
|
| 311 |
+
except Exception as e:
|
| 312 |
+
print(f"Error getting random land point: {e}")
|
| 313 |
+
return 0, 0, None, f"Error generating random point: {e}", folium.Map(location=[0,0], zoom_start=2)
|
| 314 |
+
|
| 315 |
+
print(f"Random land point selected: Longitude={lon}, Latitude={lat}")
|
| 316 |
+
|
| 317 |
+
img, metadata, location_map = fetch_sentinel_image(lon, lat, date_from, date_to, cloud_cover)
|
| 318 |
+
|
| 319 |
+
# --- START MODIFICATION: Adjust retry logic message ---
|
| 320 |
+
# The retry for *location* is now less likely needed if fetch_sentinel_image
|
| 321 |
+
# itself tries multiple images. We keep it as a fallback if an entire area
|
| 322 |
+
# yields no images or only bad quality ones repeatedly.
|
| 323 |
+
attempts = 1
|
| 324 |
+
max_attempts = 3 # Max attempts for *different random locations*
|
| 325 |
+
while img is None and attempts < max_attempts:
|
| 326 |
+
attempts += 1
|
| 327 |
+
print(f"Attempt {attempts}/{max_attempts}: No suitable image at ({lon:.2f}, {lat:.2f}). Trying another random location...")
|
| 328 |
+
try:
|
| 329 |
+
lon, lat = get_random_land_point_rejection()
|
| 330 |
+
print(f"New random land point: Longitude={lon}, Latitude={lat}")
|
| 331 |
+
img, metadata, location_map = fetch_sentinel_image(lon, lat, date_from, date_to, cloud_cover)
|
| 332 |
+
except Exception as e:
|
| 333 |
+
print(f"Error getting random land point on attempt {attempts}: {e}")
|
| 334 |
+
# Decide if you want to stop or just report error and let loop continue
|
| 335 |
+
metadata = f"Error getting random location on attempt {attempts}: {e}"
|
| 336 |
+
# Keep trying if attempts remain
|
| 337 |
+
|
| 338 |
+
if img is None:
|
| 339 |
+
# Refine the message if no image was found after multiple location attempts
|
| 340 |
+
metadata = f"Failed to find a suitable image after trying {max_attempts} random locations. The last attempt was at ({lon:.2f}, {lat:.2f}).\nDetails: {metadata}" # Append last failure reason
|
| 341 |
+
# Ensure map shows the last attempted location
|
| 342 |
+
if 'location_map' not in locals() or location_map is None:
|
| 343 |
+
location_map = create_location_map(lat, lon)
|
| 344 |
+
folium.Marker(
|
| 345 |
+
location=[lat, lon],
|
| 346 |
+
popup=f"Failed to find image after {max_attempts} attempts.\nLast try: Lon: {lon:.2f}, Lat: {lat:.2f}",
|
| 347 |
+
icon=folium.Icon(color='red', icon='times')
|
| 348 |
+
).add_to(location_map)
|
| 349 |
+
|
| 350 |
+
# Update the Gradio fields with the final lon/lat, even if fetching failed
|
| 351 |
+
return lon, lat, img, metadata, location_map
|
| 352 |
+
# --- END MODIFICATION ---
|
| 353 |
+
|
| 354 |
+
|
| 355 |
+
# Create Gradio interface
|
| 356 |
+
with gr.Blocks(title="Sentinel Product Viewer") as demo:
|
| 357 |
+
gr.Markdown("# Sentinel-2 Product Viewer")
|
| 358 |
+
gr.Markdown("Browse and view Sentinel-2 satellite product")
|
| 359 |
+
|
| 360 |
+
with gr.Row():
|
| 361 |
+
with gr.Column(scale=1):
|
| 362 |
+
# Location inputs
|
| 363 |
+
with gr.Row():
|
| 364 |
+
longitude = gr.Number(label="Longitude", value=15.0, minimum=-180, maximum=180)
|
| 365 |
+
latitude = gr.Number(label="Latitude", value=50.0, minimum=-90, maximum=90)
|
| 366 |
+
|
| 367 |
+
# Date range inputs
|
| 368 |
+
with gr.Row():
|
| 369 |
+
# Use gr.Date for better UX if Gradio version supports it well
|
| 370 |
+
# Otherwise, stick to Textbox and rely on user format
|
| 371 |
+
# date_from = gr.Date(label="Date From", value="2024-05-01")
|
| 372 |
+
# date_to = gr.Date(label="Date To", value="2025-02-01")
|
| 373 |
+
date_from = gr.Textbox(label="Date From (YYYY-MM-DD)", value="2024-05-01")
|
| 374 |
+
date_to = gr.Textbox(label="Date To (YYYY-MM-DD)", value="2025-02-01")
|
| 375 |
+
|
| 376 |
+
|
| 377 |
+
# Cloud cover slider
|
| 378 |
+
cloud_cover = gr.Slider(
|
| 379 |
+
label="Max Cloud Cover (%)",
|
| 380 |
+
minimum=0,
|
| 381 |
+
maximum=100,
|
| 382 |
+
value=20, # Lowered default for potentially better initial results
|
| 383 |
+
step=5
|
| 384 |
+
)
|
| 385 |
+
# Diverse landscape location buttons
|
| 386 |
+
gr.Markdown("### Preset Locations")
|
| 387 |
+
with gr.Row():
|
| 388 |
+
italy_btn = gr.Button("Italy")
|
| 389 |
+
amazon_btn = gr.Button("Amazon Rainforest")
|
| 390 |
+
with gr.Row():
|
| 391 |
+
tokyo_btn = gr.Button("Tokyo")
|
| 392 |
+
great_barrier_btn = gr.Button("Great Barrier Reef")
|
| 393 |
+
with gr.Row():
|
| 394 |
+
iceland_btn = gr.Button("Iceland Glacier")
|
| 395 |
+
canada_btn = gr.Button("Baffin Island")
|
| 396 |
+
|
| 397 |
+
fetch_btn = gr.Button("Fetch Image for Current Location", variant="secondary")
|
| 398 |
+
|
| 399 |
+
# Add Random Earth Location button (with distinctive styling)
|
| 400 |
+
gr.Markdown("### Random Discovery")
|
| 401 |
+
random_earth_btn = gr.Button("🌍 Get Random Earth Location & Image", variant="primary") # Changed size attr
|
| 402 |
+
|
| 403 |
+
metadata_output = gr.Markdown(label="Image Metadata")
|
| 404 |
+
|
| 405 |
+
with gr.Column(scale=2):
|
| 406 |
+
image_output = gr.Image(
|
| 407 |
+
type="pil",
|
| 408 |
+
label="Sentinel-2 Image (TCI 60m)",
|
| 409 |
+
# Removed fixed height/width to allow natural aspect ratio
|
| 410 |
+
# height=512, # Example: Set a moderate height if needed
|
| 411 |
+
# width=512,
|
| 412 |
+
show_download_button=True
|
| 413 |
+
)
|
| 414 |
+
|
| 415 |
+
map_output = Folium(
|
| 416 |
+
# Initialize with a default map view
|
| 417 |
+
value=create_location_map(50.0, 15.0), # Use initial lat/lon
|
| 418 |
+
label="Location Map",
|
| 419 |
+
height=400, # Maintain a fixed height for the map
|
| 420 |
+
)
|
| 421 |
+
|
| 422 |
+
# Button click handlers for diverse landscapes
|
| 423 |
+
# These lambda functions only update the lat/lon input fields
|
| 424 |
+
italy_btn.click(lambda: (12.39, 42.05), outputs=[longitude, latitude])
|
| 425 |
+
amazon_btn.click(lambda: (-64.7, -3.42), outputs=[longitude, latitude])
|
| 426 |
+
tokyo_btn.click(lambda: (139.70, 35.65), outputs=[longitude, latitude])
|
| 427 |
+
great_barrier_btn.click(lambda: (150.97, -20.92), outputs=[longitude, latitude])
|
| 428 |
+
iceland_btn.click(lambda: (-18.17, 64.61), outputs=[longitude, latitude])
|
| 429 |
+
canada_btn.click(lambda: (-71.56, 67.03), outputs=[longitude, latitude])
|
| 430 |
+
|
| 431 |
+
# Random Earth button - gets random location AND fetches image
|
| 432 |
+
random_earth_btn.click(
|
| 433 |
+
fn=random_location_and_fetch,
|
| 434 |
+
# Inputs: date range and cloud cover from the UI
|
| 435 |
+
inputs=[date_from, date_to, cloud_cover],
|
| 436 |
+
# Outputs: Update lon/lat fields AND image, metadata, map
|
| 437 |
+
outputs=[longitude, latitude, image_output, metadata_output, map_output]
|
| 438 |
+
)
|
| 439 |
+
|
| 440 |
+
# Main search button - uses current lat/lon, date, cloud cover
|
| 441 |
+
fetch_btn.click(
|
| 442 |
+
fn=fetch_sentinel_image,
|
| 443 |
+
inputs=[longitude, latitude, date_from, date_to, cloud_cover],
|
| 444 |
+
outputs=[image_output, metadata_output, map_output]
|
| 445 |
+
)
|
| 446 |
+
|
| 447 |
+
# Optional: Add back the About section if desired
|
| 448 |
+
# gr.Markdown("## About")
|
| 449 |
+
# ... (About text) ...
|
| 450 |
+
|
| 451 |
+
if __name__ == "__main__":
|
| 452 |
+
# Ensure necessary helper modules/functions are available
|
| 453 |
+
# (S3Connector, extract_s3_path_from_url, get_product_content, get_random_land_point_rejection)
|
| 454 |
+
if s3_client and catalog:
|
| 455 |
+
print("S3 and STAC clients initialized. Launching Gradio app.")
|
| 456 |
+
demo.launch(share=True) # Set share=False for local-only access
|
| 457 |
+
else:
|
| 458 |
+
print("Could not initialize S3 or STAC client. Ensure credentials and network access are correct.")
|
| 459 |
+
print("Gradio app will launch with limited functionality.")
|
| 460 |
+
# Optionally launch anyway, or exit
|
| 461 |
demo.launch(share=True) # Or exit(1) if clients are essential
|